-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryHeap.cpp
161 lines (150 loc) · 4.2 KB
/
binaryHeap.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "bits/stdc++.h"
using namespace std;
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v) {for (auto &it : v)cin >> it; return istream;}
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }
class minHeap {
vector<int> heap;
int parent(int i) {return ((i - 1) / 2);}
int left(int i) {return ((2 * i) + 1);}
int right(int i) {return ((2 * i) + 2);}
void heapifyUp(int i) {
while (i != 0 && heap[i] < heap[parent(i)]) {
swap(heap[i], heap[parent(i)]);
i = parent(i);
}
}
void heapifyDown(int i) {
int l = left(i), r = right(i);
int smallest = i;
if (l < size() && heap[l] < heap[smallest]) smallest = l;
if (r < size() && heap[r] < heap[smallest]) smallest = r;
if (smallest != i) {
swap(heap[smallest], heap[i]);
heapifyDown(smallest);
}
}
public:
minHeap() {}
int size() {
return heap.size();
}
void push(int element) { // insert
heap.push_back(element);
heapifyUp(size() - 1);
}
void display() {
cout << endl << "heap -> ";
for (auto &ele : heap) {
cout << ele << " ";
}
cout << endl;
}
void top() { // extractMin
cout << "Top Element -> " << heap[0] << endl;
}
void pop() { // deleteMin
if (size() == 1) {heap.pop_back(); return;}
heap[0] = heap[size() - 1];
heap.pop_back();
heapifyDown(0);
}
};
class maxHeap {
private:
vector<int> heap;
int left(int i) {return ((2 * i) + 1);}
int right(int i) {return ((2 * i) + 2);}
int parent(int i) {return (i - 1) / 2;}
void heapifyUp(int i) {
while (i != 0 && heap[i] > heap[parent(i)]) {
swap(heap[i], heap[parent(i)]);
i = parent(i);
}
}
void heapifyDown(int i) {
int biggest = i, l = left(i), r = right(i);
if (l < size() && heap[l] > heap[biggest]) biggest = l;
if (r < size() && heap[r] > heap[biggest]) biggest = r;
if (biggest != i) {
swap(heap[i], heap[biggest]);
heapifyDown(biggest);
}
}
public:
int size() {return heap.size();}
void push(int element) {
heap.push_back(element);
heapifyUp(size() - 1);
}
void top() {
cout << "Top Element -> " << heap[0] << endl;
}
void pop() {
if (size() == 1) {heap.pop_back(); return;}
heap[0] = heap[size() - 1];
heap.pop_back();
heapifyDown(0);
}
};
void heapify(vector<int> &arr, int n, int i) {
int largest = i;
int l = (2 * i) + 1;
int r = (2 * i) + 2;
if (l < n and arr[l] > arr[largest]) largest = l;
if (r < n and arr[r] > arr[largest]) largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(vector<int> &arr) {
for (int i = ((arr.size() / 2) - 1); i >= 0; i--) {
heapify(arr, arr.size(), i);
}
cout << arr << endl;
for (int i = (arr.size() - 1); i > 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void solve() {
// minHeap pq;
// maxHeap pq;
// pq.push(12);
// pq.push(14);
// pq.push(11);
// pq.push(20);
// pq.push(1);
// pq.push(2);
// pq.push(92);
// pq.top();
// pq.pop();
// pq.top();
// pq.pop();
// pq.top();
// pq.pop();
// pq.top();
// pq.pop();
// pq.top();
// pq.pop();
// pq.top();
// pq.pop();
// pq.top();
// pq.pop();
vector<int> arr = {10, 5, 6, 2, 12, 7, 9};
heapSort(arr);
cout << arr;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout.tie(0);
solve();
return 0;
}