Professional Documents
Culture Documents
CS 102
CS 102
CS 102
or
CS 102
Building a Tree
Scan the original text
Building a Tree
Scan the original text
CS 102
Building a Tree
Scan the original text
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
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
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
E 1
i 1
y 1
l 1
k 1
. 1
r 2
s 2
n 2
a 2
sp 4
e 8
CS 102
Building a Tree
y 1
l 1
k 1
. 1
r 2
s 2
n 2
a 2
sp 4
e 8
2 E 1 i 1
CS 102
Building a Tree
y 1
l 1
k 1
. 1
r 2
s 2
n 2
a 2 E 1
2
i 1
sp 4
e 8
CS 102
Building a Tree
k 1
. 1
r 2
s 2
n 2
a 2
sp 4
e 8
E 1
i 1
2 y 1
l 1
CS 102
Building a Tree
k 1
. 1
r 2
s 2
n 2
a 2
2 y 1
2 l 1
sp 4
e 8
E 1
i 1
CS 102
Building a Tree
2 y 1
2 l 1
sp
2
E 1 i 1
2 k 1 . 1
CS 102
Building a Tree
2 i 1
sp
2
E 1
4 y 1 l 1
k 1 . 1
CS 102
Building a Tree
2
E 1 i 1
sp 4 . 1
e 8
y 1
l 1
4 r 2 s 2
k 1
CS 102
Building a Tree
2 E 1 i 1
y 1
2
l 1 k 1
2
. 1
sp 4 r 2
4 s 2
CS 102
Building a Tree
2 y 1
2 l 1 k 1
2 . 1
sp 4 r 2
4 s 2
e 8
E 1
i 1
4 n 2 a 2
CS 102
Building a Tree
2 y 1
2 l 1 k 1
2 . 1
sp 4 r 2
4 s 2 n 2
4 a 2
e 8
E 1
i 1
CS 102
Building a Tree
2 k 1 . 1
sp 4 r 2
4 s 2 n 2
4 a 2
4 2 E 1 i 1 2
y 1
l 1 CS 102
Building a Tree
2 k 1 . 1
sp 4 r 2
4 s 2 n 2
4 a 2 2
4 2 y 1
e 8
E 1
i 1
l 1
CS 102
Building a Tree
4 r 2 s 2 n 2
4 a 2 E 1 6 2 k 1 . 1 sp 4 2 i 1
4 2 y 1 l 1
e 8
CS 102
Building a Tree
4 r 2 s 2 n 2
4 a 2
e sp 4 8
2 E 1 i 1
y 1
2 l 1 k 1
. 1
Building a Tree
4 2 E 1 i 1 2 2 l 1
6 sp 4
e 8
y 1
k 1
. 1
8 4 r 2 CS 102 s 2 n 2 4 a 2
Building a Tree
4 2 E 1 i 1 2 2 l 1
6 sp 4
e 8 4 r 2 s 2
8 4 n 2 a 2
y 1
k 1
. 1
CS 102
Building a Tree
e 8
4
r 2 s 2 n 2
4 10 a 2 2 4 2 y 1 6
2 l 1
k 1 . 1
sp 4
E 1
i 1
CS 102
Building a Tree
e 8
10 4 4 a 2 E 1 2 i 1 2 2 l 1 6 sp 4
4
r 2 s 2 n 2
y 1
k 1
. 1
CS 102
Building a Tree
10 4 2 E 1 i 1 2 2 l 1 16 6 sp 4 e 8 4 r 2 s 2 n 2 8 4 a 2
y 1
k 1
. 1
CS 102
Building a Tree
10 4 2 E 1 i 1 2 2 l 1 6 sp 4
16 e 8 4 r 2 s 2 n 2
8 4
a 2
y 1
k 1
. 1
CS 102
Building a Tree
26
10
4 2 E 1 i 1 y 1 2 l 1 k 1 2 . 1
16 e 8 sp 4 r 2 4 s 2 n 2
8 4
a 2
CS 102
Building a Tree
After enqueueing this node there is only one node left in priority queue.
4 s 2 n 2 a 2
26 10 4 2 E 1 i 1 y 1 2 l 1 k 1 6 e 8 sp 4 . 1 r 2 4 16 8
CS 102
Building a Tree
Dequeue the single node left in the queue. This tree contains the new code words for each character.
2 10 4 2 2 6 sp 4 e 8 4 26 16 8 4
E i y l k . 1 1 1 1 1 1
r s n a 2 2 2 2
26 characters
26 10 16 e 8 sp 4 4 8 4
4 2 2 2
E i y l k . 1 1 1 1 1 1
r s n a 2 2 2 2
CS 102
26 10 16 e 8 sp 4 4 8 4
4 2 2 2
E i y l k . 1 1 1 1 1 1
r s n a 2 2 2 2
CS 102
Char E i y l k . space e r s n a
CS 102
Code 0000 0001 0010 0011 0100 0101 011 10 1100 1101 1110 1111
Tree predetermined
based on statistical analysis of text files or file types
101000110111101111 01111110000110101
E i y l k . 1 1 1 1 1 1
r s n a 2 2 2 2
CS 102
Summary
Huffman coding is a technique used to compress files for transmission Uses statistical coding
more frequently used symbols have shorter code words
Works well for text and fax transmissions An application that uses several data structures
CS 102