-
Notifications
You must be signed in to change notification settings - Fork 1
/
Remove min.cpp
89 lines (67 loc) · 2.01 KB
/
Remove min.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Remove Min
Implement the function RemoveMin for the min priority queue class.
For a minimum priority queue, write the function for removing the minimum element present. Remove and return the minimum element.
Note : main function is given for your reference which we are using internally to test the code.
*/
#include <vector>
class PriorityQueue {
vector<int> pq;
public:
bool isEmpty() {
return pq.size() == 0;
}
int getSize() {
return pq.size();
}
int getMin() {
if (isEmpty()) {
return 0;
}
return pq[0];
}
void insert(int element) {
pq.push_back(element);
int childIndex = pq.size() - 1;
while (childIndex > 0) {
int parentIndex = (childIndex - 1) / 2;
if (pq[childIndex] < pq[parentIndex]) {
int temp = pq[childIndex];
pq[childIndex] = pq[parentIndex];
pq[parentIndex] = temp;
} else {
break;
}
childIndex = parentIndex;
}
}
void downHeapify(vector<int>& pq, int size, int index) {
int curr = index;
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if(leftChildIndex < size and pq[leftChildIndex] < pq[curr] ) {
curr = leftChildIndex;
}
if(rightChildIndex < size and pq[rightChildIndex] < pq[curr]) {
curr = rightChildIndex;
}
if(curr != index) {
swap(pq[curr], pq[index]);
// Recursion will heapify the remaining tree
downHeapify(pq, size, curr);
}
}
int removeMin() {
// Write your code here
int ans;
if(pq.size()) {
ans = pq.front();
} else {
return -1;
}
pq[0] = pq[pq.size() - 1];
pq.pop();
downHeapify(pq, pq.size(), 0);
return ans;
}
};