Professional Documents
Culture Documents
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Basic Algorithm
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Work Space
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The (Real) Basic
Algorithm
1. Scan text to be compressed and tally
occurrence of all characters.
2. Sort or prioritize characters based on
number of occurrences in text.
3. Build Huffman code tree based on
prioritized list.
4. Perform a traversal of tree to determine
all code words.
5. Scan text again and create new file
using the Huffman codes.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
Scan the original text
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
Scan the original text
E e r i space
y s n a r l k .
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
Scan the original text
Eerie eyes seen near lake.
What is the frequency of each character in the text?
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
Prioritize characters
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
The queue after inserting all nodes
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
While priority queue contains two or
more nodes
– Create new node
– Dequeue node and make it left subtree
– Dequeue next node and make it right
subtree
– Frequency of new node equals sum of
frequency of left and right children
– Enqueue new node back into queue
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
k . r s n a sp e
1 1 2 2 2 2 4 8
E i
y l
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i
k .
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
n a 2 sp e
2 2
2 2 4 8
E i y l k .
r s
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
n a 2 sp e
2 4
2
2 2 4 8
E i y l k . r s
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
2 4 e
2 2 sp
8
4
y l k . r s
E i
n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
4 4 e
2 sp
8
4
k . r s n a
2 2
E i y l
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
4 4 4
2 sp e
4 2 2 8
k . r s n a
E i y l
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
4 4 4
e
2 2 8
r s n a
E i y l
2 sp
k .
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
4 4 4 6 e
2 sp 8
r s n a 2 2
k .
E i y l
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
4 6 e
2 2 2 8
sp
E i y l k .
8
4 4
r s n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
4 6 e 8
2 2 2 8
sp
4 4
E i y l k .
r s n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
8
e
8
4 4
10
r s n a
4
6
2 2
2 sp
E i y l k .
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
8 10
e
8 4
4 4
6
2 2 2
r s n a sp
E i y l k .
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
10
16
4
6
2 2 e 8
2 sp
E i y l k . 4 4
r s n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
10 16
4
6
e 8
2 2
2 sp
4 4
E i y l k .
r s n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
26
16
10
4 e 8
6
2 2 2 sp 4 4
E i y l k .
r s n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
•After
enqueueing
26 this node
there is only
16
10 one node left
4 e 8
in priority
6
queue.
2 2
2 sp 4 4
E i y l k .
r s n a
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Building a Tree
Dequeue the single node
left in the queue.
26
16
This tree contains the 10
new code words for each 4 e 8
character. 6
2 2 2 sp 4 4
Frequency of root node E i y l k .
r s n a
should equal number of
characters in text.
Eerie eyes seen near lake. 26 characters
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Encoding the File
Traverse Tree for Codes
Perform a traversal
of the tree to
obtain new code 26
words 16
10
Going left is a 0
going right is a 1 4
6
e 8
code word is only 2 2 2 sp 4 4
completed when a
E i y l k .
leaf node is r s n a
reached
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Encoding the File
Traverse Tree for Codes
Char Code
E 0000
i 0001
y 0010 26
l 0011
16
k 0100 10
. 0101
4 e
space 011 6 8
e 10 2 2 2 4 4
sp
r 1100
s 1101 E i y l k .
r s n a
n 1110
a 1111
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Encoding the File
Rescan text and
encode file using Char Code
E 0000
new code words i 0001
Eerie eyes seen near lake. y 0010
l 0011
0000101100000110011 k 0100
1000101011011010011 . 0101
1110101111110001100 space 011
e 10
1111110100100101 r 1100
Why is there no need s 1101
n 1110
for a separator a 1111
character?
. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Encoding the File
Results
Have we made
things any 0000101100000110011
better? 1000101011011010011
1110101111110001100
73 bits to encode
1111110100100101
the text
ASCII would take
8 * 26 = 208 bits
If modified code used 4 bits per
character are needed. Total bits
4 * 26 = 104. Savings not as great.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Decoding the File
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Decoding the File
Once receiver has
tree it scans 26
0 go left 4 e 8
6
1 go right 2 2 2 sp 4 4
101000110111101111 E i y l k .
r s n a
01111110000110101
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Summary
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)