Problem of the Day
-
Upload
merritt-baldwin -
Category
Documents
-
view
15 -
download
2
description
Transcript of 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 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!
Priority Queue ADT
Prioritizes Entrys using their keys For Entrys 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()
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
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
9 3
99
0 1 2 3
2
0
1
9
2
3
3
99
`
Add nodes to end of the SequenceSimilarly, remove node at end
NO space is wasted for this!
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 Entrys means
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 Entrys means 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 Entrys 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)
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 Entrys 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)
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 Entrys 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)
Swapping Data in a Heap
Two ways to swap Entrys in a heap Preserving nodes & swapping Entrys
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?
Swapping Data in a Heap
Two ways to swap Entrys in a heap Preserving nodes & swapping Entrys
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 (NOT Alec)
choose?
Keys In a PriorityQueue
Must order keys used within PriorityQueue What types could be used to achieve this
ordering?