Data Structures Week 7: Heap/Huffman Tree http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html
Jan 27, 2016
Data Structures
Week 7: Heap/Huffman Tree
http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html
Array to Store the Heap
The same array is used both to compute the frequency
for each char and to store the heap same array
4 32 9
A CB D
8 75 6
E GF H
1
I
9 78 6
D GE H
4 35 2
A CF B
1
I
DescriptionBy replacing : the root with a leaf node and fixing the tree by swaping nodes starting from the root downwards (fix heap) a new heap obtained
If the two subtrees of the root are heaps we can obtain a new heap with two heaps and
a node of an arbitrary key value a heap from array by fixing the tree as in delete
Thus, we can create recursively making subtrees heaps making the the first element the root of two
subtrees(heaps) finally fixing the heap
Constructing HeapStep1Insert all elements to be sorted into a
heap structure arbitrarily[4, 2, 3, 9, 8, 5, 7, 6, 1]
4
2 3
9 8
6
5
1
7
arbitrary
Constructing Heap
4
2 3
9 8
6
5
1
7
construct heap construct heap
Constructing Heap
9
7 6 8
2 5
1 3
construct heap construct heap
Step 2Recursively turn subtrees of root into
heaps.
Constructing Heap
Step 3Use Fix Heap to insert label of root
4
9 7
6 8
2
5
1
3
not max heap fix heap
Fix Heap
9
4 7
6 8
2
5
1
3
Fix Heap
9
8 7
6 4
2
5
1
3
Resulting Heap
9
8 7
6 4
2
5
1
3
Building The Huffman Tree1. Build a minimum heap which contains the
nodes of all symbols with the frequency values as the keys in the message
2. Repeat until the heap is emptya) Delete two nodes from the heap
concatenate the two symbols add their frequencies insert the new node into the heap
b) Insert the new node into the Huffman tree the two nodes become the two children of the node
for the concatenate symbol
Example of The Huffman Tree
4 32 9
A CB D
8 75 6
E GF H
1
I
1 32 4 5 76 8 9index
symbol
frequency
1 32 4
I CB A
8 75 6
E GF H
9
D
1 32 4 5 76 8 9index
symbol
frequency
0
0
1 32 4
I CB A
8 75 6
E GF H
9
D
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3
3 3 5
C IB F
4 98 7
A DE G
6
H
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6
4 6 5
A H F
7 98 6
G DE CIB
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6
A4 F5
AF9
6 7 6
H G CIB
9 98
D AFE
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6
A4 F5
AF9
H6
`HCIB12
7 8 9
G E AF
9 12
D HCIB
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6
A4 F5
AF9
H6
`HCIB12
G7 E8
GE15
9 12 9
D HCIB AF
15
GE
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6
A4 F5
AF9
H6
`HCIB12
G7 E8
GE15 D9
DAF18
12 15 18
HCIB GE DAF
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6H6
`HCIB12
G7 E8
GE15
A4 F5
AF9D9
DAF18
`GEHCIB27
2718
GEHCIBDAF
1 32 4 5 76 8 9index
symbol
frequency
0
I1 B2
IB3C3
CIB6H6
`HCIB12
G7 E8
GE15
A4 F5
AF9D9
DAF18 `GEHCIB27
`DAFGEHCIB45
I1 B2
IB3C3
CIB6H6
`HCIB12
G7 E8
GE15
A4 F5
AF9D9
DAF18 `GEHCIB27
`DAFGEHCIB45
0
0
0 0
0
0
0
0
1
1 1 1
1
1
1
1
Huffman Code Table
Symbol Frequency Huffman Code
D 9 00
E 8 101
G 7 100
H 6 110
F 5 011
A 4 010
C 3 1110
B 2 11111
I 1 11110