Saint Louis University | 
    
    Computer Science 290
     | 
    
    Dept. of Math & Computer Science | 
  
For this assignment, you are allowed to work with one other student if you wish. If any student wishes to have a partner but has not been able to locate one, please let the instructor know so that we can match up partners.
Please make sure you adhere to the policies on academic integrity in this regard.
For the first Java assignment, I am going to borrow one of my favorite C++ projects from CSCI 180 (Data Structures). Although I expect it to be familiary to many of you, reimplementing a solution using Java will serve as a nice warmup to the language. I highly recommend that you use Eclipse to complete the project. Once you learn to use it, you will never want to go without.
On computer systems, all of the underlying data is represented digitally as a sequence of bits (conventionally denoted as 0's and 1's). This is true for numbers, texts, images, sound, executables, and any other data that can be stored or transmitted digitally. Yet for a program to be able to utilize any such data, it must know how to interpret that sequence of bits. We typically call this process decoding.
For motivation, we consider the case of representing text strings digitally (though the techniques we discuss here apply easily to other forms of data as well). A traditional way to represent text over a given alphabet is to have a distinct binary code for each character of the language. In English, the most common method is based on the use of ASCII (American Standard Code for Information Interchange). ASCII is an example of what is termed a fixed-length code in that the code for each character is the same number of bits; in particular, ASCII uses an 8-bit code. For example the code for the character "A" is 01000001; the code for the character "a" is 01100001. Obviously, it would be nice to use as few bits as possible to encode a long message, but the reason for ASCII using 8-bits per code is to ensure that there are enough different patterns for each character in the desired alphabet. Notice that with 8-bit codes, there are exactly 28 different possible patterns. If we look at each 8-bit pattern as if it were a binary number, we get an integer value anywhere from 0 (i.e., 00000000) up to 255 (i.e., 11111111). This provides enough codes for upper and lower cased versions of the 26 letters in the alphabet, as well as the ten numerals, various punctuations, spaces, newlines, tabs, and various other special characters.
The significance of ASCII is that it provides a standard we can rely upon. If I have a message that I am assured is encoded in ASCII, it is a very straight forward process to decode it. Starting at the beginning, I get the first 8 bits and then look up that pattern in the codebook to translated it to a character of text; then I translate the next 8 bits and so on. That said, there is nothing very special about the particular choice of which codes were assigned to which characters in ASCII; what is important is that a sender and receiver use the same code. If you were to make up your own code where "A" was assigned some other code, and "B" some other code, but you did not tell me what codes you used, I would have great difficulty in decoding your message.
That said, even for English text, there are two important reasons for considering other encodings. The first is encryption. We specifically uses ASCII as a standard so that others will be able to properly interpret our data. If our goal was to disguise the message for others, yet still make it meaningful for a friend, we might choose to have an alternate encoding that our friend knows about but others do not.
The second reason for considering other encodings is for the sake of compression. If possible, we would love to be able to represent the same information with fewer bits. This means it would take less disk space to store as well as less effort for sending through a network. Some compressions schemes save bits by losing quality (e.g. MP3 for audio); those are called lossy compression. However it is also possible to compress data in a lossless way by choosing the right encoding. This assignment explores a form of lossless compression.
As we mentioned above, ASCII is a fixed-length code (with 8 bits per character). We might reduce the overall size of a message for two reasons. First, the 8 bits are necessary in ASCII in order to have a pattern for 256 distinct characters. In some messages, perhaps not all of those characters are used, thus perhaps we could use less bits. But this is not going to help for larger messages with a rich use of characters. The key is to recognize that certain characters tend to occur much more often then others in a particular message. We can develop a coding scheme based on a tradeoff where more frequently used letters get shorter codes and less frequently used letters get longer codes. We call such a coding scheme variable-length.
We will represent our code as a proper binary tree, with a leaf node for each character of the alphabet. The code for an individual character will be represented, from left-to-right, by tracing a path from the root to that leaf. For each step the path takes to a left-child we take a '0' and for each step to a right child we take a '1'. As an example, consider the following tree:
For technical reasons, our code include a special character EOM (end-of-message) to designate the end of an encoded message. We interpret the above tree as a code over the alphabet {a,b,n,EOM} with codes:
| Character | Our Code | 
|---|---|
| a | 0 | 
| b | 100 | 
| EOM | 101 | 
| n | 11 | 
 In a later assignment, we will explore how we might design a
