7 (make-code-tree (make-leaf 'A 4)
10 (make-code-tree (make-leaf 'D 1)
13 (define sample-bits '(0 1 1 0 0 1 0 1 0 1 1 1 0))
15 (define sample-message (decode sample-bits sample-tree))
17 (define (encode message tree)
20 (append (encode-symbol (car message) tree)
21 (encode (cdr message) tree))))
23 (define (encode-symbol symbol tree)
26 (eqv? (symbol-leaf tree) symbol)) '()]
27 [(member? symbol (symbols (left-branch tree))) (cons 0 (encode-symbol symbol (left-branch tree)))]
28 [(member? symbol (symbols (right-branch tree))) (cons 1 (encode-symbol symbol (right-branch tree)))]
29 [(not (member? symbol (symbols tree))) (error "Symbol not in the tree -- ENCODE-SYMBOL " symbol)]))
31 (define (member? x set)
34 [(eqv? x (car set)) #t]
35 [else (member? x (cdr set))]))
37 ;;; a simple test. encode-decode should give me back the same symbols.
39 (decode (encode sample-message