Professional Documents
Culture Documents
:ProjectHuffman Tree
Submitted by:
Muzmmal Hussain
Muhammad Zia Shahid
Riasat Ali
What Is Huffman Tree ..?
Changing ensemble
• If the ensemble changes the frequencies and probabilities change
the optimal coding changes
• e.g. in text compression symbol frequencies vary with context
• Re-computing the Huffman code by running through the entire file
in advance?!
• Saving/ transmitting the code too?!
compressed representation.
different than data compression in general
LET A = 0
B = 100
C = 1010
D = 1011
R = 11
Thus, ABRACADABRA = 01001101010010110100110
So 11 letters demand 23 bits < 33 bits, an improvement of about
30%.
U s i n g Va r i a b l e - l e n g t h E n c o d i n g ( 2 )
BA?
AAA?
N o P ro b l e m …
……
if we use prefix codes: no code word is a prefix of another code word.
Any prefix code can be represented by a full binary tree.
Each leaf stores a symbol.
Each node has two children – left branch means 0, right means 1.
code word = path from the root to the leaf interpreting suitably the
left and right branches.
P re f i x C o d e s ( 2 )
ABRACADABRA
A = 0
B = 100
C = 1010
D = 1011
R = 11
Decoding is unique and simple!
Read the bit stream from left to right and starting from the root,
the two symbols with the smallest frequencies must be at the bottom
of the optimal tree, as children of the lowest internal node.
Repeat until all nodes merged into one tree:
– A: 40 B: 20 C: 10 D: 10 R: 20
Smallest numbers are 10 and 10 (C and D), so connect them
CONSTRUCTING A HUFFMAN CODE
(2)
C and D have already been used, and the new node above them
(call it C+D) has value 20
The smallest values are B, C+D, and R, all of which have value 20
It is clear that the algorithm does not construct a unique tree, but
even if we have chosen the other possible connection, the code would
be optimal too!
CONSTRUCTING A HUFFMAN CODE
(3)
Connect the final two nodes, adding 0 and 1 to each left and
right branch respectively.
A: 40 B: 20 C: 10 D: 10 R: 20
A = 0 100
1
B = 100
60
0
C = 1010
D = 1011 40 1
0
1
R = 11
0 20
0 1