# 2.69

## 练习 2.69 下面过程以一个符号-频度对偶表为参数（其中没有任何符号出现在多于一个对偶中），并根据Huffman算法生成Huffman编码树。

(define (generate-huffman-tree pairs)
(successive-merge (make-leaf-set pairs))
)


make-leaf-set的代码如下：

(define (make-leaf-set pairs)
(if (null? pairs)
'()
(let ((pair (car pairs)))
(make-leaf-set (cdr pairs))
)
)
)
)


(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 (leaf? object) (eq? (car object) 'leaf))

(define (left-branch tree) (car tree))

(define (encode-symbol-with-code char tree code)
(if (leaf? tree)
(if (eq? (symbol-leaf tree) char)
(list code)
'()
)

(let
(
(left (encode-symbol-with-code char (left-branch tree) '0))
(right (encode-symbol-with-code char (right-branch tree) '1))
)

(let
(
(res (append left right))
)

(if (null? res)
'()
(append (list code) res)
)
)
)
)
)

(define (encode-symbol char tree)
; (encode-symbol-with-code char tree '0)

(if (leaf? tree)
(if (eq? (symbol-leaf tree) char)
(list '0)
'()
)

(let
(
(left-res (encode-symbol-with-code char (left-branch tree) '0))
(right-res (encode-symbol-with-code char (right-branch tree) '1))
)

(append left-res right-res)
)
)
)

(encode-symbol 'A (list 'leaf 'A 1))


decode 的代码如下：

(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 (make-leaf symbol weight)
(list 'leaf symbol weight)
)

(define (make-code-tree left right)
(list
left
right
(append (symbols left) (symbols right))
(+ (weight left) (weight right))
)
)

(define (symbols tree)
(if (leaf? tree)
(list (symbol-leaf tree))
)

(define (weight tree)
(if
(leaf? tree)
(weight-leaf tree)
)
)

(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)
)
)
)
)

(define sample-message '(0 1 1 0 0 1 0 1 0 1 1 1 0))

(decode sample-message sample-tree)

(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)
)
)
)
)

(encode-symbol 'A sample-tree)


(define sorted (make-leaf-set (list (list 'A 8) (list 'B 4) (list 'C 2) (list 'D 1))))

sorted

(define p (list
(list 'A 2)
(list 'NA 16)
(list 'BOOM 1)
(list 'SHA 3)
(list 'GET 2)
(list 'YIP 9)
(list 'JOB 2)
(list 'WAH 1)
))

(make-leaf-set
p
)


(define (successive-merge sorted-leaf-set)
(if (<= (length sorted-leaf-set) 1)
sorted-leaf-set
(let
(
(left (car sorted-leaf-set))
)

(let
(
(new-leaf (make-code-tree left right))
(rest (cddr sorted-leaf-set))
)

(if (null? rest)
new-leaf
(successive-merge
)
)
)
)
)
)

(successive-merge sorted)

(generate-huffman-tree p)