CSc 28
CSc 28
CSc 28
CSc 28
CSc 28
Building a Tree
Scan the original text
CSc 28
Building a Tree
Scan the original text
E, e, r, i, space, s, n, a, r, l, k
11 characters
CSc 28
Building a Tree
Scan the original text
freq.
1
1
1
1
1
1
4
8
2
2
2
228
CSc
11
characters
Building a Tree
Prioritize characters
Steps
1. Select the two lowest valued elements
2. Combine these into a tree with the root containing
the sum of the two frequencies.
3. Reorder the list... low to high
4. Repeat the combining process until there is only
one element (tree) left in the list.
CSc 28
Building a Tree
The queue after inserting all nodes (character & frequency count)
low freq. to high freq.
sp
CSc 28
Building a Tree
When priority queue contains two or more nodes
Create new node start with two lowest (on the left)
Dequeue the two lowest adjacent nodes
Dequeue left node and make it left subtree
Dequeue next node and make it right subtree
Frequency of root node equals sum of frequency of
left and right children
Enqueue root node (with its children) back into
queue placing to the right of the nodes with the
same of lower frequency
CSc 28
Building a Tree
sp
CSc 28
Building a Tree
sp
2
i
1
E
1
CSc 28
Building a Tree
2
E
1
CSc 28
i
1
sp
Building a Tree
2
E
1
i
1
2
y
1
l
1
CSc 28
sp
Building a Tree
E
1
CSc 28
i
1
y
1
l
1
sp
Building a Tree
E
1
i
1
y
1
l
1
2
k
1
.
1
CSc 28
sp
Building a Tree
E
1
2
i
1
y
1
l
1
CSc 28
k
1
.
1
sp
Building a Tree
n
2
2
E
1
i
1
y
1
2
l
1
k
1
4
r
2
s
2
CSc 28
.
1
sp
Building a Tree
E
1
2
i
1
y
1
sp
4
l
1
k
1
.
1
CSc 28
r
2
s
2
Building a Tree
E
1
2
i
1
y
1
2
l
1
k
1
sp
4
.
1
4
n
2
a
2
CSc 28
8
r
2
s
2
Building a Tree
E
1
2
i
1
y
1
2
l
1
k
1
sp
.
1
CSc 28
8
r
2
s
2
n
2
a
2
Building a Tree
2
k
1
sp
.
1
8
r
2
s
2
n
2
a
2
4
2
E
1
i
1
2
y
1
l
1
CSc 28
Building a Tree
2
k
1
sp
.
1
r
2
4
s
2
n
2
a
2
E
1
Left to right
CSc 28
i
1
2
y
1
l
1
Building a Tree
4
r
2
4
s
2
n
2
a
2
E
1
i
1
2
y
1
l
1
6
sp
4
2
k
1
.
1
CSc 28
Building a Tree
4
r
2
s
2
n
2
4
2
a
2
E
1
i
1
2
y
1
l
1
k
1
.
1
e
sp
4
CSc 28
Building a Tree
2
E
1
i
1
2
y
1
2
l
1
k
1
.
1
e
8
sp
4
8
4
4
r
2
CSc 28
s
2
n
2
a
2
Building a Tree
2
E
1
i
1
2
y
1
2
l
1
k
1
.
1
e
8
sp
4
r
2
CSc 28
4
s
2
n
2
a
2
Building a Tree
e
8
4
r
2
s
2
n
2
10
a
2
2
E
1
CSc 28
i
1
2
y
1
2
l
1
k
1
.
1
sp
4
Building a Tree
e
8
10
r
2
4
s
2
n
2
a
2
E
1
i
1
CSc 28
2
y
1
2
l
1
k
1
.
1
sp
4
Building a Tree
10
16
2
E
1
i
1
2
y
1
2
l
1
k
1
.
1
e
8
sp
4
8
4
4
r
2
CSc 28
s
2
n
2
a
2
Building a Tree
10
16
2
E
1
i
1
2
y
1
2
l
1
k
1
.
1
sp
4
e
8
8
4
4
r
2
CSc 28
s
2
n
2
a
2
Building a Tree
26
16
10
4
2
E
1
i
1
e
8
6
2
y
1
2
l
1
k
1
.
1
8
4
sp
4
r
2
CSc 28
s
2
n
2
a
2
Building a Tree
After enqueueing this
node there is only one
node left in priority
queue.
26
16
10
4
2
E
1
i
1
e
8
6
2
y
1
2
l
1
k
1
.
1
8
4
sp
4
r
2
s
2
CSc 28
n
2
a
2
Building a Tree
Dequeue the single node left
in the queue.
26
16
10
4
E i y l k .
1 1 1 1 1 1
e
8
6
sp
4
8
4
r s n a
2 2 2 2
26
16
10
4
2
E i y l k .
1 1 1 1 1 1
CSc 28
e
8
6
sp
4
8
4
r s n a
2 2 2 2
freq.
1
1
1
1
1
1
4
8
2
2
2
2
Huffman
Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111
26
16
10
4
2
E i y l k .
1 1 1 1 1 1
CSc 28
e
8
6
sp
4
8
4
r s n a
2 2 2 2
00001011000001100111000101011011010011111
01011111100011001111110100100101
CSc 28
ch
E
i
y
l
k
.
space
e
r
s
n
a
Huffman
Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111
CSc 28
Tree predetermined
Based on statistical analysis of text files or file
types
ch
E
i
y
l
k
.
space
e
r
s
n
a
Huffman
Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111
26
16
10
4
2
E i y l k .
1 1 1 1 1 1
101000110111101111011
11110000110101
CSc 28
e
8
6
sp
4
8
4
r s n a
2 2 2 2
Summary
Huffman coding is a technique used to compress files
for transmission
Uses statistical coding
more frequently used symbols have shorter codes
CSc 28
Practice
Given the coding scheme:
a: 001
b: 0001
e: 1
r: 0000
s: 0100
t: 011
x: 01010
Practice
Given the coding scheme:
a: 001
b: 0001
e: 1
r: 0000
s: 0100
t: 011
x: 01010
a) 01110100011
b
e e
b) 0001110000
s e
c) 0100101010
t
d) 0110010101010100
CSc 28
CSc 28
15
c