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.
Add a println to the constructor of your node class that will report the character value for every leaf node that it constructs. Remember that nodes are supposed to be added to the PriorityQueue in increasing character order. If you see any values that are out of order, then you have a bug.
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.
A: A PriorityQueue's toString behavior (as well as the result of using an iterator/foreach, or viewing the PQ in jGRASP) does not show the elements in their sorted order, so it might be confusing to use these methods of debugging. Try looking at the Lectures page for more information on how to deal with priority queues.
You can write some testing code that repeatedly calls remove on your PriorityQueue, printing the frequency of each node as it is removed from the PriorityQueue. You should see an increasing sequence of values (from low frequency to high).
HuffmanCompressor
if you want to see the debug output, say "y". This will print out the bits it is writing in the log of the output.
StackOverflowError
. Why?"
'\0'
, sometimes called the 'null character'. (Not the same as null
, the null object reference.) But it doesn't really matter what character you use in a node where the character is meaningless. The program won't examine that character anyway.