1 / 22

Data Structures

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.

fayre
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Week 7: Heap/Huffman Tree http://www.cs.hongik.ac.kr/~rhanha/rhanha_teaching.html

  2. 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

  3. 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

  4. 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

  5. Constructing Heap construct heap construct heap 4 3 2 7 9 8 5 6 1

  6. Constructing Heap • Step 2 Recursively turn subtrees of root into heaps. 9 7 6 8 3 5 2 1 construct heap construct heap

  7. 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

  8. Fix Heap 9 7 4 3 6 8 5 2 1

  9. Fix Heap 9 7 8 3 6 4 5 2 1

  10. Resulting Heap 9 7 8 3 6 4 5 2 1

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. ` 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

  22. Huffman Code Table

More Related