Professional Documents
Culture Documents
Extract-Min Insert
Priority Queue
Question
Is there a data structure that supports both of the priority queue
operations in O(log n) time each?
1 4
2 5 2 5
4 3 6 1 3 6
A min-heap Not a heap
Heap-order property:
The value of a node is at least the value of its parent — Min-heap
Structure properties
A heap of height h has between 2h to 2h+1 − 1 nodes. Thus,
an n-element heap has height Θ(log n).
The structure is so regular, it can be represented in an array
with no pointers (links) necessary !!!
B C 1 2 3 4 5 6 7 8 9 10
A B C D E F G H I J
D E F G
H I J
3 4
5 4 6 5
6 7 8
3 4
5 4 6 5
6 7 8 1 Insert 1
3 4
5 1 6 5
swap
Percolate up to maintain
6 7 8 4 the min-heap property
1 swap 4
5 3 6 5
Percolate up to maintain
6 7 8 4 the min-heap property
2 4
5 3 6 5
Percolate up to maintain
6 7 8 4 the min-heap property
3 4 3 4
4 5 6 5 4 5 6 5
6 7 8 6 7 8
3 3 3
4 4 4 4 4
4 5 6 5 5 6 5 6 5 6 5
6 7 8 6 7 8 7 8
3 4
4 5 6 5
6 7 8
3 4
4 5 6 5
8 4
4 5 6 5
4 4
swap
8 5 6 5
4 4
6 5 6 5
swap
Percolate down to maintain
8 7 the min-heap property
Question
How can heaps be modified to support Decrease-Key in O(log n)
time?