Huffman Coding Tree
Huffman Coding Tree
leaf of the tree corresponds to a letter in the given alphabet. Define the
weighted path length of a leaf to be its weight times its depth. The
Huffman tree is the binary tree with minimum external path weight, i.e.,
the one with the minimum sum of weighted path lengths for the given set
of leaves. So the goal is to build a tree with the minimum external path
weight.
Suppose every external node has some weights W,then the weighted path
length for the external node will be P=w1p1+w2p2+……..wnpn where w
denotes the weight and P denotes the path length of an external node .
Suppose we create different tree which have same weights on external
nodes then it is necessary that they have same weighted path length. let
us take the weights 4,7,8,12 and create three different tree
Huffman Algorithm:
Let obtain a set of Huffman code for the message (m1.....m7) with relative
frequencies (q1.....q7) = (4,5,7,8,10,12,20). Let us draw the Huffman
tree for the given set of codes.
Step 3)
A) Remove the entries 4 and 5 from the table and inert 9 at its appropriate
position. 7,8,9,10,12,20
Combine minimum value of table and create a parent node.
B) Now remove the entries 7 and 8 from the table and insert 15 at its
appropriate position. 9,10,12,15,20
Combine minimum value of two blocks and create a parent node.
C) Remove the entries 9 and 10 from the table and insert 19 at its proper
position. 12,15,19,20.
Combine minimum value of two blocks and create parent node.
D) Remove the entries 15 and 12 from the table and insert 27 at its
appropriate position. 19,20,27
Combine minimum value of two blocks and create parent node.
E) Remove the entries 19 and 20 from the table and insert 39 in the
table. 27,39
Combine minimum value of two blocks and create parent node.
Step 4) Now assign left child as 0 and right child as 1 to encode the
frequencies.
Time complexity:
O(nlogn) is the overall time complexity. Where n is the number of
characters.