1 / 49

Problem of the Day

Problem of the Day. To get into club, must get past bouncer Recorded correct responses of those before you Guard said five & response was four Guard said three & response was five Guard said twelve & response was six Guard said six & response was three

leyna
Download Presentation

Problem of the Day

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. Problem of the Day • To get into club, must get past bouncer • Recorded correct responses of those before you • Guard said five & response was four • Guard said three & response was five • Guard said twelve & response was six • Guard said six & response was three • Guard now says to you ten, what do you say?

  2. Problem of the Day • To get into club, must get past bouncer • Recorded correct responses of those before you • Guard said five & response was four • Guard said three & response was five • Guard said twelve & response was six • Guard said six & response was three • Guard now says to you ten, what do you say? Three!

  3. CSC 212 – Data Structures Lecture 39:Complete Binary Trees & Heaps

  4. Priority Queue ADT • Prioritizes Entrysusing their keys • ForEntrys with equal priorities, ordernot specified • Priority given to each value when added to PQ • Normally, the priority not changeable while in PQ • Access single Entry: one with the lowest priority • Returns Entryusing min()or removeMin() • Location are imaginary – only smallest matters

  5. Heaps • Binary-tree based PQimplementation • Still structured using parent-child relationship • At most 2 children & 1 parent for each node in tree • Heaps must also satisfy 2 additional properties • Parent at least as important as its children • Structure must form a complete binary tree 2 5 9 7 6

  6. D C B A Picturing Linked BinaryTree B A C      D

  7. CompleteBinaryTree • ADT which extends BinaryTree • Add & remove methods defined plus those inherited • For this ADT, trees must maintain specific shape • Fill lowest level first, then can start new level below it Legal Illegal 2 2 5 9 5 9 6 7 6 7

  8. CompleteBinaryTree • ADT which extends BinaryTree • Add & remove methods defined plus those inherited • For this ADT, trees must maintain specific shape • Fill lowest level first, then can start new level below it • Lowest level must be filled in from left-to-right Legal Illegal 2 2 5 9 5 9 7 6 7 6

  9. What Is Purpose of a Heap? • Root has critical Entrythat we always access • Entryat root always has smallest priority in Heap • O(1) access time in min() without any real effort • CompleteBinaryTreemakes insert()easy • Create leftmost child on lowest level • When a level completes, start next one • Useful when:

  10. Upheap • Insertion may violate heap-order property • Upheap immediately after adding new Entry • Goes from new node to restore heap’s order • Compare priority of node & its parent • If out of order, swap node's Entrys • Continue upheapingfrom parent node • Stop only when either case occurs: • Found properly ordered node & parent • Binary tree's root is reached

  11. insert() in a Heap 2 5 6 9 7

  12. insert() in a Heap 2 5 6 9 7 1

  13. Start your upheaping! 2 5 6 9 7 1

  14. Start your upheaping! 2 5 6 9 7 1

  15. Start your upheaping! 2 5 1 9 7 6

  16. Upheaping Must Continue 2 5 1 9 7 6

  17. Upheaping Must Continue 2 5 1 9 7 6

  18. Upheaping Sounds Icky 1 5 2 9 7 6

  19. Check If We Should Continue 1 5 2 9 7 6

  20. Stop At The Root 1 5 2 9 7 6

  21. insert() Once Again 1 5 2 9 7 6 3

  22. Upheaping Begins Anew 1 5 2 9 7 6 3

  23. Maintain Heap Order Property 1 5 2 9 7 6 3

  24. We Are Done With This Upheap! 1 5 2 9 7 6 3

  25. Removing From a Heap • removeMin()must kill Entry at heap’s root • For a complete tree, must remove last node added • How to reconcile these two different demands? • Removed node's Entry moved to the root • Then remove node from the complete tree • Heap's order preserved by going down

  26. Removing From a Heap • removeMin()must kill Entry at heap’s root • For a complete tree, must remove last node added • How to reconcile these two different demands? • Removed node's Entry moved to the root • Then remove node from the complete tree • Heap's order preserved by going down Censored

  27. Downheap • Restores heap’s order during removeMin() • Downheap work starts at root • Swap with smallest child, if at least out-of-order • Downheapingcontinues with old smallest child • Stop at leaf or when node is legal

  28. Before removeMin() is called 1 2 5 7 9

  29. Move Last Entry Up To Root 1 2 5 7 9

  30. Move Last EntryUp To Root 1 2 5 7 9

  31. Move Last EntryUp To Root 9 2 5 7

  32. Compare Parent W/Smaller Child 9 2 5 7

  33. Compare Parent W/Smaller Child 9 2 5 7

  34. Compare Parent W/Smaller Child 2 9 5 7

  35. Continue Downheaping W/Node 2 9 5 7

  36. Continue Downheaping W/Node 2 9 5 7

  37. Swap If Out Of Order 2 7 5 9

  38. Check If We Should Continue 2 7 5 9

  39. Stop When We Reach a Leaf 2 7 5 9

  40. Implementation Excuses • upheap & downheaptravel height of tree • O(logn) running time for each of these • Serves as bound for adding & removing from PQ • What drawbacks does heap have? • PriorityQueue can be faster using Sequence • Only for specific mix of operations, however • PriorityQueueusing heaps are fastest overall

  41. Sequence-based BinaryTree • Node at index specified for location in Tree • Root node stored at index 0 • Root’s left child at index 1 • Right child of root at index 2 • Left child’s right child at index 4 • Right child’s left child at index 5 • Node at index n’s left child is at index 2n + 1 • Node at index n’s right child is at index 2n + 2

  42. Sequence-based BinaryTree • Node at index specified for location in Tree • Root node stored at index 0 • Root’s left child at index 1 • Right child of root at index 2 • Left child’s right child at index 4 • Right child’s left child at index 5 • Node at index n’s left child is at index 2n + 1 • Node at index n’s right child is at index 2n + 2 But how much space will this need for to hold a heap?

  43. Sequence to Implement Heap 2 2 0 0

  44. Sequence to Implement Heap 2 9 2 0 1 9 0 1

  45. Sequence to Implement Heap 2 9 3 2 0 1 9 2 3 ` 0 1 2

  46. Sequence to Implement Heap 9 1 0 2 2 99 3 99 3 9 2 3 ` 0 1 2 3

  47. Sequence to Implement Heap 2 9 3 2 0 99 9 2 3 3 99 1 Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this! ` 0 1 2 3

  48. Heaps • Binary-tree based PQimplementation • Still structured using parent-child relationship • At most 2 children & 1 parent for each node in tree • Heaps must also satisfy 2 additional properties • Parent at least as important as its children • Can not use any tree; must form complete binary tree 2 5 9 7 6

  49. Final Exam Schedule • Lab Mastery Exam is:Tues., Dec. 13thfrom 9:00 – 10:00AM in OM 119 • Final Exam is:Thurs., Dec. 15thfrom 12:30 – 2:30PMin OM 200

More Related