-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path2.68.rkt
51 lines (39 loc) · 1.39 KB
/
2.68.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(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 (encode message tree)
(if (null? message )
'()
(append (encode-symbol (car message) tree)
(encode (cdr message) tree))))
(define (encode-symbol symbol tree)
(if (memq symbol (symbols tree))
(let ((l-b (left-branch tree)) (r-b (right-branch tree)))
(if (memq symbol (symbols l-b))
(if (leaf? l-b) (list 0) (cons 0 (encode-symbol symbol l-b)))
(if (leaf? r-b) (list 1) (cons 1 (encode-symbol symbol r-b)))))
(error "symbol not in tree --" symbol)))
(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)))))
(display (encode '(A D A B B C A) sample-tree))