-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_max_heap.cpp
More file actions
261 lines (226 loc) · 3.41 KB
/
binary_max_heap.cpp
File metadata and controls
261 lines (226 loc) · 3.41 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <iostream>
#include <stdlib.h>
using namespace std;
#define MAX_SIZE 100
template <typename T>
class BinaryMaxHeap
{
public:
T *array = new T[MAX_SIZE];
int size;
int Size();
void Insert(T );
T GetMax();
void SiftUp(int );
void SiftDown(int );
void Swap(int, int);
T ExtractMax();
void Remove(int );
void ChangePriority(int , T);
void PrintHeap();
BinaryMaxHeap<T>()
{
size = 0;
}
~BinaryMaxHeap<T>()
{
delete [] array;
}
};
template <typename T>
void BinaryMaxHeap<T>::Swap(int i, int j)
{
T x = array[i];
T y = array[j];
array[j] = x;
array[i] = y;
}
template <typename T>
T BinaryMaxHeap<T>::GetMax()
{
try
{
if (size == 0)
{
throw "Heap is empty!";
}
}
catch (const char* x)
{
cout << x << endl;
return T(-1);
}
return array[0];
}
template <typename T>
int BinaryMaxHeap<T>::Size()
{
return size;
}
template <typename T>
void BinaryMaxHeap<T>::Insert(T data)
{
if (size == MAX_SIZE)
{
cout << "Can not insert new data! Full Occupancy" << endl;
return;
}
array[size] = data;
size += 1;
SiftUp(size-1);
}
int Parent(int i)
{
int parent = (i+1)/2 - 1;
return parent;
}
int LeftChild(int i)
{
int lchild = 2*i+1;
return lchild;
}
int RightChild(int i)
{
int rchild = 2*i+2;
return rchild;
}
template <typename T>
void BinaryMaxHeap<T>::SiftUp(int i)
{
try
{
if (i > size-1 or i < 0)
{
throw "Index can not be more than the heap size!";
}
}
catch (const char* x)
{
cout << x << endl;
return;
}
if (i == 0)
{
return;
}
int parent = Parent(i);
if (parent >= 0 and array[parent] < array[i])
{
Swap(i, parent);
SiftUp(parent);
}
}
template <typename T>
void BinaryMaxHeap<T>::SiftDown(int i)
{
try
{
if (i > size-1 or i < 0)
{
throw "Index can not be more than the heap size!";
}
}
catch (const char* x)
{
cout << x << endl;
return;
}
if (i == size-1)
{
return;
}
int max_index = i;
int lchild = LeftChild(i);
int rchild = RightChild(i);
if (lchild < size and array[lchild] > array[max_index])
{
max_index = lchild;
}
if (rchild < size and array[rchild] > array[max_index])
{
max_index = rchild;
}
if (max_index != i)
{
Swap(i, max_index);
SiftDown(max_index);
}
}
template <typename T>
T BinaryMaxHeap<T>::ExtractMax()
{
if (size == 0)
{
cout << "Heap is empty!" << endl;
return T(-1);
}
if (size == 1)
{
size -= 1;
return array[size];
}
T result = array[0];
array[0] = array[size-1];
size -= 1;
SiftDown(0);
return result;
}
template <typename T>
void BinaryMaxHeap<T>::Remove(int i)
{
T max = GetMax();
array[i] = max+T(10);
SiftUp(i);
ExtractMax();
}
template <typename T>
void BinaryMaxHeap<T>::ChangePriority(int i, T new_priority)
{
if (i < 0 or i > size-1)
{
cout << "Index out of range!" << endl;
return;
}
T old_priority = array[i];
array[i] = new_priority;
if (new_priority > old_priority)
{
SiftUp(i);
}
else
{
SiftDown(i);
}
}
template <typename T>
void BinaryMaxHeap<T>::PrintHeap()
{
if (size == 0)
{
cout << "Heap is empty!" << endl;
return;
}
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
typedef BinaryMaxHeap<int> BMH;
int main()
{
BMH heap;
heap.Insert(10);
heap.Insert(5);
heap.Insert(20);
heap.Insert(100);
heap.PrintHeap();
int x = heap.ExtractMax();
cout << x << endl;
heap.PrintHeap();
int y = heap.ExtractMax();
cout << y << endl;
int z = heap.ExtractMax();
cout << z << endl;
return 0;
}