Homework 8 (Huffman) FAQ

Q: I don't understand what is going on in this assignment.
A: Take a look at the pictures in the assignment writeup. They explain how the priority queue works with the alogrithm we've given you. Be sure to also look over your section handouts for help on how to attach each node of the Huffman Tree to the new "daddy" node.
Q: "My tree doesn't get created correctly. How can I tell what's going on?"
A: Try adding debugging code or use the structure viewer in jGRASP to view your Huffman Tree. See if your tree is adding in nodes in the correct manner(pull off two, make a new node and attach them to it, re-insert into the priority queue). Take a look at the writeup for how to set up jGRASP in order to Look at the HuffmanTree. Be aware of the problem with \r characters that occur as new lines in some text-editors. Avoid such characters by making sure to right-click the input text files when you save them, rather than doing select-all, copy-paste into your editor.
Q: "The contents of my priority queue don't seem to be in sorted order. Why?"
A: A PriorityQueue's iterator/foreach/toString behavior does not show the elements in their sorted order so it might be confusing to use these methdos of debugging. Try looking at the Lectures page for more information on how to deal with priority queues.
Q: "How can I tell what bits are getting written to my compressed file?"
A: Go to the HuffMain and set its DEBUG flag to true. Then re-compress the file. Now it will actually write out the file as ASCII bytes instead of bits (example: input, output), so you can read and debug it. Or set your BitIn/OutputStream to full-byte mode by calling .setBitMode(false) on it.
Q: "Why are the EOF and last few bits missing from my file?"
A: If you don't close() your output streams, the last EOF and last few bits may not get written.
Q: "Why do the compress and decompress methods accept a normal In/OutputStream as one parameter and a BitIn/OutputStream as the other? I want to call the read/writeBit methods on the normal in/out stream but I can't."
A: When you compress a file, you are reading from a (normal) input stream representing an uncompressed ASCII file. So you don't want to read it one bit at a time. You want one byte (one ASCII character) at a time. To read a byte, call the read() method on the InputStream. You do want the output stream to be a BitOutputStream, because you want to write the data as Huffman-compressed bits, one bit at a time.

There is a similar setup in the decompress method. In that method, the input is a BitInputStream, because you're reading back in from a Huffman-compressed file, so you want to do that one bit at a time. But you're writing to a normal uncompressed ASCII file, so you are given just a normal OutputStream for that.

Q: "My priority queue crashes with a ClassCastException: Comparable. Why?"
A: The HuffmanNode class must implement the Comparable interface in order to perform effectively.
Q: "What should it do if the file to compress/decompress is empty?"
A: We won't test that case.