3
\$\begingroup\$

From SICP:

Exercise 2.69. The following procedure takes as its argument a list of symbol-frequency pairs (where no symbol appears in more than one pair) and generates a Huffman encoding tree according to the Huffman algorithm.

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

Make-leaf-set is the procedure given above that transforms the list of pairs into an ordered set of leaves. Successive-merge is the procedure you must write, using make-code-tree to successively merge the smallest-weight elements of the set until there is only one element left, which is the desired Huffman tree. (This procedure is slightly tricky, but not really complicated. If you find yourself designing a complex procedure, then you are almost certainly doing something wrong. You can take significant advantage of the fact that we are using an ordered set representation.)

I wrote the following solution:

(define (generate-huffman-tree pairs)
 (successive-merge (make-leaf-set pairs)))
(define (successive-merge leaf-set)
 (define (iter result leaf-subset)
 (if (null? leaf-subset) 
 result 
 (let ((first-leaf (car leaf-subset)))
 (iter (make-code-tree first-leaf result) (cdr leaf-subset)))))
 (iter (make-code-tree (car leaf-set) (cadr leaf-set)) (cddr leaf-set)))

Is this a good answer? Can it be improved?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Apr 23, 2011 at 14:18
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

It's been a while since I've done any Lisp/Scheme, but I'll make one style point and one algorithmic point.

Style point: it's usually more readable if you define structure projection functions with meaningful names rather than using car, cdr, etc.

Algorithmic point: the simplest way of doing this is to include some priority queue structure from the standard library. Then you simply remove the two least items from the queue, create their combined Huffman tree, and reinsert them into the queue. You're done when the queue contains only one item.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Oct 16, 2011 at 23:48
\$\endgroup\$
2
  • \$\begingroup\$ I disagree with your style point. car and cdr are so fundamental to the language that they will be well understood by anyone reading your code. Redefining them will be more painful as you'll have to deal with each codebases particular convention. \$\endgroup\$ Commented Apr 14, 2012 at 9:58
  • \$\begingroup\$ @cmh: the problem is not that the reader won't understand what car and cdr mean, rather that the reader has to keep in mind the structure of your type rather than its semantics. \$\endgroup\$ Commented May 9, 2012 at 3:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.