Lecture 9 - Heap
Lecture 9 - Heap
DATA
STRUCTURES
WHAT IS HEAP?
• A Heap (also called binary heap) is a special Tree-
based data structure in which the tree is a
complete binary tree.
– The tree is completely filled except possibly the
bottom level, which is filled from left to right.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of it’s children. The same property must be recursively true for all sub-
trees in that Binary Tree.
A[PARENT(i)] ≤ A[i]
– Store data in descending order
CLİCKER QUESTİON
• Is the following binary tree a maximum binary heap?
83 91 81 77 84 99 64
8 9 10 11 12
6 14 45 78 18 47 53 83 91 81 ……
1 2 3 4 5 6 7 8 9 10
BUİLT A BINARY HEAP
Example: Convert the following array to a max heap
2 5 4 8 9 10 11
5 4
9 10 11
8
Start from bottommost and
rightmost internal mode that is
the deepest node who has a
child and work toward the root
of the tree
EXERCİSE
• Convert the following array to a max heap
4 14 7 2 8 1
4 4
4
OK OK
14 7 14 7
14 7
2 8 1 2 8 1 2 8 1
14 14 heapfy 4
heapfy
14 7
8 7 4 7
2 4 1 2 8 1 2 8 1
BUİLD A HEAP
struct MinHeap* createAndBuildHeap(int *array, int size)
{
int i;
struct MinHeap* minxHeap =(struct MinHeap*) malloc(sizeof(struct MinHeap));
minxHeap->size = size;
minxHeap->array = array;
return minxHeap;
}
void min_heapify (int Arr[ ] , int i, int heapSize) {
int left = 2*i +1;
int right = 2*i+2;
int smallest=i;
FUNCTION
smallest = right;
if(smallest != i) {
swap (Arr[ i ], Arr[ smallest ]);
min_heapify (Arr, smallest, heapSize);
}
}
struct MinHeap* createAndBuildHeap(int *array, int size){
4 Size=6 int i;
struct MinHeap* minxHeap =(struct MinHeap*) malloc(sizeof(struct MinHeap));
minxHeap->size = size;
First iteration minxHeap->array = array;
14 1 2 1 if(smallest != i) {
swap (Arr[ i ], Arr[ smallest ]);
min_heapify (Arr, smallest, heapSize);
}
}
2 8 7 14 8 7
Third iteration struct MinHeap* createAndBuildHeap(int *array, int size){
int i;
4 struct MinHeap* minxHeap =(struct MinHeap*) malloc(sizeof(struct MinHeap));
minxHeap->size = size;
minxHeap->array = array;
return minxHeap;
}
14 8 7
void min_heapify (int Arr[ ] , int i, int heapSize) {
int left = 2*i +1;
int right = 2*i+2;
Recursice call 1 int smallest=i;
MAX
largest = left;
06
14 45
78 18 47 53
06
14 45
78 18 47 42
swap with parent
83 91 81 77 84 99 64 53
42
BINARY HEAP: INSERTION
14 42
78 18 47 45
83 91 81 77 84 99 64 53
EXERCİSE
• Consider the heap given below and insert 99 in it
54
45 36
27 21 18 21
11
54
54
45 36
45 36
27 21 18 21
27 21 18 21
11 99
54
11 54
99 36
45 36
45 21 18 21
99 21 18 21
11 27
11 27
99
54 36
45 21 18 21
11 27
OPERATIONS ON A BINARY HEAP
• Delete Max: In a max heap, the first item is always the maximum. So to delete
the maximum, we delete the first item. After the deletion of the first time, we
replace the vacant first position by the last time in the array. We might need to
shift this item down in order to keep the heap property intact.
06
14 42
78 18 47 45
83 91 81 77 84 99 64 53
BINARY HEAP: DELETE MIN
• Delete minimum element from heap.
– Exchange root with rightmost leaf.
– Bubble root down until it's heap ordered.
53
14 42
78 18 47 45
83 91 81 77 84 99 64 06
BINARY HEAP: DELETE MIN
14 42
78 18 47 45
83 91 81 77 84 99 64
BINARY HEAP: DELETE MIN
53 42
78 18 47 45
83 91 81 77 84 99 64
BINARY HEAP: DELETE MIN
18 42
78 53 47 45
83 91 81 77 84 99 64
APPLICATION OF HEAP
6
10
5 1 9
• https://www.cs.usfca.edu/
~galles/visualization/Heap
Sort.html