Professional Documents
Culture Documents
An Application of Binary
Trees and Priority Queues
CS 102
Encoding and
Compression of Data
Fax Machines
ASCII
Variations on ASCII
min number of bits needed
cost of savings
patterns
modifications
CS 102
Purpose of Huffman
Coding
Proposed by Dr. David A.
Huffman in 1952
A Method for the Construction
of Minimum Redundancy Codes
CS 102
or
2.
3.
4.
5.
Building a Tree
Scan the original text
Consider the following short
text:
Eerie eyes seen near lake.
Count up the occurrences of all
characters in the text
CS 102
Building a Tree
Scan the original text
Eerie eyes seen near lake.
What characters are present?
E e r i space
y s n a r l k .
CS 102
Building a Tree
Scan the original text
Eerie eyes seen near lake.
What is the frequency of each character in the text?
Char Freq.
E
1
e
8
r
2
i
1
space 4
Char Freq.
y
1
s
2
n
2
a
2
l
1
CS 102
Char Freq.
k
1
.
1
Building a Tree
Prioritize characters
Create binary tree nodes with
character and frequency of
each character
Place nodes in a priority
queue
The lower the occurrence, the
higher the priority in the queue
CS 102
Building a Tree
Prioritize characters
Uses binary tree nodes
public class HuffNode
{
public char myChar;
public int myFrequency;
public HuffNode myLeft, myRight;
}
priorityQueue myQueue;
CS 102
Building a Tree
The queue after inserting all nodes
sp
Building a Tree
While priority queue contains two or
more nodes
Create new node
Dequeue node and make
Dequeue next node and
subtree
Frequency of new node
frequency of left and
Enqueue new node back
CS 102
it left subtree
make it right
equals sum of
right children
into queue
Building a Tree
sp
CS 102
Building a Tree
sp
2
E
CS 102
Building a Tree
sp
CS 102
Building a Tree
sp
CS 102
Building a Tree
y
E
CS 102
sp
Building a Tree
2
k
CS 102
sp
Building a Tree
CS 102
2
i
sp
Building a Tree
2
i
2
l
4
r
CS 102
sp
Building a Tree
2
i
sp
4
l
CS 102
Building a Tree
2
i
2
l
sp
4
4
n
CS 102
8
r
Building a Tree
2
i
2
l
CS 102
sp
.
8
r
Building a Tree
2
k
sp
.
8
r
4
2
E
CS 102
2
y
Building a Tree
2
k
sp
.
4
s
a
E
CS 102
2
y
8
l
Building a Tree
4
r
4
s
a
E
6
sp
2
k
CS 102
2
y
8
l
Building a Tree
4
r
4
2
2
k
e
sp
Building a Tree
2
E
2
y
2
l
e
sp
.
8
4
4
r
CS 102
Building a Tree
2
E
2
y
2
l
e
sp
.
r
CS 102
Building a Tree
e
8
10
r
2
E
CS 102
2
y
2
l
sp
.
Building a Tree
e
8
10
4
s
a
E
CS 102
2
y
2
l
sp
.
Building a Tree
10
16
2
E
2
y
2
l
sp
.
r
CS 102
4
s
Building a Tree
10
16
2
E
2
y
2
l
sp
.
r
CS 102
4
s
Building a Tree
26
16
10
4
2
E
6
2
y
2
l
CS 102
sp
r
Building a Tree
After
enqueueing
this node
there is only
one node left
in priority
queue.
26
16
10
4
2
E
6
2
y
2
l
CS 102
sp
r
Building a Tree
Dequeue the single node
left in the queue.
26
4
2
16
10
e
6
2
E i y l k .
sp
8
4
r s n a
26 characters
26
16
10
4
2
6
2
E i y l k .
sp
8
4
r s n a
26
16
10
4
2
6
2
E i y l k .
sp
8
4
r s n a
0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101
Why is there no need
for a separator
character?
.
CS 102
Char Code
E 0000
i 0001
y 0010
l 0011
k 0100
. 0101
space
011
e 10
r 1100
s 1101
n 1110
a 1111
Tree predetermined
based on statistical analysis of text files or
file types
101000110111101111
01111110000110101
CS 102
26
16
10
4
2
6
2
E i y l k .
sp
8
4
r s n a
Summary
Huffman coding is a technique used
to compress files for transmission
Uses statistical coding
more frequently used symbols have
shorter code words