0% found this document useful (0 votes)
10 views

Lecture 9 - Heap

asd

Uploaded by

toprakcihalil0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 9 - Heap

asd

Uploaded by

toprakcihalil0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

COME 205

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.

• Binary heap does not use the binary-search


differentiation between the left and right sub-trees
to create a linear ordering. Instead, a binary heap
only specifies the relationship between a parent and
its children.
• Heap order
OTHER TYPES OF HEAP
– Numerous other heaps exists:
• d-ary heaps
• Leftist heaps
• Skew heaps
• Binomial heaps
• Fibonacci heaps
• Bi-parental heaps
MAIN TYPES OF BINARY HEAP
Generally, Heaps can be of two types:
Max-Heap: In a Max-Heap the key present at the root node must be greatest 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 ascending order

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?

A: It is a maximum binary heap


B: It is not a maximum binary heap
C: It is a minimum binary heap
D: I don’t care
E: I don’t listen you, what is going on?
ARRAY IMPLEMENTATION OF BINARY
HEAPS
• The root element will be at Arr[0].
• Below table shows indexes of other nodes for
the ith node, i.e., Arr[i]:
– Arr[(i-1)/2] Returns the parent node
– Arr[(2*i)+1] Returns the left child
node
– Arr[(2*i)+2] Returns the right child
node struct MinHeap
{
• The traversal method use to achieve Array int size;
representation is Level Order int* array;
};
6 i=2
1
14 45
2 3
• Parent(i) = i/2 6 at i=1
• Left(i) = 2i 78 at i=4
78 18 47 53
4 5 6 7 • Right(i) = 2i + 1 18 at i=5

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

Picture the array as a complete binary tree:

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;

for (i = (minxHeap->size - 2) / 2; i >= 0; --i)


minHeapify(minxHeap, i);

return minxHeap;
}
void min_heapify (int Arr[ ] , int i, int heapSize) {
int left = 2*i +1;
int right = 2*i+2;
int smallest=i;

if(left <= heapSize && Arr[left] < Arr[ i ] )


MIN smallest = left;

HEAPIFY if(right <= heapSize && Arr[right] < Arr[smallest] )

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 7 Second iteration for (i = (minxHeap->size - 2) / 2; i >= 0; --i)


minHeapify(minxHeap, i);

Third iteration return minxHeap;


}
2 8 1
void min_heapify (int Arr[ ] , int i, int heapSize) {
int left = 2*i +1;
int right = 2*i+2;
First iteration Second iteration int smallest=i;

if(left <= heapSize && Arr[left] < Arr[ i ] )


4 4 smallest = left;

if(right <= heapSize && Arr[right] < Arr[smallest] )


smallest = right;

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;

for (i = (minxHeap->size - 2) / 2; i >= 0; --i)


2 1 minHeapify(minxHeap, i);

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;

if(left <= heapSize && Arr[left] < Arr[ i ] )


1 1 smallest = left;
OK
if(right <= heapSize && Arr[right] < Arr[smallest] )
smallest = right;
2 4 2 4 if(smallest != i) {
swap (Arr[ i ], Arr[ smallest ]);
min_heapify (Arr, smallest, heapSize);
}
14 8 7 14 8 7 }
void max_heapify (int Arr[ ], int i, int heapSize) {
int left = 2*i +1 //left child
int right = 2*i +2 //right child
int largest = i;

if(left<= heapSize && Arr[left] > Arr[i] )

MAX
largest = left;

HEAPIFY if(right <= heapSize && Arr[right] > Arr[largest])


largest = right;
FUNCTION if(largest != i ) {
swap (Ar[i] , Arr[largest]);
max_heapify (Arr, largest, heapSize);
}
}
OPERATIONS ON A BINARY HEAP
• Insertion: The new item is initially inserted at the end of the heap (at the last
position of the array).
– If it satisfies the heap property then we are done.
– Otherwise, we shift it up (heapify) in the tree as long as it violates the heap
property

1. Insert the new item at the end of the heap.


2. Compare the newly inserted item with its parent (heapify).
If the parent is larger, stop.
If the parent is smaller, swap the item with its parent.
3. Repeat step 2 until the parent is larger or equal to the item.
BINARY HEAP: INSERTION

06

14 45

78 18 47 53

83 91 81 77 84 99 64 42 next free slot


BINARY HEAP: INSERTION

06

14 45

78 18 47 42
swap with parent

83 91 81 77 84 99 64 53
42
BINARY HEAP: INSERTION

06 stop: heap ordered

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.

1. Store the first time in the tree in some temporary variable.


2. Replace the first node with the item in the last node.
3. Check the first node with its children nodes.
If the left child is larger, we swap it with the left child.
If the right node is larger, we swap it with the right node.
4. Repeat step 3 until the parent node is larger than the left and right child
node.
5. Return the maximum value (stored in the temporary variable).
BINARY HEAP: DELETE MIN

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

53 exchange with left child

14 42

78 18 47 45

83 91 81 77 84 99 64
BINARY HEAP: DELETE MIN

14 exchange with right child

53 42

78 18 47 45

83 91 81 77 84 99 64
BINARY HEAP: DELETE MIN

14 stop: heap ordered

18 42

78 53 47 45

83 91 81 77 84 99 64
APPLICATION OF HEAP

• Operating Systems- Jobs / Process scheduling


• Heap Sorting
• Graph Application
• Priority Queue
HEAP SORT ALGORITHM
• In this sorting algorithm, we use
– Max Heap to arrange list of elements in Ascending order
– Min Heap to arrange list elements in Descending order.

• For example, Heap Sort Algorithm for sorting in increasing order:


– Build a max heap from the input data.
– At this point, the largest item is stored at the root of the heap. Replace it with the
last item of the heap followed by reducing the size of heap by 1. Finally, heapify the
root of tree.
– Repeat above steps while size of heap is greater than 1.
EXAMPLE OF HEAP SORTING
EXERCİSE
12

6
10

5 1 9

This is the max heap. Create the ascending order list


https://www.youtube.com/watch?v=2DmK_H7IdTo
VISUALIZATION OF HEAP SORT

• https://www.cs.usfca.edu/
~galles/visualization/Heap
Sort.html

You might also like