]> git.rkrishnan.org Git - sicp.git/commitdiff
solution to 2.69
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Thu, 16 Sep 2010 18:27:56 +0000 (23:57 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Thu, 16 Sep 2010 18:27:56 +0000 (23:57 +0530)
src/sicp/ex2_69.rkt [new file with mode: 0644]

diff --git a/src/sicp/ex2_69.rkt b/src/sicp/ex2_69.rkt
new file mode 100644 (file)
index 0000000..d169fb1
--- /dev/null
@@ -0,0 +1,25 @@
+#lang racket
+
+(require rackunit
+         "ch2_3.rkt")
+
+(define (generate-huffman-tree pairs)
+  (successive-merge (make-leaf-set pairs)))
+
+(define (successive-merge set)
+  (cond [(empty? (rest set)) (first set)]
+        [else (successive-merge (adjoin-set (make-code-tree (first set)
+                                                            (second set))
+                                            (rest (rest set))))]))
+
+(test-begin
+ (let ([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))))])
+   (check equal? 
+          (generate-huffman-tree '((A 4) (B 2) (C 1) (D 1)))
+          sample-tree)))
+
+(provide generate-huffman-tree)
\ No newline at end of file