Problem of the Day To get into club, must get past bouncer Recorded correct responses of those...
-
Upload
emory-harvey -
Category
Documents
-
view
217 -
download
0
Transcript of Problem of the Day To get into club, must get past bouncer Recorded correct responses of those...
Problem of the Day
To get into club, must get past bouncer Recorded correct responses of those
before you Guard said five & response was four Guard said three & response was five Guard said twelve & response was six Guard said six & response was three
Guard now says to you ten, what do you say?
Problem of the Day
To get into club, must get past bouncer Recorded correct responses of those before
you Guard said five & response was four Guard said three & response was five Guard said twelve & response was six Guard said six & response was three
Guard now says to you ten, what do you say?
Three!
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()
Location are imaginary – only smallest matters
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 Structure must form a complete binary tree
2
95
67
Legal
CompleteBinaryTree
ADT which extends BinaryTree Add & remove methods defined plus those
inherited For this ADT, trees must maintain
specific shape Fill lowest level first, then can start new
level below it
2
95
67
Illegal
2
95
7
6
CompleteBinaryTree
ADT which extends BinaryTree Add & remove methods defined plus those
inherited For this ADT, 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
2
95
67
Illegal
2
95
67
What Is Purpose of a Heap?
Root has critical Entry that we always access Entry at root always has smallest priority in
Heap O(1) access time in min() without any real
effort CompleteBinaryTree makes insert()
easy Create leftmost child on lowest level When a level completes, start next one
Useful when:
Upheap
Insertion may violate heap-order property Upheap immediately after adding new Entry
Goes from new node to restore heap’s order Compare priority of node & its parent If out of order, swap node's Entrys Continue upheaping from parent node
Stop only when either case occurs: Found properly ordered node & parent Binary tree's root is reached
Removing From a Heap
removeMin() must kill Entry at heap’s root For a complete tree, must remove last node
added How to reconcile these two different
demands? Removed node's Entry moved to the
root Then remove node from the complete tree Heap's order preserved by going down
Removing From a Heap
removeMin() must kill Entry at heap’s root For a complete tree, must remove last node
added How to reconcile these two different
demands? Removed node's Entry moved to the
root Then remove node from the complete tree Heap's order preserved by going down
Censored
Downheap
Restores heap’s order during removeMin() Downheap work starts at root Swap with smallest child, if at least out-of-
order Downheaping continues with old smallest
child Stop at leaf or when node is legal
Implementation Excuses
upheap & downheap travel height of tree O(log n) running time for each of these Serves as bound for adding & removing
from PQ What drawbacks does heap have?
PriorityQueue can be faster using Sequence
Only for specific mix of operations, however
PriorityQueue using heaps are fastest overall
What Is Purpose of a Heap?
Root has critical Entry that we always access Entry at root always has smallest priority in
Heap O(1) access time in min() without any real
effort CompleteBinaryTree makes insert()
easy Create leftmost child on lowest level When a level completes, start next one
Useful when: Will eventually add & remove everything Equally (large) numbers of adds & removes