1 |
|
- The program takes into account extended ASCII characters which is 0 - 255, and creates
two separate arrays that correspond with the ASCII character to frequency.
1 | // Get user input |
I used insertion sort to create code that sorted both the character and frequency array in tandem from lowest to highest frequency.
Store the already sorted character-frequncy arrays into a node structure array.
The sorted Node array provided a massive
advantage because I could simply take the first two Node structs in the array and combine
them into a new Node. The combined Node will contain the combined frequency of the first
two Node structs, and also point to the first two Node structs.I shift the sortedNode array to the left by 2. This overrides the first 2 nodes. However, the
implementation of our sortedNodes is such that the “combined” Node still points to Node
[c:2] and Node [b:3] memory locations. I also decremented the count by 2.Then, I add the “combined” Node back into the sortedNodes array and increment the count.
Then, I sort the sortedArray again.
This process repeats itself until there is one node remaining, which then the while loop
condition exits, and I assign the last remaining node as “root”Obtaining the Huffman codes. I used preorder traversal. I tried to avoid helper functions,
but in order to preorder traverse, I only knew how to do so recursively. I passed the root
Node and an empty string as arguments. Any time a left node was visited, I concatenated a
0 to the string, and any time a right node was visited, I concatenated a 1 to the string.