Heaps and Priority Queues Slides
Heaps and Priority Queues Slides
Robert Horvick
SOFTWARE ENGINEER
@bubbafat www.roberthorvick.com
Heap Overview
- Min and Max Heaps
Overview
Tree as Array
Heap Operations
- Push
- Pop
- Top
Priority Queue
Heap
A tree-based container type that provides O(1) access to
the minimum (min-heap) or maximum (max-heap) value
while satisfying the heap property.
Heap Property
The value in the current tree node is greater than, or
equal to, its children (max-heap).
Heaps
Max-heap Min-heap
8 1
5 6 3 2
4 2 1 3 8 4 5 6
Satisfies the heap property
8 4 5 6
Min-heap
1
Satisfies the heap property
8 4 5 6
1
Satisfies the heap property
8 4 5 6
1
Satisfies the heap property
8 3 5 6
8
Satisfies the heap property
4 2 1 3
Max-heap
8
Satisfies the heap property
4 2 1 3
Complete Tree
A tree where every level is filled out from left-to-right be
starting the next level.
8
Satisfies the heap property
Complete tree
8
Satisfies the heap property
Complete tree
8
Satisfies the heap property
Complete tree
8
Satisfies the heap property
4
Complete tree
8
Satisfies the heap property
4 2
Complete tree
8
Satisfies the heap property
4 2 1
Complete tree
8
Satisfies the heap property
4 2 1 3
Complete tree
8
Satisfies the heap property
Incomplete tree
8
Satisfies the heap property
4 1
Incomplete tree
8
Satisfies the heap property
4 2
Incomplete tree
Trees as Arrays
Complete binary trees can be compactly stored as arrays
eliminating all structural overhead and providing O(1)
data access.
Tree as Array
1 2
3 4 5 6
7 8 9 10
Tree as Array
1 2
3 4 5 6
7 8 9 10
0 1 2 3 4 5 6 7 8 9 10
Tree as Array
Index 0 1 2 3 4 5 6 7 8 9 10
Parent x 0 0 1 1 2 2 3 3 4 4
Left 1 3 5 7 9 11 13 15 17 19 21
Right 2 4 6 8 10 12 14 16 18 20 22
0 1 2 3 4 5 6 7 8 9 10
Tree as Array
Index 0 1 2 3 4 5 6 7 8 9 10
Parent x 0 0 1 1 2 2 3 3 4 4
Left 1 3 5 7 9 11 13 15 17 19 21
Right 2 4 6 8 10 12 14 16 18 20 22
0 1 2 3 4 5 6 7 8 9 10
Tree as Array
Index 0 1 2 3 4 5 6 7 8 9 10
Parent x 0 0 1 1 2 2 3 3 4 4
Left 1 3 5 7 9 11 13 15 17 19 21
Right 2 4 6 8 10 12 14 16 18 20 22
0 1 2 3 4 5 6 7 8 9 10
Tree as Array
Index 0 1 2 3 4 5 6 7 8 9 10
Parent x 0 0 1 1 2 2 3 3 4 4
Left 1 3 5 7 9 11 13 15 17 19 21
Right 2 4 6 8 10 12 14 16 18 20 22
0 1 2 3 4 5 6 7 8 9 10
Tree as Array
Index 0 1 2 3 4 5 6 7 8 9 10
Parent x 0 0 1 1 2 2 3 3 4 4
Left 1 3 5 7 9 11 13 15 17 19 21
Right 2 4 6 8 10 12 14 16 18 20 22
0 1 2 3 4 5 6 7 8 9 10
Heap Operations
Heap Operations
Add the new value to the end of the While the heap property is not
heap array satisfied, swap the new item with its
parent
8
8 5 6 4 2
8
8 5 6 4 2
8
8 5 6 4 2 9
8
8 5 6 4 2 9
8
8 5 6 4 2 9
8
8 5 6 4 2 9
8
8 5 9 4 2 6
8
8 5 9 4 2 6
8
8 5 9 4 2 6
9
8 5 9 4 2 6
9
9 5 8 4 2 6
9
9 5 8 4 2 6
9
9 5 8 4 2 6 3
Top
Returns the first item (min or max) in the heap.
Top
5 8
4 2 6
9 5 8 4 2 6
Top
5 8
4 2 6
9 5 8 4 2 6
public T Top() {
if (Count > 0) {
return data[0];
Top
Return the first item in the heap
Pop
Removes the top item from the heap, moving the
replacement item into the first valid position in the heap
tree.
Pop Operations
Move the last item in the heap array While the heap property is not
into the zero (root) index satisfied, swap the item with one of
its children
9
9 5 8 4 2 6
9
9 5 8 4 2 6
Replace top value with 5 8
right-most child
9 5 8 4 2 6
6
9 5 8 4 2 6
6
6 5 8 4 2
6
6 5 8 4 2
6
6 5 8 4 2
6
6 5 8 4 2
5
5 6 8 4 2
6
6 5 8 4 2
6
6 5 8 4 2
8
6 5 8 4 2
8
8 5 6 4 2
8
8 5 6 4 2
9
9 7 8 4 2 6
9
9 7 8 4 2 6
6
6 7 8 4 2
7
7 6 8 4 2
6
6 7 8 4 2
8
8 7 6 4 2
Priority Queue
A queue that pops item in priority, not FIFO, order.
Priority Queue
Job
Job 4
3
2
1
Priority Queue
Job 4
Priority Queue
Job 3 Job 4
Priority Queue
Job 3 Job 4
Priority Queue
T value = heap.Top();
heap.Pop();
return value;
}
Demo
Review heap code
Review priority queue
Example: job queue