Javatpoint Logo
Javatpoint Logo

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():


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


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:

Important MinHeap Methods:


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.


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


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).


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


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.


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

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA