Professional Documents
Culture Documents
10 Heaps
10 Heaps
10 Heaps
•Possible implementations:
• Linked Lists
• O(1) insertion time (insert at the front)
• O(N) deleteMin time
2
PRIORITY QUEUES
•Possible implementations:
• Linked Lists
• O(1) insertion time (insert at the front)
• O(N) deleteMin time
• Sorted Linked Lists
• O(N) insertion time (insert at the right place)
• O(1) deleteMin time (delete the first element)
3
PRIORITY QUEUES
•Possible implementations:
• Linked Lists
• O(1) insertion time (insert at the front)
• O(N) deleteMin time
• Sorted Linked Lists
• O(N) insertion time (insert at the right place)
• O(1) deleteMin time (delete the first element)
• Binary Search Trees/ AVL Tree
• O(log N) insertion time
• O(log N ) deleteMin time (asymmetry, tree
becomes right heavy!)
4
PRIORITY QUEUES
5
BINARY HEAPS
•Binary heaps are binary trees with two
properties:
• Structure Property
• Heap-order Property
6
BINARY HEAPS
•Structure Property
• A heap is a binary tree that is completely
filled with the possible exception of the
bottom level, which is filled from left to
right.
7
BINARY HEAPS
• Structure Property
• A heap is a binary tree that is completely filled with
the possible exception of the bottom level, which is
filled from left to right.
A
B C
A binary heap
D E F G
H I J
8
BINARY HEAPS
• Structure Property
• Such a tree is known as a complete binary tree
B C
D E F G
H I J
9
BINARY HEAPS
• Other examples of complete binary trees
B C
D E F G
H I
10
BINARY HEAPS
B C
D E F G
H I J K
11
BINARY HEAPS
• Examples of binary trees which are NOT COMPLETE
B C
D E F G
H I K
12
BINARY HEAPS
• Examples of binary trees which are NOT COMPLETE
B C
D E G
H I
13
BINARY HEAPS
• Examples of binary trees which are NOT COMPLETE
B C
D E F G
H J K
14
Perfect Binary Trees
• A perfect binary tree is a tree in which every node other
than the leaves has two children and all the leafs are
located at the bottom level.
A
A
B C
16
COMPLETE BINARY TREES
•Structure Property:
17
COMPLETE BINARY TREES
A
B C
D E F G
H I J
0 1 2 3 4 5 6 7 8 9 10 11
18
COMPLETE BINARY TREES
A
B C
D E F G
H I J
0 1 2 3 4 5 6 7 8 9 10 11
19
COMPLETE BINARY TREES
A
B C
D E F G
H I J
A B C
0 1 2 3 4 5 6 7 8 9 10 11
20
COMPLETE BINARY TREES
A
B C
D E F G
H I J
A B C D E
0 1 2 3 4 5 6 7 8 9 10 11
21
COMPLETE BINARY TREES
A
B C
D E F G
H I J
A B C D E F G
0 1 2 3 4 5 6 7 8 9 10 11
22
COMPLETE BINARY TREES
A
B C
D E F G
H I J
A B C D E F G H I
0 1 2 3 4 5 6 7 8 9 10 11
23
COMPLETE BINARY TREES
A
B C
D E F G
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11
24
COMPLETE BINARY TREES
A
B C
D E F G
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11
26
HEAP CLASS
template <class Comparable>
class BinaryHeap
{
public:
BinaryHeap( int capacity = 100 );
void buildHeap( );
void percolateDown( int hole );
}; 27
HEAP CLASS
template <class Comparable>
class BinaryHeap
{
public:
BinaryHeap( int capacity = 100 );
void buildHeap( );
void percolateDown( int hole );
}; 28
HEAP CLASS
template <class Comparable>
class BinaryHeap
{
public:
BinaryHeap( int capacity = 100 );
void buildHeap( );
void percolateDown( int hole );
}; 29
HEAP CLASS
template <class Comparable>
class BinaryHeap
{
public:
BinaryHeap( int capacity = 100 );
void buildHeap( );
void percolateDown( int hole );
}; 30
HEAP ORDER PROPERTY
31
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
32
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
33
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
34
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
35
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
36
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
37
HEAP ORDER PROPERTY
13
21 16
24 31 19 100
65 26 32
Note that siblings are not ordered in any
particular way!
38
HEAP ORDER PROPERTY
13
21 16
6 31 19 100
65 26 32
This complete tree does NOT have the
heap-order property
39
BASIC HEAP OPERATIONS
•INSERT X
• We create a hole in the next available
location (to maintain the structure
property)
• Place X in the hole
• If heap order property is violated keep on
interchanging X with its parent until
property is established
40
INSERTION
13
We want to insert 14
21 16
24 31 19 100
65 26 32
41
INSERTION
13
We want to insert 14
21 16
Create a hole and put 14 there
24 31 19 100
65 26 32 14
42
INSERTION
13
We want to insert 14
21 16
Create a hole and put 14 there
65 26 32 14
43
INSERTION
13
We want to insert 14
21 16
Create a hole and put 14 there
44
INSERTION
13
We want to insert 14
21 16
Create a hole and put 14 there
45
INSERTION
13
We want to insert 14
14 16
Create a hole and put 14 there
46
INSERTION
13
We want to insert 14
14 16
Create a hole and put 14 there
47
INSERTION
13
14 16
24 21 19 100
65 26 32 31
This strategy is called percolation or
bubble-up
48
insert
/**
* Insert item x into the priority queue, maintaining heap order.
* Duplicates are allowed.
* Throw Overflow if container is full.
*/
template <class Comparable>
void BinaryHeap<Comparable>::insert( const Comparable & x )
{
if ( isFull( ) )
throw Overflow( );
// Percolate up
// Note that instead of swapping we move the hole up
int hole = ++currentSize;
for ( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 )
array[ hole ] = array[ hole / 2 ];
array[ hole ] = x;
} 49
INSERTION
13
We want to insert 14
21 16
Create a hole
Heap property violated
24 31 19 100
65 26 32
14
0 1 2 3 4 5 6 7 8 9 10 11
13 21 16 24 31 19 100 65 26 32
Hole
50
INSERTION
13
21 16
24 19 100
65 26 32 31
14
0 1 2 3 4 5 6 7 8 9 10 11
13 21 16 24 19 100 65 26 32 31
Hole
51
INSERTION
13
16
24
2 19 100
1
65 26 32 31
14
0 1 2 3 4 5 6 7 8 9 10 11
13 16 24 21 19 100 65 26 32 31
Hole
52
INSERTION
13 We want to insert 14
14 16
24 21 19 100
65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11
13 14 16 24 21 19 100 65 26 32 31
Hole
53
insert
• If an element needs to be percolated up d
levels, the insert operation uses d+1
assignment operations
• A swap would have required 3 assignments per
swap, for a total 3d swaps for percolating d
levels.
55
DELETING THE MINIMUM
13 Remove 13 from the root
14 16
24 21 19 100
65 26 32 31
56
DELETING THE MINIMUM
Remove 13 from the root
14 16
24 21 19 100
65 26 32 31
57
DELETING THE MINIMUM
31
Remove 13 from the root
14 16
Put the last element to the root
and remove the last node
24 21 19 100
65 26 32
58
DELETING THE MINIMUM
14
Remove 13 from the root
31 16
Put the last element to the root
and remove the last node
24 21 19 100
Percolate 31 one level down
65 26 32
59
DELETING THE MINIMUM
14
Remove 13 from the root
21 16
Put the last element to the root
and remove the last node
24 31 19 100
Percolate 31 one level down
Percolate 31 one level down
65 26 32
60
DELETING THE MINIMUM
14
Remove 13 from the root
21 16
Put the last element to the root
and remove the last node
24 31 19 100
Percolate 31 one level down
Percolate 31 one level down
65 26 32
One can also think of this as the hole at root moving down!
61
deleteMin()
/**
* Remove the smallest item from the priority queue.
* Throw Underflow if empty.
*/
template <class Comparable>
void BinaryHeap<Comparable>::deleteMin( )
{
if ( isEmpty( ) )
throw Underflow( );
// move the last element to the first and shrink the array
array[ 1 ] = array[ currentSize–– ];
percolateDown( 1 );
}
62
deleteMin(...)
/**
* Remove the smallest item from the priority queue
* and place it in minItem. Throw Underflow if empty.
*/
template <class Comparable>
void BinaryHeap<Comparable>::deleteMin( Comparable & minItem )
{
if ( isEmpty( ) )
throw Underflow( );
minItem = array[ 1 ];
array[ 1 ] = array[ currentSize-- ];
percolateDown( 1 );
}
63
(private) percolateDown
/**
* Internal method to percolate down in the heap.
* hole is the index at which the percolate begins.
*/
template <class Comparable>
void BinaryHeap<Comparable>::percolateDown( int hole )
{
int child;
Comparable tmp = array[ hole ]; // tmp is the item that will
// be percolated down
for( ; hole * 2 <= currentSize; hole = child )
{
child = hole * 2;
if ( child != currentSize && array[ child + 1 ] < array[ child ] )
child++;
if ( array[ child ] < tmp )
array[ hole ] = array[ child ];
else
break;
}
array[ hole ] = tmp; 64
(private) percolateDown
/**
* Internal method to percolate down in the heap.
* hole is the index at which the percolate begins.
*/
template <class Comparable>
void BinaryHeap<Comparable>::percolateDown( int hole )
{
int child;
Comparable tmp = array[ hole ]; // tmp is the item that will
// be percolated down
for( ; hole * 2 <= currentSize; hole = child )
{
child = hole * 2;
if ( child != currentSize && array[ child + 1 ] < array[ child ] )
child++; // child is the minimum of the children
if ( array[ child ] < tmp )
array[ hole ] = array[ child ];
else
break;
}
array[ hole ] = tmp; 65
(private) percolateDown
/**
* Internal method to percolate down in the heap.
* hole is the index at which the percolate begins.
*/
template <class Comparable>
void BinaryHeap<Comparable>::percolateDown( int hole )
{
int child;
Comparable tmp = array[ hole ]; // tmp is the item that will
// be percolated down
for( ; hole * 2 <= currentSize; hole = child )
{
child = hole * 2;
if ( child != currentSize && array[ child + 1 ] < array[ child ] )
child++; // child is the minimum of the children
if ( array[ child ] < tmp )
array[ hole ] = array[ child ]; // swap hole and min child
else
break;
}
array[ hole ] = tmp; 66
(private) percolateDown
/**
* Internal method to percolate down in the heap.
* hole is the index at which the percolate begins.
*/
template <class Comparable>
void BinaryHeap<Comparable>::percolateDown( int hole )
{
int child;
Comparable tmp = array[ hole ]; // tmp is the item that will
// be percolated down
for( ; hole * 2 <= currentSize; hole = child )
{
child = hole * 2;
if ( child != currentSize && array[ child + 1 ] < array[ child ] )
child++; // child is the minimum of the children
if ( array[ child ] < tmp )
array[ hole ] = array[ child ]; // swap hole and min child
else
break;
}
array[ hole ] = tmp; 67
percolateDown
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11
68
percolateDown
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11
14 16 24 21 19 68 65 26 32 31
31 tmp = 31
0 1 2 3 4 5 6 7 8 9 10 11
69
percolateDown
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11
14 16 24 21 19 68 65 26 32 31
31 tmp = 31
0 1 2 3 4 5 6 7 8 9 10 11
14 14
31 16 24 21 19 68 65 26 32 31 tmp = 31
child = 2
0 1 2 3 4 5 6 7 8 9 10 11
70
percolateDown
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11
14 16 24 21 19 68 65 26 32 31
31 tmp = 31
0 1 2 3 4 5 6 7 8 9 10 11
14 14
31 16 24 21 19 68 65 26 32 31 tmp = 31
child = 2
0 1 2 3 4 5 6 7 8 9 10 11
14 21 16 24 31 19 68 65 26 32 31 tmp = 31
child = 5
0 1 2 3 4 5 6 7 8 9 10 11
71
percolateDown
14 16 24 19 68 65 26 32 31 tmp = 31
21 31
child = 10
0 1 2 3 4 5 6 7 8 9 10 11
72
PRIORITY QUEUES - HEAPS
73
OTHER HEAP OPERATIONS
• buildHeap
• take N items and place them into an empty
heap.
• obviously this can be done with N inserts
• O(1) average (experimental), O(log N) worst case
• Total O(N) average, O(N log N) worst case
• Can we guarantee a O(N) worst-case bound?
74
buildHeap
/**
* Establish heap order property from an arbitrary
* arrangement of items. Runs in linear time.
*/
template <class Comparable>
void BinaryHeap<Comparable>::buildHeap(
vector<Comparable> Input)
{
array = Input; // copy input array to private array;
currentSize = Input.size();
for ( int i = currentSize / 2; i > 0; i–– )
percolateDown( i );
}
75
buildHeap
150 80 40 30 10 70 110 100 20 90 60 50 120 140 130
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
76
buildHeap
150 80 40 30 10 70 110 100 20 90 60 50 120 140 130
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(7)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
77
buildHeap
150 80 40 30 10 70 110 100 20 90 60 50 120 140 130
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(7)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(6)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
78
buildHeap
After percolateDown(5)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
79
buildHeap
After percolateDown(5)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
80
buildHeap
After percolateDown(5)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(4)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(3)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
81
buildHeap
After percolateDown(2) 1st level
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
82
buildHeap
After percolateDown(2) 1st level
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
83
buildHeap
After percolateDown(1) 1st level
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
84
buildHeap
After percolateDown(1) 1st level
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
85
buildHeap
After percolateDown(1) 1st level
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
After percolateDown(1) 3rd level
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
86
COMPLEXITY OF buildHeap
87
COMPLEXITY OF buildHeap
88
COMPLEXITY OF buildHeap
89
COMPLEXITY OF buildHeap
90
COMPLEXITY OF buildHeap
91
COMPLEXITY OF buildHeap
Theorem: The sum of the heights of the nodes
in a perfect binary tree of height h is
2h+1 – 1 – (h + 1)
92
COMPLEXITY OF buildHeap
93
APPLICATIONS
• Heap Sort
94
HEAP SORT
•Heap Sort
• Input an array of N items
• buildHeap (takes O(N) time)
• perform N deleteMin operations (takes
O(N log N) time
• Total Sort time = O(N) + O(N log N)
= O(N log N)
95
SELECTION
96
SELECTION
•Algorithm 1
• Read the N elements into an array
• buildHeap (takes O(N) time)
• perform k deleteMins (takes O(k log N)
time)
• the last element obtained is our result
97
SELECTION
• Algorithm 1
• If k is small (k = O(N / log N)), the running time is
dominated by the buildHeap operation ⇒
O(N)
• For larger values of k, selection takes O(k log N)
time.
• k = ⎡N/2⎤ ⇒ Selection takes Θ(N log N) time
• Note that heap sort is a special case of this
algorithm.
• By redefining the heap-order property we can
also find the kth largest element.
98
SELECTION
•Algorithm 2 (but let us find the kth
largest element)
• We maintain a set of S of the k largest
elements.
• Let Sk be the smallest element in S.
• Look at a new element.
• If the new element is larger than Sk, then it
replaces it in S.
• S then will have a new smallest element.
• After looking at all the other elements Sk is the
answer
99
SELECTION
•Algorithm 2 (but let us find the kth
largest element)
• Take the first k elements and buildHeap
(takes O(k) time)
• For the remaining N-k elements
• O(1) to see if it has to replace Sk
• O(log k) to delete Sk and find the new minimum
if necessary.
• Total time is O(k + (N-k) log k) = O(N log k)
100