1
+ // Min heap implementation.
2
+ // This question is submitted on Hackerrank to "QHEAP1". So, it is according to it.
3
+ #include < cmath>
4
+ #include < cstdio>
5
+ #include < vector> // Not used in this.
6
+ #include < iostream>
7
+ #include < algorithm> // Used for heap STL. Not used in this.
8
+ #include < climits> // It is used to get maximum and minimum limits of different data types.
9
+ using namespace std ;
10
+
11
+ void swap (int *x, int *y){ // Swap function to swap the elements of array.
12
+ int temp=*x;
13
+ *x=*y;
14
+ *y=temp;
15
+ }
16
+
17
+ class MinHeap // Class MINHEAP
18
+ {
19
+ int size;
20
+ int capacity;
21
+ int *harr; // Pointer to array to be made into Min heap.
22
+ public:
23
+ MinHeap (int cap); // Constrcutor which takes capacity.
24
+ void insertkey (int k);
25
+ void deletekey (int k);
26
+ int getmin (); // returns minimum element or the top element from the heap.
27
+ void Minheapify (int i); // Recreates the heap which was disturbed due to deletion or insertion of element.
28
+
29
+ int parent (int i){ // Returns parent node index of i. Similarly left and right functions do their works.
30
+ return (i-1 )/2 ;
31
+ }
32
+
33
+ int left (int i){
34
+ return (2 *i)+1 ;
35
+ }
36
+
37
+ int right (int i){
38
+ return (2 *i)+2 ;
39
+ }
40
+ };
41
+
42
+ MinHeap::MinHeap (int cap){ // Constructor for MinHeap.
43
+ size=0 ;
44
+ capacity=cap;
45
+ harr=new int [cap]; // Creates array of size cap=q. new is used to get address of array in harr.
46
+ }
47
+
48
+ void MinHeap::insertkey (int k){ // Insert key in MinHeap.
49
+ if (size==capacity){
50
+ cout<<" Heap size is full. Couldn't insert." ;
51
+ return ;
52
+ }
53
+
54
+ size++;
55
+ int i=size-1 ;
56
+ harr[i]=k;
57
+ while (i!=0 && harr[parent (i)]>harr[i]){
58
+ swap (&harr[parent (i)],&harr[i]);
59
+ i=parent (i);
60
+ }
61
+ }
62
+
63
+ void MinHeap::deletekey (int k){ // Find the value(k) and delete it from the heap.
64
+ int i,j;
65
+ for (j=0 ;j<size;j++){
66
+ if (harr[j]==k){
67
+ i=j;
68
+ break ;
69
+ }
70
+ }
71
+ if (size==1 || size-1 ==i){
72
+ size--;
73
+ return ;
74
+ }
75
+ harr[i]=harr[size-1 ];
76
+ size--;
77
+ Minheapify (i);
78
+ }
79
+
80
+ int MinHeap::getmin (){ // Returns minimum element from the heap.
81
+ return harr[0 ];
82
+ }
83
+
84
+ void MinHeap::Minheapify (int i){ // Heapify the heap to MinHeap at index i.
85
+ int smallest=i;
86
+ int l=left (i);
87
+ int r=right (i);
88
+ if (harr[smallest]>harr[l] && l<size){
89
+ smallest=l;
90
+ }
91
+ if (harr[smallest]>harr[r] && r<size){
92
+ smallest=r;
93
+ }
94
+ if (smallest!=i){
95
+ swap (&harr[i],&harr[smallest]);
96
+ Minheapify (smallest);
97
+ }
98
+ }
99
+
100
+ int main (){
101
+ int q; // Numeber of queries.
102
+ cin>>q;
103
+ MinHeap h (q); // h is object of types MinHeap.
104
+ while (q--){
105
+ int operation, k;
106
+ cin>>operation;
107
+ if (operation==1 ){
108
+ cin>>k;
109
+ h.insertkey (k);
110
+ }
111
+ else if (operation==2 ){
112
+ cin>>k;
113
+ h.deletekey (k);
114
+ }
115
+ else if (operation==3 ){
116
+ cout<<h.getmin ()<<" \n " ;
117
+ }
118
+ }
119
+ return 0 ;
120
+ }
0 commit comments