-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
352 lines (269 loc) · 6.34 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* heap.cpp
*
* Implementations for Heap interface/abstrast base class.
*
* @author Juan Arias
*
*/
#include <iostream>
#include "heap.h"
// Type definition for Nodes in a heap
using Node = int;
//**************// //**************// //**************//
//* PUBLIC: *// //* PUBLIC: *// //* PUBLIC: *//
//**************// //**************// //**************//
/*
* Destroys heap and deallocates all dynamic memory
*/
template <class T>
Heap<T>::~Heap() {
this->clear();
}
/*
* Assignment operator overload
* @param other The other heap to copy
* @return this heap by reference
*/
template<class T>
Heap<T>& Heap<T>::operator=(const Heap<T>& other) {
if (this != &other) {
if (this->MAX != other.MAX) {
this->clear();
this->arr = new T[other.MAX];
this->MAX = other.MAX;
}
this->itemCount = other.itemCount;
this->copyArray(other.arr);
}
return (*this);
}
/*
* Equality operator overload
* @param other The other heap to compare
* @return true if equal, else false
*/
template <class T>
bool Heap<T>::operator==(const Heap<T>& other) const {
bool equal(true);
if (this != &other || !(this->isEmpty()) && (other.isEmpty()) ) {
if (this->itemCount != other.itemCount) {
equal = false;
} else {
for (Node curr(Heap<T>::ROOT); (curr < this->itemCount && equal); ++curr) {
equal = (this->arr[curr] == other.arr[curr]);
}
}
}
return equal;
}
/*
* Inequality operator overload
* @param other The other heap to compare
* @return true if not equal, else false
*/
template <class T>
bool Heap<T>::operator!=(const Heap<T>& other) const {
return !((*this) == other);
}
/*
* Check if heap is empty
* @return true if empty, else false
*/
template <class T>
bool Heap<T>::isEmpty() const {
return (this->itemCount == Heap<T>::EMPTY);
}
/*
* Get the height of the heap
* @return the height of the heap
*/
template <class T>
int Heap<T>::getHeight() const {
Node curr(Heap<T>::ROOT);
int height(0);
while (curr < this->itemCount) {
++height;
curr = Heap<T>::left(curr);
}
return height;
}
/*
* Get the number of nodes in the heap
* @return the number of nodes in the heap
*/
template <class T>
int Heap<T>::getNodes() const {
return this->itemCount;
}
/*
* Get the peek item in the heap
* @return the peek item in the heap
*/
template <class T>
T& Heap<T>::peek() const {
if (this->arr != nullptr && this->itemCount > Heap<T>::EMPTY) {
return this->arr[Heap<T>::ROOT];
}
throw Heap<T>::EMPTY;
}
/*
* Clear the heap
*/
template <class T>
void Heap<T>::clear() {
if (this->arr != nullptr) {
delete[] this->arr;
this->arr = nullptr;
this->itemCount = Heap<T>::EMPTY;
}
}
/*
* Display heap sideways
* @param
*/
template<class T>
void Heap<T>::displaySideways() {
this->sideways(Heap<T>::ROOT, Heap<T>::EMPTY);
}
//***************// //***************// //***************//
//* PROTECTED: *// //* PROTECTED: *// //* PROTECTED: *//
//***************// //***************// //***************//
/*
* Constructs empty heap
*/
template<class T>
Heap<T>::Heap() :arr(nullptr), itemCount(Heap<T>::EMPTY), MAX(Heap<T>::EMPTY) {}
/*
* Constructs heap from given array
* @param arr The array to construct heap from
* @param size The size of arr
*/
template<class T>
Heap<T>::Heap(const T arr[], int size) :arr(new T[size * 2]), itemCount(size), MAX(size * 2) {
this->copyArray(arr);
}
/*
* Default initialization
*/
template<class T>
void Heap<T>::initialize() {
this->arr = new T[Heap<T>::DEFAULT / 2];
this->MAX = Heap<T>::DEFAULT;
}
/*
* Swaps the items in the given indexes
* @param node1 The index of the first node
* @param node2 The index of the second node
*/
template<class T>
void Heap<T>::swap(Node node1, Node node2) {
Heap<T>::swap(this->arr, node1, node2);
}
/*
* Static method
* Checks if the given node is a leaf
* @param curr The current node
* @return true if leaf, else false
*/
template<class T>
bool Heap<T>::isLeaf(Node curr, int itemCount) {
return (Heap<T>::left(curr) >= itemCount);
}
/*
* Static method
* Gets the larger of the given node's children
* @param curr The current node
* @return the larger child
*/
template<class T>
Node Heap<T>::largerChild(T arr[], int itemCount, Node curr) {
Node larger = Heap<T>::left(curr);
if (Heap<T>::hasRight(curr, itemCount)) {
Node right = Heap<T>::right(curr);
larger = (arr[right] > arr[larger]) ? right : larger;
}
return larger;
}
/*
* Static method
* Gets the parent of the given node
* @param curr The current node
* @return the parent of curr
*/
template<class T>
Node Heap<T>::parent(Node curr) {
return (curr - 1) / 2;
}
/*
* Static method
* Swaps the items in the given indexes
* @param node1 The index of the first node
* @param node2 The index of the second node
*/
template<class T>
void Heap<T>::swap(T arr[], Node node1, Node node2) {
T temp = arr[node1];
arr[node1] = arr[node2];
arr[node2] = temp;
}
//**************// //**************// //**************//
//* PRIVATE: *// //* PRIVATE: *// //* PRIVATE: *//
//**************// //**************// //**************//
/*
* Copies elements of given array and forms heap
* @param arr The given array to copy
*/
template<class T>
void Heap<T>::copyArray(const T arr[]) {
for (Node curr(Heap<T>::ROOT); curr < this->itemCount; ++curr) {
this->arr[curr] = arr[curr];
}
}
/*
* Static method
* Gets the left child of the given node
* @param curr The current node
* @return the left child of curr
*/
template<class T>
Node Heap<T>::left(Node curr) {
return (2 * curr + 1);
}
/*
* Static method
* Checks if the given node has right child
* @param curr The current node
* @return true if has right child, else false
*/
template<class T>
bool Heap<T>::hasRight(Node curr, int itemCount) {
return (Heap<T>::right(curr) < itemCount);
}
/*
* Static method
* Gets the right child of the given node
* @param curr The current node
* @return the right child of curr
*/
template<class T>
Node Heap<T>::right(Node curr) {
return (2 * curr + 2);
}
/*
* Helper function for display sideways
* @param curr The current node
* @param level the current level of the heap
*/
template<class T>
void Heap<T>::sideways(Node curr, int level) {
if (curr < this->itemCount) {
++level;
this->sideways(Heap<T>::right(curr), level);
for (int i(level); i >= Heap<T>::ROOT; --i) {
std::cout << " ";
}
std::cout << this->arr[curr] << std::endl;
this->sideways(Heap<T>::left(curr), level);
}
}