I don't understand how to draw Huffman coding tree

94 Views Asked by At

If I'm correct, Huffman Tree is made with two minimum frequencies and keeps going. For example, letters A,B,C,D,E,F has frequencies of 15, 40, 60, 10, 30, 45 (200 total). first, D would go left and A on right to make leaf nodes. second, E goes right

this is where it confuses me.

third, the next lowest frequencie letter is B. So I thought B is smaller than the current total(55) and therefore goes on the left like this:Current Tree

But the answer is to draw a new leaf nodes with B and F and then C goes to the left of 55 tree. Resulting this drawing:answer

I dont understand why B and F needs to make a new leaf node. Can someone explain?

I expected the B letter to go on the left of 55 node which makes the total of 95, next F(45) goes to left, lastly C(60) on the left. but the answer tree is not drawn as what i expected it to be. by what standards does a new tree (like B + F in the answer pic) is made?

1

There are 1 best solutions below

2
user3386109 On

Start with a list of nodes sorted by frequency:

D(10) A(15) E(30) B(40) F(45) C(60)

Then combine the two nodes with the lowest frequency (D + A) to create a new node whose frequency is the sum:

D+A(25) E(30) B(40) F(45) C(60)

After combining two nodes, the new node (DA + E) should be inserted in the list at the proper sorted position:

B(40) F(45) DA+E(55) C(60)

Always combine the two nodes with the lowest frequencies (B + F), keeping the list sorted:

DAE(55) C(60) B+F(85)

Continue combining the two lowest frequencies (DAE + C):

BF(85) DAE+C(115)

until there's only one node (BF + DAEC):

BF+DAEC(200)

When creating a new node like DA+E, the node DA is the left child of node DAE, and the node E is the right child.