-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeapAdvanced.cpp
240 lines (201 loc) · 4.88 KB
/
MinHeapAdvanced.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
MinHeap with special additional functionalities
*/
#include <iostream>
using namespace std;
class minHeap
{
private:
int* h;
int maxSize, currHeapSize;
void bubble_up(int i)
{
int parent = (i - 1) / 2;
while (i > 0 && h[i] < h[parent])
{
swap(h[i], h[parent]);
i = parent;
parent = (i - 1) / 2;
}
}
void bubble_down(int i)
{
int left_child = 2 * i + 1;
int right_child = 2 * i + 2;
int smallest = i;
if (left_child < currHeapSize && h[left_child] < h[smallest])
{
smallest = left_child;
}
if (right_child < currHeapSize && h[right_child] < h[smallest])
{
smallest = right_child;
}
if (smallest != i)
{
swap(h[i], h[smallest]);
bubble_down(smallest);
}
}
public:
minHeap(int capacity = 10) : h(new int[capacity]), maxSize(capacity), currHeapSize(0) {}
void buildMinHeap()
{
for (int i = currHeapSize / 2 - 1; i >= 0; --i)
{
bubble_down(i);
}
}
void insertASingleValueInHeap(const int& x)
{
if (currHeapSize == maxSize)
{
cout << "Heap is full. Cannot insert more elements." << endl;
return;
}
h[currHeapSize] = x;
bubble_up(currHeapSize);
++currHeapSize;
}
bool isEmpty() const
{
return currHeapSize == 0;
}
const int& getMin() const
{
if (isEmpty())
{
cout << "Heap is empty. No minimum value to retrieve." << endl;
return h[0];
}
return h[0];
}
void heapSort(int arr[], int N)
{
minHeap mh(N);
for (int i = 0; i < N; ++i)
{
mh.insertASingleValueInHeap(arr[i]);
}
for (int i = 0; i < N; i++)
{
arr[i] = mh.getMin();
mh.h[0] = mh.h[mh.currHeapSize - 1];
mh.currHeapSize--;
mh.bubble_down(0);
}
}
void heapSortDescending(int arr[], int n)
{
minHeap mh(n);
for (int i = 0; i < n; ++i)
{
mh.insertASingleValueInHeap(arr[i]);
}
for (int i = n - 1; i >= 0; i--)
{
arr[i] = mh.getMin();
mh.h[0] = mh.h[mh.currHeapSize - 1];
--mh.currHeapSize;
mh.bubble_down(0);
}
}
bool isHeap(int arr[], int i, int n)
{
if (i >= n)
{
return true;
}
int left_child = 2 * i + 1;
int right_child = 2 * i + 2;
if (left_child < n && arr[left_child] < arr[i])
{
return false;
}
if (right_child < n && arr[right_child] < arr[i])
{
return false;
}
return isHeap(arr, left_child, n) && isHeap(arr, right_child, n);
}
void printSmallerThan(int arr[], int n, int x)
{
for (int i = 0; i < n; ++i)
{
if (arr[i] < x)
{
cout << arr[i] << " ";
}
}
cout << endl;
}
void convertBSTtoMinHeap(int arr[], int n)
{
heapSort(arr, n);
}
};
void heapify(int* arr, int size, int i)
{
int l = 2 * i + 1;
int r = (2 * i) + 2;
int smll = i;
if (arr[l] < arr[smll] && l < size)
{
smll = l;
}
if (arr[r] < arr[smll] && r < size)
{
smll = r;
}
swap(arr[smll], arr[i]);
if (smll != i)
{
heapify(arr, size, smll);
}
}
void bstToMinheap(int* arr, int size)
{
for (int i = 0; i <= (size / 2) - 1; i++)
{
heapify(arr, size, i);
}
for (int i = 0; i <= size; i++)
{
heapify(arr, size, i);
}
for (int i = 0; i <= (size / 2) - 1; i++)
{
heapify(arr, size, i);
}
}
int main()
{
minHeap myHeap;
int arr[] = { 5, 3, 8, 2, 1, 7, 6 };
int n = 7;
cout << "Original array: ";
for (int i = 0; i < n; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
myHeap.heapSort(arr, n);
cout << "Sorted array using heapsort: ";
for (int i = 0; i < n; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
bool isHeap = myHeap.isHeap(arr, 0, n);
cout << "Is the array a binary heap? " << (isHeap ? "Yes" : "No") << endl;
myHeap.heapSortDescending(arr, n);
cout << "Sorted array in descending order using heapsort: ";
for (int i = 0; i < n; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
cout << "Values smaller than 6: ";
myHeap.printSmallerThan(arr, n, 6);
return 0;
}}