310 likes | 400 Views
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).
E N D
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?
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?
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?
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!
CSC 212 – Data Structures Lecture 40:Implementing Heaps
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()
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
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?
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
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?
Sequence to Implement Heap 2 2 0 0
Sequence to Implement Heap 2 9 2 0 1 9 0 1
Sequence to Implement Heap 2 9 3 2 0 1 9 2 3 ` 0 1 2
Sequence to Implement Heap 9 1 0 2 2 99 3 99 3 9 2 3 ` 0 1 2 3
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
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
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
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
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)
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)
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)
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?
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?
Keys In a PriorityQueue • Must order keys used within PriorityQueue • What types could be used to achieve this ordering?
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