data structures
DESCRIPTION
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. A. B. C. D. E. F. G. H. I. 4. 2. 3. 9. 8. 5. 7. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/1.jpg)
Data Structures
Week 7: Heap/Huffman Tree
http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html
![Page 2: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/2.jpg)
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
![Page 3: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/3.jpg)
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
![Page 4: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/4.jpg)
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
![Page 5: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/5.jpg)
Constructing Heap
4
2 3
9 8
6
5
1
7
construct heap construct heap
![Page 6: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/6.jpg)
Constructing Heap
9
7 6 8
2 5
1 3
construct heap construct heap
Step 2Recursively turn subtrees of root into
heaps.
![Page 7: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/7.jpg)
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
![Page 8: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/8.jpg)
Fix Heap
9
4 7
6 8
2
5
1
3
![Page 9: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/9.jpg)
Fix Heap
9
8 7
6 4
2
5
1
3
![Page 10: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/10.jpg)
Resulting Heap
9
8 7
6 4
2
5
1
3
![Page 11: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/11.jpg)
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
![Page 12: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/12.jpg)
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
![Page 13: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/13.jpg)
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
![Page 14: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/14.jpg)
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
![Page 15: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/15.jpg)
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
![Page 16: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/16.jpg)
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
![Page 17: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/17.jpg)
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
![Page 18: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/18.jpg)
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
![Page 19: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/19.jpg)
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
![Page 20: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/20.jpg)
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
![Page 21: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/21.jpg)
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
![Page 22: Data Structures](https://reader035.vdocuments.mx/reader035/viewer/2022062500/5681556b550346895dc3365f/html5/thumbnails/22.jpg)
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