Last Updated: 22 Jun, 2022

Min Heap Implementation

Moderate

Problem statement

Implement the Min Heap data structure.

Min heap is a tree data structure where the root element is the smallest of all the elements in the heap. Also, the children of every node are smaller than or equal to the root node. 

The insertion and removal of elements from the heap take log('N'), where 'N' is the number of nodes in the tree. 

Implement the “minHeap” class. You will be given the following types of queries:-

0 extractMinElement(): Remove the minimum element present in the heap, and return it.

1 deleteElement( i ): Delete the element present at the 'i' th index.

2 insert( key ): Insert the value 'key' in the heap.

For queries of types 0 and 1, at least one element should be in the heap.
Input Format :
The first line will contain a single integer 'N', the number of queries.

Then each of the following ‘N’ lines contains queries of the following type:-
0 
1 X
2 X

Where 'X' is some value and 0, 1, and 2 are the respective values as shown in the queries above.
Output format :
Print the output of each query if there is some output.
Note :
The input will contain at least one operation of type 1.

You don't need to print anything. It has already been taken care of. Just implement the given function.

Approaches

01 Approach

The heap property is satisfied by the heap data structure. And follows the properties of a complete binary tree. Heap property is as follows, which we have used all over the approach:-

Each parent element is smaller than or equal to its child elements.

As we all know, the complete binary tree is a tree with every level filled and all nodes as far left as feasible. The last level of the binary tree may be empty and unfilled. 

 You're probably wondering what the heap property is.

Every node of the tree is given a key value or weight in the heap data structure.

Now, the key value of the root node is compared with the values of children's nodes, and then the tree is arranged into two categories, respectively i.e., max Heap or min-heap. This data structure is used as a sorting algorithm to sort the elements in a list and an array. This sorting algorithm can be used in data structures like order statistics, priority queues, Dijkstra's algorithm, or Prim's algo. Heapify refers to creating a heap data structure using a binary tree. To create the Min-Heap, the heapify process is used. For a complete binary tree, the left child in an array is 2*i+1 and the right child is 2*i+2 if the current node index is ‘i’ in the array. For the index ‘i’ the parent of it is (i-1)/2.

Now to create min-heap following steps are used:-

 

left ( k )

  1. Return 2*k+1.

 

right ( k )

  1. Return 2*k+2.
     

parent ( k )

  1. Return ( k-1 ) / 2.
     

heapify ( k)

  1. Find the left child of the node.
  2. Find the right child of the node.
  3. Find the smallest element between the current node and its children.
  4. Check if the left child is the smallest.
  5. Check if the right node is smallest than the previous smallest.
  6. If the smallest element is not in the current node.
  7. We have to heapify the Heap to take that element to the top.
    1. Swap the values of the current node and the smallest node value.
    2. Call the heapify function on the smallest value node which now contains the value of the parent node.

 

insert( Val )

  1. Insert a val in the heap.
  2. The function contains heap array, val to inserted.
  3. Insert the val at the end of the heap.
  4. If there is more than 1 node in the Heap.
  5. MinHeapify the heap by checking the val at its parent node.
  6. Also, do it until the heap property is not satisfied.
  7. while( i != 0 and heap[parent(i)] > heap[i]):
    1. Swap the value of the current node with its parent.
    2. Check the same if the parent element of the current element is satisfying the heap property.
    3. i = parent(i)

 

removeMin()

  1. Check if the current node is the only node in the heap.
    1. Reduce the size of the heap and return.
  2. Take out the min value and remove it from the heap.
  3. Put the last node on the top of the heap. So that we can heapify from the root node till the last children.
  4. Decrease the size of the heap as the minimum element is removed.
  5. Heapify the heap to satisfy the heap property.

minElement()

  1. Return the topmost element from the heap.