Please read the file, everything you need to know is on that file. The extra credit is not needed!When coding, please use Java, NetBean
assignment_4_pa.pdf

Unformatted Attachment Preview

CSC 280 Data Structures, Spring 2017
Huffman code implementation

Please refer your text book page 427 problem 8.5.

You will write a program to implement the encoding part of the Huffman code. The
decoding part will be counted as extra credits (up to 5 points for a total of 25 points for
this assignment)

Your program should do the following:
1. Accept a text file (Huffman_original.txt) as an input. A text file is provided on
BlackBoard.
2. Examine the source file’s contents and count the number of occurrences of each
character. You may assume all capital letters.
3. Place each character and its frequency (count of occurrences) into a sorted “priority”
queue. (5 points)
4. Create a Huffman tree for this message (i.e., convert the contents of this priority
queue into a binary tree with a particular structure). (5 points)
5. Create a code table (i.e., traverse the tree to discover the binary encodings of each
character). (5 points)
6. Encode the message into binary and write to the destination file
(Huffman_encoded.txt) (i.e., re-examine the source file’s contents, and for each
character, output the encoded binary version of that character to the destination file).
(5 points)
7. (Extra credits) Decode the message from binary back to the text and write the text to a
file (Huffman_text_decoded.txt) (i.e., take the destination file as an input and convert
the binary string back to text). (5 points)


Submit your source code, the 3 files mentioned above and the result (a frequency table, a
Huffman tree, and a code table) in a word doc and the encoded file to Black Board by
Friday April 28 11:59 PM.
The followings are steps of encoding sentence(s) to a Huffman code:
o Make 3 classes.
▪ HuffmanTreeNode: This will be a link as well as a tree node
• Instance variables
o key: a letter if the treenode is a leaf
o count
▪ the frequency of the letter if a node is a leaf
CSC 280 Data Structures, Spring 2017
▪ sum of counts of its children if a node is not a leaf
next:
This HuffmanTreeNode is a link, so we need this.
o
o leftChild, rightChild: This is also a tree node of a binary
tree, so we need them.
• Methods: constructor, print
▪ SortedList: Think of a HuffmanTreeNode as a link and this class as a
linked list.
• Instance variables: head
• Methods
o insert: Insert a HuffmanTreeNode in this SortedList in the
appropriate position so that the list can be sorted.
o remove: Remove the head and return it.
o isEmpty, isOneItemLeft
Huffman:
This
is the main class of this application.

• Instance variable: sortedList
• Methods
o Encode: convert a string to the Huffman code
o Decode: convert a Huffman code to the string

Use the following 3 sentences to test your algorithm.
o “SUSIE SAYS IT IS EASY.”
o “DATA STRUCTURE IS THE MOST FUN AND USEFUL CLASS IN
COMPUTER SCIENCE.”
o “COMPUTER SCIENCE IS THE BEST PROGRAM IN THE SCHOOL OF
ENGINEERING.”
Encoding
• Make a frequency table
o In a for loop, count frequency of each letter, and save in the array.
o Test: print out the table.

Next step is to build the Huffman tree using the frequency table.
o You need two constructors in the TreeNode class. One is for the initial tree with
the root node only (you need to pass the letter and the frequency count). The other
one is to construct a new tree with two children (the left child and the right child).
o You may need another member function, IsLeaf, testing whether the current node
is a leaf node.

Now, we need to make the SortedList class.
o You need a member, head.
o You need four member functions.
▪ boolean IsEmpty(): returns true when the list is empty.
CSC 280 Data Structures, Spring 2017
▪ boolean IsOneItemLeft(): returns true when one item is left in the list.
▪ void insert(TreeNode node): inserts the node in the sorted order.
▪ TreeNode remove(): removes the head node and returns the removed node.
o Test: Every item in the frequency table whose value is not zero should be inserted
to the SortedList after making a tree. You can make a traverse method in the
SortedList to print out the initial list.

We are ready to build the Huffman tree.
o From the initial list, we remove two items from it and build a HuffmanTreeNode
with two removed HuffmanTreeNodes and insert the new HuffmanTreeNode back
to the list.
o We keep doing the above until only item is left in the list;

Let’s make a lookup table.
o Make an instance variable of 256 strings (lookupTable) to save the Huffman code
for each letter.
o Make an instance variable code in the HuffmanTreeNode class.
o Make a recursive function void traverse(HuffmanTreeNode localRoot). Use
preorder traversal.
o In the traverse, test whether the localRoot is a leaf node. If so, save the code to
the lookupTable.
o Then, visit the left child if it’s not null. Then, visit the right child if it’s not null.
Before visiting, update the children’s code. When you visit the leftChild, append
“0” to its own code. When you visit the rightChild, append “1” to its own code.

Congratulations! Now you can call encode a regular string to the Huffman code.

What you need to do in the assignment 4 for extra credits is to convert the Huffman code
back to the regular sentence. Read each character from the Huffman code and follow left
link if it’s 0, or follow the right link if it’s 1 until you reach the leaf node. You can
continue to do this until you process the whole Huffman code.

Purchase answer to see full
attachment