Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap. Based on this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its children.

Max-Heap − Where the value of the root node is greater than or equal to either of its children.

Both trees are constructed using the same input and order of arrival.
Max Heap Construction Algorithm
We shall use the same example to demonstrate how a Max Heap is created. The procedure to create Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At any point of time, heap must maintain its property. While insertion, we also assume that we are inserting a node in an already heapified tree.
Step 1 − Create a new node at the end of heap. Step 2 − Assign new value to the node. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. Step 5 − Repeat step 3 & 4 until Heap property holds.
Note − In Min Heap construction algorithm, we expect the value of the parent node to be less than that of the child node.
Let’s understand Max Heap construction by an animated illustration. We consider the same input sample that we used earlier.

Example
Following are the implementations of this operation in various programming languages −
//C code for Max Heap construction Algorithm#include <stdio.h>#include <stdlib.h>// Structure to represent a heaptypedefstruct{int* array;// Array to store heap elementsint capacity;// Maximum capacity of the heapint size;// Current size of the heap} Heap;// Function to create a new heap
Heap*createHeap(int capacity){
Heap* heap =(Heap*)malloc(sizeof(Heap));
heap->array =(int*)malloc(capacity *sizeof(int));
heap->capacity = capacity;
heap->size =0;return heap;}// Function to swap two elements in the heapvoidswap(int* a,int* b){int temp =*a;*a =*b;*b = temp;}// Function to heapify a subtree rooted at index ivoidheapify(Heap* heap,int i){int largest = i;int left =2* i +1;int right =2* i +2;// Check if the left child is larger than the rootif(left < heap->size && heap->array[left]> heap->array[largest])
largest = left;// Check if the right child is larger than the largest so farif(right < heap->size && heap->array[right]> heap->array[largest])
largest = right;// If the largest is not the root, swap the root with the largestif(largest != i){swap(&heap->array[i],&heap->array[largest]);heapify(heap, largest);}}// Function to insert a new element into the heapvoidinsert(Heap* heap,int value){if(heap->size == heap->capacity){printf("Heap is full. Cannot insert more elements.\n");return;}// Insert the new element at the endint i = heap->size++;
heap->array[i]= value;// Fix the heap property if it is violatedwhile(i !=0&& heap->array[(i -1)/2]< heap->array[i]){swap(&heap->array[i],&heap->array[(i -1)/2]);
i =(i -1)/2;}}// Function to extract the maximum element from the heapintextractMax(Heap* heap){if(heap->size ==0){printf("Heap is empty. Cannot extract maximum element.\n");return-1;}// Store the root elementint max = heap->array[0];// Replace the root with the last element
heap->array[0]= heap->array[heap->size -1];
heap->size--;// Heapify the rootheapify(heap,0);return max;}// Function to print the elements of the heapvoidprintHeap(Heap* heap){printf("Heap elements: ");for(int i =0; i < heap->size; i++){printf("%d ", heap->array[i]);}printf("\n");}// Example usage of the heapintmain(){
Heap* heap =createHeap(10);insert(heap,35);insert(heap,33);insert(heap,42);insert(heap,10);insert(heap,14);insert(heap,19);insert(heap,27);insert(heap,44);insert(heap,26);insert(heap,31);printHeap(heap);int max =extractMax(heap);printf("Maximum element: %d\n", max);return0;}
Output
Heap elements: 44 42 35 33 31 19 27 10 26 14 Maximum element: 44
Max Heap Deletion Algorithm
Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap always happens at the root to remove the Maximum (or minimum) value.
Step 1 − Remove root node. Step 2 − Move the last element of last level to root. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. Step 5 − Repeat step 3 & 4 until Heap property holds.

Example
Following are the implementations of this operation in various programming languages −
//C code for Max Heap Deletion Algorithm#include <stdio.h>#include <stdlib.h>// Structure to represent a heaptypedefstruct{int* array;// Array to store heap elementsint capacity;// Maximum capacity of the heapint size;// Current size of the heap} Heap;// create a new heap
Heap*createHeap(int capacity){
Heap* heap =(Heap*)malloc(sizeof(Heap));
heap->array =(int*)malloc(capacity *sizeof(int));
heap->capacity = capacity;
heap->size =0;return heap;}// swap two elements in the heapvoidswap(int* a,int* b){int temp =*a;*a =*b;*b = temp;}// Heapify a subtree rooted at index ivoidheapify(Heap* heap,int i){int largest = i;int left =2* i +1;int right =2* i +2;// Check if the left child is larger than the rootif(left < heap->size && heap->array[left]> heap->array[largest])
largest = left;// Check if the right child is larger than the largest so farif(right < heap->size && heap->array[right]> heap->array[largest])
largest = right;// If the largest is not the root, swap the root with the largestif(largest != i){swap(&heap->array[i],&heap->array[largest]);heapify(heap, largest);}}// Function to insert a new element into the heapvoidinsert(Heap* heap,int value){if(heap->size == heap->capacity){printf("Heap is full. Cannot insert more elements.\n");return;}// Insert the new element at the endint i = heap->size++;
heap->array[i]= value;// Fix the heap property if it is violatedwhile(i !=0&& heap->array[(i -1)/2]< heap->array[i]){swap(&heap->array[i],&heap->array[(i -1)/2]);
i =(i -1)/2;}}// delete the maximum element from the heapintdeleteMax(Heap* heap){if(heap->size ==0){printf("Heap is empty. Cannot extract maximum element.\n");return-1;}// Store the root elementint max = heap->array[0];// Replace the root with the last element
heap->array[0]= heap->array[heap->size -1];
heap->size--;// Heapify the rootheapify(heap,0);return max;}// print the elements of the heapvoidprintHeap(Heap* heap){for(int i =0; i < heap->size; i++){printf("%d ", heap->array[i]);}printf("\n");}// Deallocate memory occupied by the heapvoiddestroyHeap(Heap* heap){free(heap->array);free(heap);}// Example usage of the heapintmain(){
Heap* heap =createHeap(10);insert(heap,35);insert(heap,33);insert(heap,42);insert(heap,10);insert(heap,14);insert(heap,19);insert(heap,27);insert(heap,44);insert(heap,26);insert(heap,31);printf("Heap elements before deletion: ");printHeap(heap);// Deleting the maximum element in the heapint max =deleteMax(heap);printf("Maximum element: %d\n", max);printf("Heap elements after deletion: ");printHeap(heap);destroyHeap(heap);return0;}
Output
Heap elements before deletion: 44 42 35 33 31 19 27 10 26 14 Maximum element: 44 Heap elements after deletion: 42 33 35 26 31 19 27 10 14
Leave a Reply