Problem of the day
Implement the Min Heap data structure.
You will be given 2 types of queries:-
0 X
Insert X in the heap.
1
Print the minimum element from the heap and remove it.
The first line will contain the integer 'T', denoting the number of test cases.
For each test case, the first line will contain a single integer 'N', the number of queries.
Then, each of the next ‘N’ lines contains two types of query either 0 ‘X’ or 1.
Output format :
For each test case, output the answer for query of type 1.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^5
1 <= X <= 50
Time Limit: 1 sec
2
3
0 2
0 1
1
2
0 1
1
1
1
For the first test case:-
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 and remove the smallest element which is 1.
For the second test case:-
Insert 1 in the heap and currently, 1 is the smallest element in the heap.
Return the smallest element from the heap which is 1 and remove it.
2
5
0 5
1
0 43
0 15
0 5
2
0 4
1
5
4
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 its child elements.
The complete binary tree, as we all know, is a tree with every level filled and all nodes as far left as feasible. It's possible that the last level of the binary tree is 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 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 ( heap, k)
insert( heap, val )
extractMin( heap )
minHeap ( N, Q )
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 at 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 ).