-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertIntoHeap.cpp
More file actions
98 lines (81 loc) · 2.3 KB
/
insertIntoHeap.cpp
File metadata and controls
98 lines (81 loc) · 2.3 KB
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
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<vector>
using namespace std;
class Heap{
vector<int>v;
bool minHeap; // If true, we'll build a min heap. Else, a max heap
bool compare(int a, int b){
if(minHeap){
return a < b;
}
else{
return a > b;
}
}
void heapify(int parent){
// Indices of left and right child
int left = 2*parent;
int right = 2*parent + 1;
int min_idx = parent; // To track min (or max) element up till now
int last = v.size()-1;
// Swapping can be done only if left/right child exist in the first place
// To check whether they exist, we'll see if indices of both the left and right child
// is less than the last index of the vector
if(left <= last && compare(v[left], v[parent])){
min_idx = left;
}
if(right <= last && compare(v[right], v[min_idx])){
min_idx = right;
}
if(min_idx != parent){
swap(v[parent], v[min_idx]);
heapify(min_idx);
}
}
public:
Heap(int default_size=10, bool type=true){
v.reserve(default_size);
v.push_back(-1); // blocking the 0th index with -1
minHeap = type;
}
void push(int data){
v.push_back(data);
int idx = v.size()-1 ;
int parent_idx = idx/2 ;
// Readjust the tree to maintain heap property
// Keep pushing the element to the top, till you either reach the root, or have to
// stop midway 'cuz your node has become > (or < ) its parent.
while(idx > 1 && compare(v[idx], v[parent_idx])){
swap(v[idx], v[parent_idx]);
idx = parent_idx;
parent_idx = idx/2 ;
}
}
int top(){
return v[1];
}
void pop(){
int last_idx = v.size() - 1;
swap(v[1], v[last_idx]);
v.pop_back();
heapify(1); // Call heapify with the index you want to start heapification with
}
bool isEmpty(){
return v.size() == 1;
}
};
int main(){
Heap h;
int n;
cin >> n;
for (int i = 0; i < n; i++){
int d;
cin >> d;
h.push(d);
}
while(!h.isEmpty()){
cout << h.top() << " ";
h.pop();
}
return 0;
}