#lang racket
(define (element-of-
set? x set
)(cond (
(null
?set
) #f)
((equal? x (car set
)) #t)
(else
(element-of-
set? x (cdr set))
)))(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
(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-l 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-l (cdr bits) tree)
)(decode-l (cdr bits) next-branch)))
))(decode-l bits tree)
)(define (choose-branch bit branch)
(cond (
(= bit 0
)(left-branch branch))(
(= bit 1
)(right-branch branch))(
else
(error "bad bit -- chosse-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)
(cadr pair)
)(make-leaf-
set(cdr pairs)))
)))(define (encode message tree)(if
(null
? message)
'()(encode (cdr message) tree)))
)(define (encode-symbol message tree)
(cond (
(leaf? tree)
null)(
(element-of-
set? message (symbols (left-branch tree)))
(cons 0
(encode-symbol message (left-branch tree)))
)((element-of-
set? message (symbols (right-branch tree)))
(cons 1
(encode-symbol message (right-branch tree)))
)(else
(error "bad bit -- chosse-branch" message)))
)(define (generate-huffman-tree pairs)
(successive-merge (make-leaf-
set pairs)))
(define (successive-merge set)(
if(=(length set)1
)(car set
)(successive-merge (adjoin-
set(make-code-tree (car set
)(cadr set))
(cddr set))
)))(define n5-pairs '((a
16)(b
8)(c
4)(d
2)(e
1)))
(define n10-pairs '((a
512)(b
256)(c
128)(d
64)(e
32)(f
16)(g
8)(h
4)(i
2)(j
1)))
(define n5-tree (generate-huffman-tree n5-pairs)
)(define n10-tree (generate-huffman-tree n10-pairs)
)(define n5-max '(aa
aa a)
)(define n5-min '(ee
ee e)
)(define n10-max '(aa
aaaa
aaa a)
)(define n10-min '(jj
jjjj
jjj j)
)(length (encode n5-max n5-tree)
)(length (encode n5-min n5-tree)
)(length (encode n10-max n10-tree)
)(length (encode n10-min n10-tree)
)
執行結果
5
2010
90
如果是有n個符號進行編碼,那麼,最小的編碼長度位n,最大的編碼長度為n*(n-1),因為最低頻率出現的符號,在樹的最低端,樹一共有n-1層,所以他的編碼長度是n-1,所以一共就是n*(n-1)的長度 電腦程式的構造和解釋 練習題1 44
平滑函式的公式為 f s x f x dx f x f x dx 3f s x f x dx f x f x dx 3 fs x f x dx f x f x d x 3他的幾何意義應該是將每個點的導數,變化率變小。說簡單點就是函式那塊曲線彎度比較大,加上這個函式就會減小他的彎度,直到他無限接近於一...
電腦程式的構造和解釋 練習題2 19
lang racket define no more?coin values null coin values define except first denomination coin values cdr coin values define first denomination coin va...
電腦程式的構造和解釋 練習題2 61
跟element of set?一樣的道理,當元素大於當時的set表時,就說明元素根本不在這個表裡,所以將元素直接插入到當時表的前面就可以了。最壞的情況還是把錶迴圈了一遍,最好的情況是只迴圈了一次,這樣平均的步數就是n 2次。define adjoin set x set cond null set...