1 / 51

Problem of the Day

Problem of the Day. You are trapped alone in a dark room with: Candle; Wood stove ; and Gas lamp (with full tank). You only have one match; what do you light 1 st ?. Problem of the Day. You are trapped alone in a dark room with: Candle; Wood stove ; and Gas lamp (with full tank).

neka
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 • You are trapped alone in a dark room with: • Candle; • Wood stove; and • Gas lamp (with full tank). • You only have one match; what do you light 1st?

  2. Problem of the Day • You are trapped alone in a dark room with: • Candle; • Wood stove; and • Gas lamp (with full tank). • You only have one match; what do you light 1st?

  3. Problem of the Day • You are trapped alone in a dark room with: • Candle; • Wood stove; and • Gas lamp (with full tank). • You only have one match; what do you light 1st?

  4. Problem of the Day • You are trapped alone in a dark room with: • Candle; • Wood stove; and • Gas lamp (with full tank). • You only have one match; what do you light 1st? The match!

  5. CSC 212 – Data Structures Lecture 37:Complete Binary Trees & Heaps

  6. Heaps • Binary-tree implementation with add & remove • 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’s value smaller than its children’s values • Structure must form a complete binary tree 2 5 9 7 6

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

  8. Complete Binary Tree • Specific way to organize a BinaryTree • Add & remove location defined so can be discussed • For this idea, 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

  9. Complete Binary Tree • Specific way to organize a BinaryTree • Add & remove location defined so can be discussed • For this idea, 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

  10. What Is Purpose of a Heap? • Root has critical element that we always access • Element at root always has smallest value in heap • O(1)access time to root without any real effort • Complete binary trees makes adding data easy • Create leftmost child on lowest level • When a level completes, start next one • Useful when:

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

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

  13. addElement() in a Heap 2 5 6 9 7 1

  14. Start Your Reheapify! 2 5 6 9 7 1

  15. Start Your Reheapify! 2 5 6 9 7 1

  16. Start Your Reheapify! 2 5 1 9 7 6

  17. Reheapify Must Continue 2 5 1 9 7 6

  18. Reheapify Must Continue 2 5 1 9 7 6

  19. Reheapify Sounds Icky 1 5 2 9 7 6

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

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

  22. addElement() Once Again 1 5 2 9 7 6 3

  23. Reheapify Begins Anew 1 5 2 9 7 6 3

  24. Heap-Order Property Maintained 1 5 2 9 7 6 3

  25. Done With This Reheapify! 1 5 2 9 7 6 3

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

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

  28. Removal Reheapify • Restores heap’s order during removeMin() • Reheapify removal work starts at root • Swap with smallest child, if out-of-order • Process then continues with old smallest child • Stop at leaf or when node is legal

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

  30. Move Last Element Up To Root 1 2 5 7 9

  31. Move Last Element Up To Root 1 2 5 7 9

  32. Move Last Element Up To Root 9 2 5 7

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

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

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

  36. Continue Going Down W/Node 2 9 5 7

  37. Continue Going Down W/Node 2 9 5 7

  38. Swap If Out Of Order 2 7 5 9

  39. Check If We Should Continue 2 7 5 9

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

  41. Implementation Excuses • Reheapifysteps willtravel height of tree • O(logn) running time for each of these • Serves as bound for adding & removing from Heap • What drawbacks does heap have? • Coding and understanding heap can be confusing • Use unclear w/o PriorityQueue(hint: Monday)

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

  43. Array-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?

  44. Array to Implement Heap 2 2 0 0

  45. Array to Implement Heap 2 9 2 0 1 9 0 1

  46. Array to Implement Heap 2 9 3 2 0 1 9 2 3 ` 0 1 2

  47. Array to Implement Heap 9 1 0 2 2 99 3 99 3 9 2 3 ` 0 1 2 3

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

  49. Heaps • Binary-tree implementation with add & remove • 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’s value smaller than its children’s values • Structure must form a complete binary tree 2 5 9 7 6

  50. Your Turn • Get into your groups and complete activity

More Related