220 likes | 311 Views
Learn how to construct and manipulate heaps and Huffman trees efficiently. Understand the process step by step from building a heap to creating a Huffman tree. Dive into heap operations and tree construction.
E N D
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 6 1 D E G H A F C B I 9 8 7 6 4 5 3 2 1
Description • By 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 Heap • Step1 Insert all elements to be sorted into a heap structure arbitrarily [4, 2, 3, 9, 8, 5, 7, 6, 1] 4 3 2 7 9 8 5 arbitrary 6 1
Constructing Heap construct heap construct heap 4 3 2 7 9 8 5 6 1
Constructing Heap • Step 2 Recursively turn subtrees of root into heaps. 9 7 6 8 3 5 2 1 construct heap construct heap
Constructing Heap • Step 3 Use Fix Heap to insert label of root 4 not max heap fix heap 7 9 3 6 8 5 2 1
Fix Heap 9 7 4 3 6 8 5 2 1
Fix Heap 9 7 8 3 6 4 5 2 1
Resulting Heap 9 7 8 3 6 4 5 2 1
Building The Huffman Tree • Build a minimum heap which contains the nodes of all symbols with the frequency values as the keys in the message • Repeat until the heap is empty a) 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 index 1 2 3 4 5 6 7 8 9 0 symbol A B C D E F G H I frequency 4 2 3 9 8 5 7 6 1 index 1 2 3 4 5 6 7 8 9 0 symbol I B C A E F G H D frequency 1 2 3 4 8 5 7 6 9
index 1 2 3 4 5 6 7 8 9 0 symbol I B C A E F G H D frequency 1 2 3 4 8 5 7 6 9 IB3 I1 B2
index 2 3 4 5 6 7 8 9 0 1 symbol C IB F A E D G H frequency 3 3 5 4 8 9 7 6 CIB6 C3 IB3 I1 B2
index 2 3 4 5 6 7 8 9 0 1 symbol A H F G E D CIB frequency 4 6 5 7 8 9 6 CIB6 C3 AF9 IB3 A4 F5 I1 B2
index 2 3 4 5 6 7 8 9 0 1 symbol H G CIB D E AF frequency 6 7 6 9 8 9 ` HCIB12 H6 CIB6 C3 AF9 IB3 A4 F5 I1 B2
index 2 3 4 5 6 7 8 9 0 1 symbol G E AF D HCIB frequency 7 8 9 9 12 ` HCIB12 H6 CIB6 GE15 AF9 C3 IB3 G7 E8 A4 F5 I1 B2
index 2 3 4 5 6 7 8 9 0 1 symbol D HCIB AF GE frequency 9 12 9 15 ` HCIB12 H6 CIB6 DAF18 C3 D9 IB3 GE15 AF9 I1 B2 G7 A4 E8 F5
index 2 3 4 5 6 7 8 9 0 1 symbol HCIB GE DAF frequency 12 15 18 ` GEHCIB27 ` HCIB12 GE15 G7 H6 CIB6 E8 DAF18 C3 D9 IB3 AF9 I1 B2 A4 F5
index 3 4 5 6 7 8 9 0 1 2 symbol DAF GEHCIB frequency 18 27 ` DAFGEHCIB45 ` GEHCIB27 DAF18 ` D9 HCIB12 AF9 GE15 A4 G7 F5 H6 CIB6 E8 C3 IB3 I1 B2
` DAFGEHCIB45 0 1 ` GEHCIB27 DAF18 0 1 0 1 ` HCIB12 AF9 D9 GE15 0 1 0 0 1 1 A4 G7 F5 H6 CIB6 E8 0 1 C3 IB3 1 0 I1 B2