sicp-2-3-4
Posted on 2015-02-23 02:20:49 +0900
in SICP
Lisp
2.67
( define ( make-leaf symbol weight )
( list 'leaf symbol weight ))
( define ( leaf? object )
( eq? ( car object ) 'leaf ))
( define ( symbol-leaf x ) ( cadr x ))
( define ( weight-leaf x ) ( caddr x ))
( define ( make-code-tree left right )
( list left
right
( append ( symbols left ) ( symbols right ))
( + ( weight left ) ( weight right ))))
( define ( left-branch tree ) ( car tree ))
( define ( right-branch tree ) ( cadr tree ))
( define ( symbols tree )
( if ( leaf? tree )
( list ( symbol-leaf tree ))
( caddr tree )))
( define ( weight tree )
( if ( leaf? tree )
( weight-leaf tree )
( cadddr tree )))
( define ( decode bits tree )
( define ( decode-1 bits current-branch )
( if ( null? bits )
' ()
( let (( next-branch
( choose-branch ( car bits ) current-branch )))
( if ( leaf? next-branch )
( cons ( symbol-leaf next-branch )
( decode-1 ( cdr bits ) tree ))
( decode-1 ( cdr bits ) next-branch )))))
( decode-1 bits tree ))
( define ( choose-branch bit branch )
( cond (( = bit 0 ) ( left-branch branch ))
(( = bit 1 ) ( right-branch branch ))
( else ( error "bad bit -- CHOOSE-BRANCH" bit ))))
( define ( adjoin-set x set )
( cond (( null? set ) ( list x ))
(( < ( weight x ) ( weight ( car set ))) ( cons x set ))
( else ( cons ( car set )
( adjoin-set x ( cdr set ))))))
( define ( make-leaf-set pairs )
( if ( null? pairs )
' ()
( let (( pair ( car pairs )))
( adjoin-set ( make-leaf ( car pair ) ; symbol
( cadr pair )) ; frequency
( make-leaf-set ( cdr pairs ))))))
( define sample-tree
( make-code-tree ( make-leaf 'A 4 )
( make-code-tree
( make-leaf 'B 2 )
( make-code-tree ( make-leaf 'D 1 )
( make-leaf 'C 1 )))))
' ( 0 1 1 0 0 1 0 1 0 1 1 1 0 )
' ( A D A B B C A )
2.68
( define ( encode message tree )
( if ( null? message )
' ()
( append ( encode-symbol ( car message ) tree )
( encode ( cdr message ) tree ))))
( define ( encode-symbol symbol tree )
( cond (( not ( memq symbol ( symbols tree )))
( error "bad symbol -- encode-symbol" symbol ))
(( leaf? tree )
' ())
(( memq symbol ( symbols ( left-branch tree )))
( cons 0
( encode-symbol symbol ( left-branch tree ))))
(( memq symbol ( symbols ( right-branch tree )))
( cons 1
( encode-symbol symbol ( right-branch tree ))))))
2.69
( define ( generate-huffman-tree pairs )
( successive-merge ( make-leaf-set pairs )))
( define ( successive-merge leaves )
( if ( null? ( cdr leaves ))
( car leaves )
( successive-merge
( adjoin-set ( make-code-tree
( car leaves )
( cadr leaves ))
( cddr leaves )))))
2.70
( define lyric-tree
( generate-huffman-tree ' (( A 2 ) ( BOOM 1 ) ( GET 2 ) ( JOB 2 )
( NA 16 ) ( SHA 3 ) ( YIP 9 ) ( WAH 1 ))))
( length ( encode ' ( GET A JOB
SHA NA NA NA NA NA NA NA NA
GET A JOB
SHA NA NA NA NA NA NA NA NA
WAH YIP YIP YIP YIP YIP YIP YIP YIP YIP
SHA BOOM )
lyric-tree ))
It take 84
bits compared with the 108
bits in
fixed-length.
2.71
It takes 1
bit to encode the most frequent bit and n - 1
bits to
encode the least frequent bit.
2.72
If the encode
uses an unordered set to keep its symbols, it would
take about $O(n)$ to encode the most frequent symbol and
to encode the least frequent symbol.