1 / 31

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

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 40:Implementing Heaps

  6. 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()

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

  8. Implement CompleteBinaryTree • Already know basics, this extends BinaryTree • Use existing tree implementation to make easier • 2 implementations of BinaryTree: which to use? • Sequence-based possible should be 1st question? • Continue relying on trick of mapping children to indices • FullPQException not helpful, so can this work? • Even if so, how much extra space would this require?

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

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

  11. Sequence to Implement Heap 2 2 0 0

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

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

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

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

  16. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserve the nodes, but exchange their elements • Move nodes within tree & leave the elements

  17. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserve the nodes, but exchange their elements • Move nodes within tree & leave the elements • Preserving nodes & swapping Entrysmeans

  18. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserve the nodes, but exchange their elements • Move nodes within tree & leave the elements • Preserving nodes & swapping Entrysmeans • Setting two references (one in each node) • If decide you want to move nodes around instead

  19. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserve the nodes, but exchange their elements • Move nodes within tree & leave the elements • Preserving nodes & swapping Entrysmeans • Setting two references (one in each node) • If decide you want to move nodes around instead • Set two references for parents (one in each node)

  20. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserve the nodes, but exchange their elements • Move nodes within tree & leave the elements • Preserving nodes & swapping Entrysmeans • Setting two references (one in each node) • If decide you want to move nodes around instead • Set two references for parents (one in each node) • Set another two references for left children (1 in each)

  21. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserve the nodes, but exchange their elements • Move nodes within tree & leave the elements • Preserving nodes & swapping Entrysmeans • Setting two references (one in each node) • If decide you want to move nodes around instead • Set two references for parents (one in each node) • Set another two references for left children (1 in each) • Swap right children in each node (2 more references)

  22. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserving nodes & swapping Entrysmeans • Setting two references (one in each node) • If decide you want to move nodes around instead • Set two references for parents (one in each node) • Set another two references for left children (1 in each) • Swap right children in each node (2 more references) Either way CAN work; Which do you choose?

  23. Swapping Data in a Heap • Two ways to swap Entrys in a heap • Preserving nodes & swapping Entrysmeans • Setting two references (one in each node) • If decide you want to move nodes around instead • Set two references for parents (one in each node) • Set another two references for left children (1 in each) • Swap right children in each node (2 more references) Either way CAN work; Which do you (NOT Alec) choose?

  24. Keys In a PriorityQueue • Must order keys used within PriorityQueue • What types could be used to achieve this ordering?

  25. What Type For This Priority?

  26. What Type For This Priority?

  27. What Type For This Priority?

  28. What Type For This Priority?

  29. What Type For This Priority?

  30. For Everything Else There Are…

  31. Final Exam Schedule • Lab Mastery Exam is:Tues., Dec. 14thfrom 2:45PM – 3:45PM in OM 119 • Final Exam is: Fri., Dec. 17thfrom 8AM – 10AM in OM 200

More Related