tree which is optimized for the character frequency of a
particular message.  For now, we consider things more from the
perspective of the decoder.  To be able to read the message, someone
will need to be informed of the specific codes being used.   With the
tree representation, decoding a message is rather straightforward.
The codes have a prefix-free property ensuring that one can
decode the message from left to right by repeatedly walking from the
root downward. Each
time we reach a leaf, we can output the character associated with that
leaf and then continue from the root.  For example, with a message
1000110110101 in this example, we find:
1000110110101
  ba na naEOM = banana
The reader must have a way to know when the message ends. One approach is to simply stop writing bits to the underlying file once the final character has been represented. Unfortunately, it is not quite this easy. Because of the architecture of a file system, file sizes must often be stored in larger blocks of bits, such as a byte. But a decoder cannot necessarily differentiate between final bits that are legitimate versus "junk". Therefore, we assume there is a special character in the alphabet which we denote as EOM. If we wanted to apend this character to ASCII, we would presumably want to give it a different code than all other characters. Since ASCII characters are coded from 0 to 255, a common choice for the EOM is to have it as 256. We adopt this convention, and therefore EOM has a natural nine-bit binary value (though we may assign it a different pattern in our encoding). We'll come back to this issue soon...
As mentioned earlier, for an encoded file to be useful for a reader, that reader must have knowledge of the specific code which is being used. A common approach for handling this issue is to simply place a representation of the tree itself as a header at the beginning of the message file; in this way, a reader is sure to have the code. Of course that tree representation must itself be represented at the lowest level as a sequence of zeros and ones. Since the goal is to minimize the overall number of bits, it is worthwhile trying to store the tree in as condensed a form as possible, so long as the reader can adequately reconstruct it.
Here is the approach we will use. If one were to perform a preorder traversal of a proper binary tree and furthermore were to output '0' when initially visiting each internal node, and '1' when initially visiting each external node, it turns out that this is sufficient information to be able to reconstruct the shape of the original tree. For example, the shape of the tree in the earlier diagram of this assignment would produce the pattern '0100111' in such a pre-order traversal (try it!).
Of course, we must represent not only the shape of the coding tree but also the underlying characters at the leaves (there is not relevant data for the internal nodes). Each leaf has a single character (or the special EOM character). To convey the choice of characters here, we will revert to using the original ASCII codes for each character -- however because the EOM requires a nine-bit code we will actually assume that we use an augmented nine-bit version of ASCII so that we can represent numbers from 0 to 256 (rather than 0 to 255).
Our precise file format is as follows.  We perform an pre-order
traversal. For each internal node the file contains a '0'.   For each
external node the file contains a '1' followed by an additional
nine bits which specify the character associated with that
leaf.  Looking again at the earlier example of a tree, we represent it
as follows:
0100110000100100110001011000000001001101110
  ----a----   ----b---- ---EOF--- ----n----
In the end, the complete file contains the tree representation
followed by the encoded message, thus in this example:
01001100001001001100010110000000010011011101000110110101
  ----a----   ----b---- ---EOF--- ----n----  ba na naEOM = banana
Of course the color coding shown here is not really in the file.   The
decode must infer the structure based upon the detailed conventions.
Consider an original message formatted as follows:
The coding tree we will use for this message appears as follows.This is a test. This is only a test. Testing, one, two three.
A preorder traversal of this tree, tagged with '0' for internal and '1' for external appears as: 0000110110101001110010001101101101011. Taking into consideration the insertion of 9-bit patterns for the character at each leaf, the full coding tree is represented as: The structure of this tree would be represented in binary as:
0000100101010010000010100100110111110011010000100111010001001101110001001111001100111001010001011000010011100110001001100111110000000001001101100100111011101001100001100010111001000100000010011010011001100101
     ----T---- ----\n---  ----o---- ----h----  ----t----  ----n----   ----y---- ----r---- ----,----   ----s----    ----g---- ---EOM---  ----l---- ----w----  ----a---- ----.----  ----\s---  ----i---- ----e----
With such a coding tree established, our original message is encoded beginning with,
000000111110100110111010011010110110010111110001010111
   T   h   i  s \s   i  s \s    a \s  t   e  s  t    .
