-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
314 lines (279 loc) · 7.74 KB
/
Copy pathheap.cpp
File metadata and controls
314 lines (279 loc) · 7.74 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
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
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
//Use proper macros so that both min heap and max heap are handled.
#define UNDEFINED 1000001 //remove it
#define ERROR -1 //use exceptions handling
#define DEFAULT_HEAP_CAPACITY 5
typedef bool (*compareFn) (int, int);
#define ENABLE_DEBUG_PRINT 0
#define ENABLE_EXCEPTION_PRINT 1
void debugPrint(string msg) {
#if ENABLE_DEBUG_PRINT
if(!msg.empty()) {
cout << msg << endl;
}
#endif // ENABLE_DEBUG_PRINT
}
void exceptionPrint(string exceptionMessage) {
#if ENABLE_EXCEPTION_PRINT
if(!exceptionMessage.empty()) {
cout << "Exception encountered: " << exceptionMessage << endl;
}
#endif // ENABLE_EXCEPTION_PRINT
}
bool smaller (int a, int b) {
return a < b;
}
bool bigger (int a, int b) {
return a > b;
}
class Heap {
protected:
vector<int> elementStore;
int size;
int capacity;
compareFn comparator;
string heapName;
void heapify(int);
void exchange (int&, int&);
int getLeftChild(int);
int getRightChild(int);
int getParent(int);
bool isValidIndex(int);
public:
Heap(compareFn f);
Heap(int c, compareFn f);
Heap(int c, compareFn f, vector<int> &a);
Heap(int c, compareFn f, int* a, int n);
int getTop(void);
int getSize(void);
int getCapacity(void);
bool isEmpty(void);
bool isFull(void);
int deleteTop(void);
int insertKey(int);
int changeKey(int, int);
void printElementStore(void);
string getHeapName(void);
void setHeapName(string);
void printHeapStats(void);
};
Heap::Heap(compareFn f) {
capacity = DEFAULT_HEAP_CAPACITY;
elementStore.resize(capacity);
assert(capacity == elementStore.size());
size = 0;
comparator = f;
heapName = "<NotDefined>";
}
Heap::Heap(int c, compareFn f) {
assert(c >= 0);
capacity = c;
elementStore.resize(capacity);
assert(c == elementStore.size());
size = 0;
comparator = f;
heapName = "<NotDefined>";
}
Heap::Heap(int c, compareFn f, vector<int> &a) {
assert((c >= 0) && (c >= a.size()));
capacity = c;
size = a.size();
comparator = f;
elementStore.resize(capacity);
for(int i = 0; i < size; i++)
elementStore[i] = a[i];
heapName = "<NotDefined>";
assert(capacity == elementStore.size());
assert(size == a.size());
//can put here assert for values' equality check
if(size)
heapify(0);
}
Heap::Heap(int c, compareFn f, int *a, int n) {
assert((c >= 0) && (c >= n));
capacity = c;
size = n;
comparator = f;
heapName = "<NotDefined>";
elementStore.resize(capacity);
for(int i = 0; i < n; i++)
elementStore[i] = a[i];
assert(c == elementStore.size());
if(size)
heapify(0);
}
string Heap::getHeapName(void) {
return heapName;
}
void Heap::setHeapName(string name) {
heapName = name;
}
void Heap::printHeapStats(void) {
cout << "Heap-name = " << heapName << ": " ;
cout << "Heap-size = " << size << " ";
cout << "Heap-capacity = " << capacity << endl;
}
int Heap::getLeftChild(int index) {
if(isValidIndex(index)) {
return (index << 1) + 1;
}
return -1;
}
int Heap::getRightChild(int index) {
if(isValidIndex(index)) {
return (index << 1) + 2;
}
return -1;
}
int Heap::getParent(int index){
if(isValidIndex(index)) {
return ((index - 1) >> 1);
}
return -1;
}
int Heap::getSize(void) {
return size;
}
int Heap::getCapacity(void) {
return capacity;
}
bool Heap::isValidIndex(int index) {
if((index >= size) || (index < 0))
return false;
else
return true;
}
bool Heap::isEmpty(void) {
return size == 0;
}
bool Heap::isFull(void) {
return size == capacity;
}
void Heap::exchange(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
void Heap::heapify(int curIndex) {
int left = getLeftChild(curIndex);
int right = getRightChild(curIndex);
int better = curIndex;
if(isValidIndex(left) && comparator(elementStore[left], elementStore[better]))
better = left;
if(isValidIndex(right) && comparator(elementStore[right], elementStore[better]))
better = right;
if(better != curIndex) {
exchange(elementStore[better], elementStore[curIndex]);
heapify(better);
}
}
int Heap::insertKey(int key) {
if(isFull()) {
exceptionPrint("HeapFull");
printHeapStats();
exceptionPrint("HeapExpansion since Heap is full.");
elementStore.resize(2*capacity);
assert(elementStore.size() == 2*capacity);
capacity = 2*capacity;
}
elementStore[size] = key;
size++;
int curIndex = size - 1;
int parent = getParent(curIndex);
while((curIndex > 0) && (comparator(elementStore[curIndex], elementStore[parent]))) {
exchange(elementStore[parent], elementStore[curIndex]);
curIndex = parent;
parent = getParent(curIndex);
}
return 0; //1 for error - 0 for success
}
int Heap::changeKey(int index, int key) {
if(!isValidIndex(index)) return 1;
if(comparator(elementStore[index], key)) return 1; //only considers changes to make existing values "better"
elementStore[index] = key;
int curIndex = index;
int parent = getParent(curIndex);
while((curIndex > 0) && (comparator(elementStore[parent], elementStore[curIndex]))) {
exchange(elementStore[parent], elementStore[curIndex]);
curIndex = parent;
parent = getParent(curIndex);
}
return 0; //1 for error, 0 for success
}
int Heap::getTop(void) {
if(isEmpty()) return UNDEFINED; //Need proper UNDEFINED value for distinguishing error with genuine top value
return elementStore[0];
}
int Heap::deleteTop(void) {
if(isEmpty()) return -1; //Need proper value to distinguish error scenario with genuine top value
int curTop = getTop();
elementStore[0] = elementStore[size - 1]; //can't use changeKey since we may be "worsening" the top value
size--;
heapify(0);
return curTop;
}
void Heap::printElementStore(void) {
if(isEmpty()) {
exceptionPrint("HeapEmpty");
return;
}
cout << heapName << ": ";
for(int i = 0; i < size; i++){
cout << elementStore[i] << " ";
}
cout << endl;
}
class MinHeap : public Heap {
public:
MinHeap() : Heap(smaller) {}
MinHeap(int c) : Heap(c, smaller) {}
MinHeap(int c, vector<int> &a) : Heap(c, smaller, a) {}
MinHeap(int c, int* a, int n) : Heap(c, smaller, a, n) {}
};
class MaxHeap : public Heap {
public:
MaxHeap() : Heap(bigger) {}
MaxHeap(int c) : Heap(c, bigger) {}
MaxHeap(int c, vector<int> &a) : Heap(c, bigger, a) {}
MaxHeap(int c, int* a, int n) : Heap(c, bigger, a, n) {}
};
int main() {
cout << "Using min heap first\n";
MinHeap minh;
minh.insertKey(4);
cout << "Added 4\n";
minh.insertKey(2);
cout << "Added 2\n";
cout << minh.getTop() << endl;
minh.deleteTop();
cout << "performed deleteTop\n";
cout << minh.getTop() << endl;
minh.insertKey(5);
cout << "added 5\n";
minh.insertKey(7);
cout << "added 7\n";
minh.insertKey(3);
cout << "added 3\n";
cout << minh.getTop() << endl;
cout << "Now using max heap\n";
MaxHeap maxh;
maxh.insertKey(4);
cout << "added 4\n";
maxh.insertKey(2);
cout << "added 2\n";
cout << maxh.getTop() << endl;
maxh.deleteTop();
cout << "performed deleteTop\n";
cout << maxh.getTop() << endl;
maxh.insertKey(5);
cout << "added 5\n";
maxh.insertKey(7);
cout << "added 7\n";
maxh.insertKey(3);
cout << "added 3\n";
cout << maxh.getTop() << endl;
return 0;
}