next up previous
Next: Build the Code Table Up: Assignment 11: Huffman Codes Previous: Build the Frequency table

Build the Huffman tree

1.
Create a new class named HuffTree to represent a Huffman Tree (as seen on pages 299-301). In this case we won't bother with a sentinel (just to keep the number of classes reasonable).

public class HuffTree implements Comparable {
  int freq;
  char c;
  HuffTree left, right;
}

Since HuffTrees implement the Comparable interface, they can be inserted into a Heap. You can use your Heap implementation or mine, but you should not have to make any changes to Heap.java or Comparable.java--that's the nice thing about ADTs!

2.
Add the the methods buildHuffTree and decode to the Huffman class.

To debug your program you may want to use methods that print the contents of the tree. Keep in mind that you might not get exactly the same tree as in the book, but it should be similar in the sense that each letter should have the same code length as in the book, even if the code is not identical.



Allen Downey
1999-11-20