How To Implement Min Heap In C++

When we need a data structure that can handle insertion, deletion , and finding the minimum in O(Logn) , Min Heap comes in handy. In this article, we will discover how Min Heap is implemented in C++. A complete binary tree that is either a Min Heap or a Max Heap is referred to as a Binary Heap . The root key of a Max Binary Heap must be the largest key among all other keys in the heap. For every node in a binary tree , this property needs to be recursively true. Similar to Min Heap is Min Binary Heap

Algorithm of MIN heap:

For min_heap:

Example

Begin
 Declare function min_heap(int *a, int m, int n)
 Declare j, t of the integer datatype.
 Initialize t = a[m].
 j = 2 * m;
 while (j <= n) do
 if (j < n && a[j+1] < a[j]) then
 j = j + 1
 if (t < a[j]) then
 break
 else if (t >= a[j]) then
 a[j / 2] = a[j]
 j = 2 * j
 a[j/2] = t
 return
End.

For build_minheap:
Begin
 Declare function build_minheap(int *a,int n).
 Declare k of the integer datatype.
 for(k = n/2; k >= 1; k--)
 Call function min_heap(a,k,n)
End.

Example:

Let's take an example to implement Min Heap in C++:

Example

#include <iostream>
#include <conio.h>
using namespace std;

void min_heapify(int *arr, int n, int i) {
 int smallest = i;
 int left_child = 2 * i + 1;
 int right_child = 2 * i + 2;

 if (left_child < n && arr[left_child] < arr[smallest])
 smallest = left_child;

 if (right_child < n && arr[right_child] < arr[smallest])
 smallest = right_child;

 if (smallest != i) {
 swap(arr[i], arr[smallest]);
 min_heapify(arr, n, smallest);
 }
}

void build_min_heap(int *arr, int n) {
 for (int i = n / 2 - 1; i >= 0; i--) {
 min_heapify(arr, n, i);
 }
}

int main() {
 int n;
 cout << "Enter the number of elements in the array: ";
 cin >> n;
 
 int arr[100];
 
 cout << "Enter " << n << " elements: ";
 for (int i = 0; i < n; i++) {
 cin >> arr[i];
 }

 build_min_heap(arr, n);

 cout << "Min Heap:\n";
 for (int i = 0; i < n; i++) {
 cout << arr[i] << " ";
 }

 getch();
 return 0;
}

Output:

Output

Enter the number of elements in the array: 10
Enter 10 elements: 25 10 30 5 15 40 20 35 45 50
Min Heap:
5 10 20 25 15 40 30 35 45 50

C++ Representation of Min Heap

MinHeap is kept in an array. The root element is Arr[0].

For a node at index i:

Example

(i-1)/2 is the index of the parent node.
(2*i + 1) is the index of the left child.
(2*i + 2) is the index of the right child.

Important MinHeap Methods:

insert:

At the very end of the heap array, a new element is inserted . After that, the heap property is reinstated by switching the current element for its parent until the parent's value exceeds the current value. Insertion into the MinHeap requires an O(Logn) amount of time.

getMin:

It provides the root element's (minimum element's) value. getMin in MinHeap has a temporal complexity of O(1) .

extractMin:

The root element of MinHeap is deleted and returned by this procedure. The last element of the heap array is swapped for the root element before the MinHeap is subjected to the Heapify procedure. ExtractMin has a time complexity of O(Logn) .

decreaseKey:

The key at index i loses value while using this technique. This approach has O(Logn) time complexity.

delete:

The key at index i is removed using this procedure. MinHeap's deletion operation has an O(Logn) time complexity.

Features of a Binary Heap

These are Binary Heap's characteristics:

It is an entire tree. In other words, all floors are entirely occupied. Even if the final level may not be entirely completed, all of its components are on the left. This characteristic allows the binary heap to be stored in a 1D array.

There are two types of binary heaps: Min Heap and Max Heap. The root element's value is minimum or maximum in the Min/Max Heap.

Conclusion:

The C++ implementation of the Min Heap was covered in this article. A few of Min Heap's most crucial techniques were also examined

Input Required

This code uses input(). Please provide values below: