Problem of the day
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.
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.
3
2 2
2 1
0
1
Insert 2 in the heap, and currently, 2 is the smallest element in the heap.
Insert 1 in the heap and now the smallest element is 1.
Return the smallest element, which is 1.
5
2 5
2 43
2 15
1 2
0
15
Insert 5 in the heap.
Insert 43 in the heap.
Insert 15 in the heap.
Remove element at index 2 i.e. X from the heap.
Return the smallest element, which is 15.
1 <= N <= 10^5
1 <= X <= 50
Time Limit: 1 sec
Complete Binary Tree with the top element as the smallest element.
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 )
right ( k )
parent ( k )
heapify ( k)
insert( Val )
removeMin()
minElement()
O( N * log( N )), Where ‘N’ is the number of queries.
We are iterating over each query which is ‘N’ and for each insert or remove we do heapify operation where we each time go to child or parent which is at its double or half of the current position respectively and max, we will go to the height of the tree which is log ( N ).
Hence the time complexity will be O( N * log( N )).
O( N ), Where ‘N’ is the number of queries.
We are creating a heap array of size ‘N’.
Hence the space complexity will be O( N ).