0% found this document useful (0 votes)
18 views50 pages

Heaps

The document defines a binary heap, its properties as a nearly complete binary tree where parent nodes are always greater than children in a max heap or less than children in a min heap. It describes how to implement a binary heap using an array and the operations to insert, remove the maximum/minimum element, and their time complexities.

Uploaded by

jayden goh1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views50 pages

Heaps

The document defines a binary heap, its properties as a nearly complete binary tree where parent nodes are always greater than children in a max heap or less than children in a min heap. It describes how to implement a binary heap using an array and the operations to insert, remove the maximum/minimum element, and their time complexities.

Uploaded by

jayden goh1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

(Binary) Heap

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

Height of a complete binary tree is log n


Which of the following is a complete binary tree ?

Tree 1 Tree 2 Tree 3


A
A
A
B C
B C
B C

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

■ Heap Exercise – Question 1


Binary Heap : Implementation
■ To represent a binary tree, one implementation is to make a dynamic
allocation for each node, with 2 pointers pointing to its children.
■ However, there is more elegant way of implement the heap with array
– In fact, many STL using array to implement it.

int *heapArr; // pointer to array of elements in heap


int capacity; // maximum possible size of max heap (array capacity)
int size; // Current number of elements in max heap
MaxHeap::MaxHeap(int cap) {
heapArr = new int[cap];
size = 0;
capacity = cap;
}

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;
}

int MaxHeap::left(int i) { void MaxHeap ::displayArr(){


return (2*i + 1); } for (int i=0;i<size ; i++)
cout << heapArr[i]<<endl;
int MaxHeap::right(int i) { }
return (2*i + 2);
}
int MaxHeap::getMax(){
if (size > 0)
return heapArr[0];
else{
cout << "\nFail to insert, capacity is full\n";
return INT_MAX;
}
}
Binary Heap (Max heap) : Insert
Insert 60

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;
}

// Insert the new value at the end of the heap


size++;
int index = size - 1;
heapArr[index] = k;

// shift up if breaking the heap property


while (index != 0 && heapArr[index] > heapArr[parent(index)] ) {
swap(&heapArr[index], &heapArr[parent(index)]);
index = parent(index);
} void MaxHeap::swap(int *x, int *y) {
} int temp = *x;
*x = *y;
*y = temp;
}
Your turn : Construct a max heap (show tree structure)

■ 3,4,2,10,6

More Heap exercises Question 2


Binary Heap (Max heap) : Remove
60 Normally, heap only allow to remove root node.

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];
}

int root = heapArr[0]; //store the maximum value


heapArr[0] = heapArr[size-1]; //move the last value to root
size --; //reduce the heap size

MaxHeapify(0); //perform heapify

return root;
}
Binary Heap (Max heap) : remove
void MaxHeap::MaxHeapify(int i)
{
int leftIndex = left(i);
int rightIndex = right(i);
int largestIndex = i;

if (leftIndex < size && heapArr[leftIndex] > heapArr[largestIndex])


largestIndex = leftIndex;
if (rightIndex < size && heapArr[rightIndex] > heapArr[largestIndex])
largestIndex =rightIndex ;
if (largestIndex != i)
{
swap(&heapArr[i], &heapArr[largestIndex]);
MaxHeapify(largestIndex);
}
}
Your turn
■ Remove the max value of the max heap constructed in slides 16. Show your working
steps.

More Heap exercises Question 3


Time complexity
■ Insertion is O(log n)
■ Remove is O(log n)
Backup
More about removing
■ Recall on removing from heap
– Every deletion give you a max or min value (depending the max or min heap)
– For example in max heap :
First time remove -> largest value
2nd time remove -> 2nd largest value
3rd time remove -> 3rd largest value and so on…..

■ From previous example, after removing 60, heap size = 7.


■ By placing the removed item at the empty space at the end , then, a sorted list can
be form

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)

■ Is a process of creating heap by inserting and rearranging the


elements downward
Heapsort
step1 : build a binary heap
(using heapify method)
Heapsort
step2 :
Sorting
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:
Example 2:

Finish Step 1 – building the heap. Need to continue to Step 2 –


removing/sorting
Priority Queue

■ 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

■ Heap is a good data structure to implement priority Queue


– Use min heap if your have : smallest number represent highest priority
– Use max heap if you have : largest number represent highest priority
■ Time complexity O (log n) – insertion O(logn) + deletion (O logn)
Backup
C++ STL

■ make heap  eg: convert a vector into a heap


http://www.cplusplus.com/reference/algorithm/make_heap/

You might also like