Next: About this document
Up: Assignment 11: Huffman Codes
Previous: The HuffTree
Make a class called Huffman that will serve as the
central organizing class. Here is what its public interface
might look like:
public class Huffman {
private FreqTab ft;
private HuffTree ht;
private CodeTab ct;
public Huffman (String sample);
public String encode (String s);
public String decode (String s);
}
Here are some suggestions for how to proceed:
- Write buildHuffTree first and use the print method
to check it. 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.
- You can use your Heap implementation or mine. Either way, you
should not have to make any changes to Heap.java--that's the nice
thing about ADTs!
- Write the method decode next, since you don't need
the CodeTab to test it.
- Write buildCodeTable next.
You can use an array, Vector, or Hashtable to implement the
code table.
HINT: to build the code table, you have to traverse the HuffTree
and keep track of the path as you go, since the path to each
leaf is its Huffman code.
- Write encode last. If buildCodeTable works,
encode is trivial.
Allen Downey
Mon Nov 20 11:50:58 EST 2000