Heaps
Heaps
Learning Outcome
Successful students should be able to:
Define a binary heap, and its properties
Construct and manipulate elements in a heap and explain time complexity of each
operation
Explain how the heap can be used in heapsort (with heapify) and priority queue
What is a Binary Heap ?
■ It is a nearly complete binary tree
■ It has heap properties :
– Every Parent node value is always smaller than its descendants (it
is called min heap) OR
– Every Parent node value is always higher than its descendants (It
is called Max heap)
■ Normally removing only happen on top of the heap (root)
■ Application of binary heap
– Heap sort, priority queue and etc
3
Complete Binary Trees
■ A complete binary tree is a binary tree in which every level, except possibly the
last, is completely filled, and all nodes are as far left as possible.
A A
B C B C
D E F G D E F
D E D E
D E F
F
5
Example of heap :
10
50
20 15
30 20
15 6 30 50 25
Max heap
• The root always stores the largest Min heap
value of the tree • The root always stores the smallest
value of the tree
Your Turn
MaxHeap::~MaxHeap(){
delete[] heapArr;
}
Binary Heap : Array presentation
Value 50 30 20 15 6 12 11
50 Position(p) 1 2 3 4 5 6 7 8 9 10
Index (i) 0 1 2 3 4 5 6 7 8 9
30 20
15 6 12 11 in position in index
If a Node p i
Its left child is 2*p 2*i+1
Its right child is 2*p+1 2*i+2
Its parent is floor (p /2 ) floor ( (i-1)/2)
9
Binary Heap : Implementation
int MaxHeap::parent (int i) {
return (i-1)/2;
}
50
50 30 20 15 6 12 11
30 20
0 1 2 3 4 5 6 7
15 6 12 11
Binary Heap (Max heap) : Insert - Step 1
Step 1 : After validation,
Insert 60 as the last elements of the array
50
30 20
15 6 12 11
60
50 30 20 15 6 12 11 60
0 1 2 3 4 5 6 7
Binary Heap (Max heap) : Insert – step 2
Step 2 : Compare the value with parent. Swap the value if its value is larger than the parent.
50
30 20
15 6 12 11
60
50 30 20 15
60 6 12 11 60
15
0 1 2 3 4 5 6 7
Binary Heap (Max heap) : Insert – step 3
Step 3 : Repeat Step 2 until its value is smallest or equal to parent value.
This moving upward process until the heap property is not violates is called reheapification upward or bubble
up
50 60
60 20 50 20
30 6 12 11 30 6 12 11
15 15
50 60 20 50 30 12 11 15 60 50 20 50 30 12 11 15
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
Binary Heap (Max heap) : Insert
void MaxHeap::insert(int k) {
if (size == capacity) {
cout << "\nFail to insert, capacity is full\n";
return;
}
■ 3,4,2,10,6
50 20
30 6 12 11
15
60 50 20 30 6 12 11 15
0 1 2 3 4 5 6 7
Binary Heap (Max heap) : Remove – Step 1
60 Step 1 : after validation, replace the root
with the last element in the heap. Reduce
the size of the heap
50 20
30 6 12 11
15
60
15 50 20 30 6 12 11 15
0 1 2 3 4 5 6 7
Binary Heap (Max heap) : Remove – Step 2
15 Step 2 :
• Compare the current value with left and
right child.
20 • Swap the value by moving downward if
50
the heap property is violated.
• This process called max-heapify (for
11
min heap – min heapify)
30 6 12
15
50 50
15 20 30 6 12 11
0 1 2 3 4 5 6 7
Binary Heap (Max heap) : Remove – Step 3
50 Step 3 :
• Repeat the same until no violation of
the heap property.
15 20 50
30 6 12 11 30 20
50 15 20 30 6 12 11
15 6 12 11
0 1 2 3 4 5 6 7
50 30 20 15 6 12 11
0 1 2 3 4 5 6 7
Binary Heap (Max heap) : remove
int MaxHeap::remove()
{
if (size <= 0)
return INT_MAX;
if (size == 1) {
size--;
return heapArr[0];
}
return root;
}
Binary Heap (Max heap) : remove
void MaxHeap::MaxHeapify(int i)
{
int leftIndex = left(i);
int rightIndex = right(i);
int largestIndex = i;
50 30 20 15 6 12 11
1 2 3 4 5 6 7 8
Heap sort
■ To perform heap sort
– First step – build heap
■ Build a heap based on the given array (eg: max heap for sorted in
ascending order)
– Second step -sorting
■ removing the element (from the root) one by one and keep the deleted root
at the end of the array.
■ Time complexity is O (n log n)
– First step O(n log n) - assume the building based on insertion
algorithm we discuss previously – moving upward
– Second step ->O (n log n)
Heapify
■ An optimized way of building heap based on array which able to
achieve O (n)
■ An element of highest priority always appears at the front of the queue. If that
element is removed, the next highest priority element advances to the front.
– For example priority queue of a shared printer