I’m trying to understand what to do with my homework problem. I am trying to create a Huffman Tree that will encode and decode messages in Java. I am given Strings and Frequency.
[a=10, b=15, c=12, e=3, nl=4, sp=13, t=1].
I know that with Huffman Tree you take the two lowest Frequencies and make them into a tree with the sum of their Frequency as the parent.
I understand that using a Priority Queue I can insert all the Frequency into it and use the remove()
method to take out the 2 lowest Frequency. Then add them together to get the Weight of them both, then insert that Weight back into the Queue and repeat.
The final Tree should hold weight of
[58=root, root.left = 33, root.right = 25]
[33.left = 18, 18.left = 8, 8.left = 4]
I am not sure exactly how to even begin to implement a Huffman Tree code that will be able to create the tree with the Frequency and display the Tree. I’ve look at other codes and it seem like they all are creating from Streaming Input Code or so.
Any help would be great in getting me going. Thanks in advance!
I’m suppose to print out with a format like : (pre-order traversal)
58
- 33
- - 18
- - - 8
- - - - 4
- - - - - 1:t
- - - - - 3:e
- - - - 4:nl
- - - 10:a
- - 15:b
- 25
- - 12:c
- - 13:sp