Continuing to use binary trees, we are going to use Huffman trees to encode and decode files. In this assignment you will write a program that decompresses files that were compressed using Huffman encoding.
This assignment will give you more practice with:
This is where we will put all our classes for homework 11.
Download the following example files and place them in your Eclipse workspace.
Here is my workspace with the example files.
The BitReader will be at the start of the pre-order traversal of the Huffman tree. Internal nodes are indicated by a 0 bit. Leaf nodes are indicated by a 1 bit followed by the byte that represents the data in the node. Think about the steps of building the root from the pre-order traversal.
Pre-order traversal:
Given that you have built the Huffman tree, now decode the number of bytes based upon the encoding from the BitReader, writing out each byte to the out. You probably want to write a recursive helper method to traverse the tree based upon the encoding from the BitReader, 0 means go left, 1 go right, until you get to a leaf node then output the byte.
Think about the steps needed to decompress the InputStream writing it to the OutputStream.
The InputStream will be at the beginning of the .huff file. The .huff file will contain the following segments in this order:
How can you use the two previous methods to solve this problem?
Hint: The BitReader class has the method readInt that returns an Integer from the InputStream.
Hint2: You can create an instance of Huffman from a BitReader.
Your program should take a filename as a command line argument. If no such argument is given, print a usage message explaining what the program does and how to use it.
If the filename ends with a “.huff” extension, decompress the file. Remove the .huff extension to get the output filename. For example, if you run your program as
your program will decompress homework.txt.huff into homework.txt.
If the filename does not end with a “.huff” extension, what your program does is up to you (so long as it doesn’t crash). You can compress the given file instead (using the code given below), or you can just print an error message and quit. If the file you are extracting to (such as homework.txt in the example above) already exists, it is also up to you whether to simply overwrite it or to ask the user if they would like to abort.
You must also build your own test code to make sure that your implementation works. Please thoroughly test your code and briefly discuss your testing strategy. Turn in all test code.
Criterion | Excellent (100%) | Satisfactory (75%) | Borderline (50%) | Unsatisfactory (25%) | Poor (0) |
---|---|---|---|---|---|
Adherence to standards - 2 points Does it conform to standards in every detail? |
No errors. | Minor details of the assignment are violated, or poor choices are made where the assignment is unclear. | Significant details of the assignment or the underlying program intent are violated, but the program still fulfills essential functions. | Significant details of the assignment or the underlying program intent are violated, but the program still fulfills some essential functions. | Misses the point of the assignment. |
Breakdown (modular design) - 1 point Does it demonstrate good modular design? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
Correctness of code - 4 points Does it work? Does it pass JUnit? |
Passes all tests. | Works for typical input, may fail for minor special cases. | Fails for typical input, for a minor reason. | Fails for typical input, for a major reason. | No. |
Documentation, and style - 2 points Is it clear and maintainable? Does it pass CheckStyle? Are your steps documented? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
Efficiency of code - 1 point Does it use the Java features well? |
No errors. | 1-3 minor errors. | > 3 minor errors OR 1 major error. | 2 major errors | > 2 major error. |
The assignment is due on Friday at 11:55pm. You may turn it in early.