SICP Exercise 2.69

Question

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

Answer

Since the list is ordered, we can simply take the car and cadr of the make-code-tree result as the two elements with the lowest weight. It is important that we use adjoin-set here so that iterations of successive-merge don’t mess up the ordering.

(define (successive-merge leaves)
  (cond ((null? (cdr leaves)) (car leaves))
        (else
         (successive-merge (adjoin-set
                            (make-code-tree (car leaves) (cadr leaves))
                            (cddr leaves))))))