Huffman Coding
Huffman Coding
Char a b c e g h i l n o r s t v w y
Freq 3 1 1 6 1 2 2 2 3 2 4 2 3 1 4 1
• Can we do better?
• .
Depth of c in T
Frequency of character c
HUFFMAN(C)
1. For to -1
2. allocate a new node .
3. EXTRACT-MIN()
4. EXTRACT-MIN()
5. INSERT(, )
6. return EXTRACT-MIN() //return root of the tree
14 d:16 25 a:45
0 1 0 1
f:5 e:9 c:12 b:13
14 d:16 25 a:45
0 1 0 1
f:5 e:9 c:12 b:13
30 a:45
25
0 1
0 1
c:12 b:13 14 d:16
0 1
f:5 e:9
a:45 55 1
0
25 30
0 1
0 1
c:12 b:13 14 d:16
0 1
f:5 e:9
BITS Pilani, Hyderabad Campus
Example
a:45 55 1
0
25 30
0 1
0 1
c:12 b:13 14 d:16
0 1
100 f:5 e:9
0 1
a:45 55 1
0
25 30
0 1
0 1
c:12 b:13 14 d:16
0 1
f:5 e:9 BITS Pilani, Hyderabad Campus
Time Complexity