I am trying to figure out what to do with my home problem. I am trying to create a Huffman tree that will encode and decode messages in Java. I have been given strings and frequency.
[a=10, b=15, c=12, e=3, nl=4, sp=13, t=1].
I know that with the Huffman Tree you take the two lowest frequencies and turn them into a tree with the sum of their frequencies as a parent. I understand that using the priority queue I can insert the entire frequency into it and use the method remove()to take out the 2 lower frequency. Then add them together to get the weight of both of them, then put that Weight back into the queue and repeat.
The final tree must contain weight
[58=root, root.left = 33, root.right = 25]
[33.left = 18, 18.left = 8, 8.left = 4]
I don’t know exactly how to even begin to implement the Huffman tree code, which can create a tree with a frequency and display the Tree. I look at other codes, and it seems that they all create from a Streaming Input Code or so.
Any help would be good at getting me going. Thanks in advance!
Suppose you can print it in a format such as: (preliminary traversal)
58
- 33
- - 18
- - - 8
- - - - 4
- - - - - 1:t
- - - - - 3:e
- - - - 4:nl
- - - 10:a
- - 15:b
- 25
- - 12:c
- - 13:sp
source
share