0% found this document useful (0 votes)
22 views

Huffman Coding Tree

Uploaded by

sayanpal854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Huffman Coding Tree

Uploaded by

sayanpal854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Huffman coding tree or Huffman tree is a full binary tree in which each

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

weighted path length of A=4*2+7*2+8*2+12*2=62

weighted path length of B=4*1+7*3+8*2+12*3=77

weighted path length of C=4*3+7*3+8*2+12*1=61

Huffman Algorithm:

1. Let us take there are n weights w1,w2,w3,……wn


2. Take two minimum weights and create subtree. Suppose w1,w2are 1st
two minimum weight then sub tree will be

3. Then minimum weight will be w1+w2,w3,w4,……wn


4. Create tree for all weights.
Example:

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 1) Arrange the data in ascending order in a table.


4,5,7,8,10,12,20
Step 2) Combine first two entries of a table and by this create a parent
node.

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.

Now, codes for the given frequencies are given below:

Time complexity:
O(nlogn) is the overall time complexity. Where n is the number of
characters.

You might also like