1 / 32

Chapter 21 The Binary Heap

Chapter 21 The Binary Heap. Bernard Chen Spring 2006. What’s priority queue. Problem: find MIN and Delete . Possible methods: unsorted array sorted array Binary Search tree. What’s Binary Heap.

diep
Download Presentation

Chapter 21 The Binary Heap

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. Chapter 21The Binary Heap Bernard Chen Spring 2006

  2. What’s priority queue

  3. Problem: find MIN and Delete • Possible methods: • unsorted array • sorted array • Binary Search tree

  4. What’s Binary Heap • The Binary Heap supports the insertion of new items and delete of MIN item in logarithmic worst-case time. • It uses only an array to implement. (Instead of linked list) • It is the classic method used to implement priority queues

  5. Structure Property • A COMPLETE BINARY TREE is a tree that complete filled.

  6. Complete Binary Tree

  7. Advantages of Complete Tree • The height of a complete binary tree is at most logN • Left and right pointers are not needed

  8. Advantages of Complete Tree • The parent is in position i/2 • The left child is in position 2i • The right child is in position 2i+1

  9. Heap-Order property • In a Heap, for every node X with parent P, the key in P is smaller than or equal to the key in X

  10. 21.2 Implementation of the Basic Operations • Insert operation • Delete operation

  11. 21.2.1 The INSERT operation • Insertion is implemented by creating a hole at the next available location and then percolating it up until the new item can be placed in it without violating a heap order. • Insert takes constant time on average but logarithmic time in worst case.

  12. Insert step 1

  13. Insert step 2

  14. Insert step 3

  15. Insert step 4

  16. Insert step 5

  17. 21.2.2 The DeleteMIN Operation • Deletion of the min involves placing the former last item in a hole that is created at the root. • The hole is percolated down the tree through min children until the item can be placed without violating the heap order property.

  18. DeleteMIN step 1

  19. DeleteMIN step 2

  20. DeleteMIN step 3

  21. DeleteMIN step 4

  22. DeleteMIN step 5

  23. 21.3 the buildHeap Operation:Linear-Time Heap construction • The buildHeap operation can be done in linear time by applying a percolate down routine to nodes in reverse order

  24. BuildHeap step 1

  25. BuildHeap step 2

  26. BuildHeap step 3

  27. BuildHeap step 4

  28. BuildHeap step 5

  29. BuildHeap step 6

  30. BuildHeap step 7

  31. BuildHeap step 8

  32. BuildHeap step 8

More Related