We have created several sample files for you to play with. They can either be downloaded here or accessed on turing directly from the Public directory for the course. Here is a quick synopsis.
| filename | description | original size(bytes) | "compressed" size(bytes) | Compressed View | 
|---|---|---|---|---|
| banana | the word 'banana' | 6 | 7 | view | 
| testing | the preceding example | 62 | 57 | view | 
| hwExample | an example given as a previous homework exercise for CSCI 180 | 28 | 36 | view | 
| index | HTML source for this program specification | 24744 | 15275 | |
| moby | Moby Dick | 1221175 | 696916 | 
The end goal for this assignment is to have a functioning decode. We will provide a driver that prompts the user for the name of a compressed file (i.e., the combined tree and message). It then prints the decoded message to the console. Your responsibilities will be to implement a HuffmanTree class with the following two public behaviors:
      Construct a tree from a bit stream
      
      Though you are not required to use recursion, we strongly
      encourage you to use recursion to your benefit.
      Think of the following.   Read the first bit of the (sub)tree
      represenation.  If it is a '1' then it represents a single leaf:  read
      the next nine bits which is the (extended) ASCII representation
      of the character.    If that first bit had been a '0' then the
      current node should be internal.   Therefore, the root of the
      larger tree should have a left and right subtree, each of which
      can be constructed from what remains of the bit stream.
  
      Decode the Message
      
      Once the tree constructed, the remaining bits
      comprise the actual message.  Read it bit-by-bit while
      simulating walks from the root of the tree down to leaves,
      outputing a character to the result each time you reach a leaf,
      until you find the designated EOM character which ends the message.
  
This project will rely upon a combination of classes, two of which are provided by us, and some which are part of the standard Java libraries. Those provided by us can be download here, or found on turing in /Public/goldwasser/290/assignments/decode/src.
      HuffmanTree
      
      This is the class which you must implement.  We are not
      providing you with any code, but the link above provides
      documentation for the following two behaviors that you must
      implement:
      
      BitReader
      
      The standard Java classes for input streams tend to allow for
      data to be read byte-by-byte because that is how it is organized
      in memory and in stored files.  But for this assignment, it is
      more intuitive to be able to read one more bits at a time. This
      can be accomplished by reading a byte at a time, using
      bit-manipulation operators to extract individual bits, and then
      leaving any still unexamined bits buffered.  To simplify the
      assignment, we have provided a simple wrapper class with such
      functionality.
  
      Decoder
      
      This is a simple class with a static main function that serves
      as the main driver for the program.  It prompts the user for the
      original file name, creates a BitReader for the file,
      and calls the HuffmanTree constructor based on that
      stream.  Once the construction is done, it presumes that any
      bits still remaining in the reader comprise the message, so it
      calls the decode method of the tree class accordingly.
  
      OutputStream
      
      This defines an interface that is part of the standard
      java.io package. It is used as an abstraction for a
      variety of classes that manage output (e.g., console, files).
      All that is relevant for this project is that any object
      implementing this interface supports a
      write(int b) method to send a single byte to the
      output.
  
      HuffmanTree.java
      We expect all of your newly authored source code to be in this
      one file.
      
  
      Readme File
      
      A brief summary of your program, and any further comments you
      wish to make to the grader.  If you do the extra credit, please
      make this clear.
  
The assignment is worth 10 points.
For the required part of the assignmnt, you were allows to assume that the input file which you are decoding was indeed legitimately formatted as per the conventions discussed for this assignment. Of course, if you tried to run your decoder on some other type of file, there is a very good chance that your program will crash.
For extra credit, write your decoder so that it gracefully handles a case in which the input file does not match the expected format. In particular, when faced with any of the following three scenarios, your program should print a single, identifying error message and then gracefully exit.
A case where the "header" is mal-formed, as recognized when reaching the end of the file during the construction of the presumed encoding tree.
A case where the "header" is mal-formed, as recognized because the tree is too big (recall that we are assuming at most 256 distinct alphabet symbols at the leaves of the tree).
A case where the "message" is possibly truncated, as recognized by reaching the end of the file without ever finding the designated EOM character. In this case, your program should still generate the output file based upon the portion of the message which was decoded, yet should also print a warning message to the user identifying the concern.