Download presentation
Presentation is loading. Please wait.
Published byEugene Newman Modified over 9 years ago
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 1 st ?
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 1 st ?
3
Problem of the Day You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank). one match You only have one match; what do you light 1 st ?
4
Problem of the Day You are trapped alone in a dark room with: Candle; Wood stove; and Gas lamp (with full tank). one match You only have one match; what do you light 1 st ? The match!
5
CSC 212 – Data Structures
6
Priority Queue ADT Prioritizes Entry s using their keys For Entry s with equal priorities, order not 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 Entry using min() or removeMin()
7
Heaps Binary-tree based PQ implementation 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 95 67
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 1 st 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 T REE 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 T REE 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 0 2 0
12
29 01 2 01 9
13
293 012 2 01 9 2 3 `
14
29399 0123 2 01 9 2 3 3 `
15
Sequence to Implement Heap 29399 0123 2 01 9 2 3 3 ` Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this! Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this!
16
Swapping Data in a Heap Two ways to swap Entry s 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 Entry s in a heap Preserve the nodes, but exchange their elements Move nodes within tree & leave the elements Preserving nodes & swapping Entry s means
18
Swapping Data in a Heap Two ways to swap Entry s in a heap Preserve the nodes, but exchange their elements Move nodes within tree & leave the elements Preserving nodes & swapping Entry s means 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 Entry s in a heap Preserve the nodes, but exchange their elements Move nodes within tree & leave the elements Preserving nodes & swapping Entry s means 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 Entry s in a heap Preserve the nodes, but exchange their elements Move nodes within tree & leave the elements Preserving nodes & swapping Entry s means 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 Entry s in a heap Preserve the nodes, but exchange their elements Move nodes within tree & leave the elements Preserving nodes & swapping Entry s means 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 Entry s in a heap Preserving nodes & swapping Entry s means 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
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?
30
For Everything Else There Are…
31
Final Exam Schedule Lab Mastery Exam is: Tues., Dec. 14 th from 2:45PM – 3:45PM in OM 119 Final Exam is: Fri., Dec. 17 th from 8AM – 10AM in OM 200
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.