Google | Onsite | Software Engineer | Huffman Coding Algorithm
Anonymous User
12950

Implement and test the Huffman Coding Algorithm. Your implementation should construct the code while constructing the Huffman tree. Test your program for the English letters ( the frequency of letters are available on the web) using the tree for encoding and decoding .
This was the task given above. I almost misunderstood the question when it was asked. This is
not the regular Huffman Coding Algorithm. It must construct the code WHILE constructing the tree not after. I was able to come up with the algorithm in the allotted time but not the implementation.

INPUT: Set of characters with their frequencies
OUTPUT: Hoffman Coding Tree and an array of strings representing the code of the characters.

Step1: Define the class Huffman Tree Node which represents the nodes of the Huffman Tree Nodes. The class contains an array of characters denoted by st ( or a set of characters, a string, a vector of characters), and the frequency . This class should implements the Interface Comparable by defining the function compareTo based on the value of the frequency.

Step2: For each input character, create an object of type Huffman TreeNode which includes the character (as an array of characters) and its frequency.

Step3: Include the class Heap after modification to change it from max heap into min heap;

        Create an empty instance of the min heap;
    
         Push all objects of Step2 onto the heap.

Step4: Repeat the following steps until the heap contains only one node(object of type Huffman Tree Node):

Step 4.1: Remove a node from the heap and assign it to the variable left( you will be removing 
 the object of type Huffman Tree Node with the smallest frequency).
 
 Step4.2: Remove a node from the heap and assign it to the variable right.

 Step4.3: Create an object of type Huffman Tree Node denoted by parent whose frequency =
  left. frequency + right. frequency and whose array st = left. st + right. st;


Step4.4: Label the left and right branches of parent with zero and one, respectively;     

Step4.5: Add 0 to the codes of the characters in the array left.st;
             Add 1 to the codes of the characters in the array right.st;

Step4.6:  Update the Huffman Tree) by connecting the objects left and right as the left and right child of parent, respectively;     

Step4.7:  Push the newly created object parent on to the heap.

END

Comments (8)