-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheap.cpp
50 lines (37 loc) · 1.04 KB
/
heap.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
#include <iostream>
#include <vector>
using namespace std;
void min_heapify(vector<int> &arr, int idx) {
int N = arr.size();
if (idx >= N)
return;
int left = 2*idx + 1;
int right = 2*idx + 2;
int smallest = idx;
if(left < N && arr[left] < arr[smallest] )
smallest = left;
if(right < N && arr[right] < arr[smallest] )
smallest = right;
if(smallest != idx) { // Case 2 and 3
swap(arr[idx], arr[smallest]);
min_heapify(arr, smallest);
}
}
int pop(vector<int> &arr) {
int root = arr[0];
int N = arr.size();
swap(arr[0], arr[N-1]);
arr.pop_back();
min_heapify(arr, 0);
return root;
}
int main() {
// heap
vector<int> arr = {3, 6, 9, 10, 14, 10, 11, 17, 21, 16, 20, 13};
cout << "Heap before popping: ";
for (auto it : arr) cout << it << " "; cout << "\n";
cout << "Popped integer: " << pop(arr) << "\n";
cout << "Heap after popping: ";
for (auto it : arr) cout << it << " "; cout << "\n";
return 0;
}