-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathheap.h
400 lines (354 loc) · 8.07 KB
/
heap.h
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
HEAP.H
------
*/
#ifndef HEAP_H
#define HEAP_H
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/*
class ANT_PRIMARY_CMP
---------------------
*/
template <typename T> class ANT_primary_cmp
{
public:
int operator()(const T &a, const T &b) { return a - b; }
};
/*
class ANT_HEAP
--------------
*/
template <typename T, typename _Compare = ANT_primary_cmp<T> >
class ANT_heap
{
private:
T *array;
long long size;
inline void swap (T *a, T *b)
{
T temp = *a;
*a = *b;
*b = temp;
}
_Compare compare;
public:
ANT_heap(T &arry, long long size, const _Compare& __c = _Compare()) : array(&arry), size(size), compare(__c) {};
#define parent_pos(i) (((i)-1) >> 1)
#define left_pos(i) (((i) << 1) + 1)
#define right_pos(i) (((i) << 1) + 2)
inline void set_size(long long size) { this->size = size; }
inline long long get_size(void) { return this->size; }
void max_heapify(long long pos);
void max_heapify(long long pos, long long hsize);
void build_max_heap(void);
int max_update_maximum(T key);
void max_heapsort(void);
void text_render(long long i);
void min_heapify(long long pos);
void min_heapify(long long pos, long long hsize);
void build_min_heap(void);
void min_heapsort(void);
int min_insert(T key);
void min_update(T key);
T get_second_smallest(void);
};
/*
ANT_HEAP::MAX_UPDATE_MAXIMUM()
------------------------------
*/
template <typename T, typename _Compare> int ANT_heap<T, _Compare>::max_update_maximum(T key)
{
long long i = 0, lpos, rpos;
while (i < this->size)
{
lpos = left_pos(i);
rpos = right_pos(i);
// check array out of bound, it's also the stopping condition
if (lpos < this->size && rpos < this->size)
{
if (compare(key, array[lpos]) >= 0 && compare(key, array[rpos]) >= 0)
break; // we're smaller then the left and the right so we're done
else if (compare(array[lpos], array[rpos]) > 0)
{
array[i] = array[lpos];
i = lpos;
}
else
{
array[i] = array[rpos];
i = rpos;
}
}
else if (lpos < this->size) // and rpos > this->size (because this is an else)
{
if (compare(key, array[lpos]) < 0)
{
array[i] = array[lpos];
i = lpos;
}
else
break;
}
else
break; // both lpos and lpos exceed end of array
}
array[i] = key;
return 1;
}
/*
ANT_HEAP::MIN_INSERT()
----------------------
*/
template <typename T, typename _Compare>
int ANT_heap<T, _Compare>::min_insert(T key)
{
long long i = 0, lpos, rpos;
// if key is less than the minimum in heap, then do nothing
if (compare(key, array[0]) < 0)
return 0;
while (i < this->size)
{
lpos = left_pos(i);
rpos = right_pos(i);
// check array out of bound, it's also the stopping condition
if (lpos < this->size && rpos < this->size)
{
if (compare(key, array[lpos]) <= 0 && compare(key, array[rpos]) <= 0)
break; // we're smaller then the left and the right so we're done
else if (compare(array[lpos], array[rpos]) < 0)
{
array[i] = array[lpos];
i = lpos;
}
else
{
array[i] = array[rpos];
i = rpos;
}
}
else if (lpos < this->size) // and rpos > this->size (because this is an else)
{
if (compare(key, array[lpos]) > 0)
{
array[i] = array[lpos];
i = lpos;
}
else
break;
}
else
break; // both lpos and lpos exceed end of array
}
array[i] = key;
return 1;
}
/*
ANT_HEAP::MIN_UPDATE()
----------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::min_update(T key)
{
long long i, lpos, rpos;
for (i = 0; i < this->size; i++)
if (array[i] == key)
break;
while (i < this->size)
{
lpos = left_pos(i);
rpos = right_pos(i);
if ((lpos < this->size) && (rpos < this->size))
{
if ((compare(key, array[lpos]) <= 0) && (compare(key, array[rpos]) <= 0))
break;
else if (compare(array[lpos], array[rpos]) > 0)
{
array[i] = array[rpos];
i = rpos;
}
else
{
array[i] = array[lpos];
i = lpos;
}
}
else if (lpos < this->size)
{
if (compare(key, array[lpos]) > 0)
{
array[i] = array[lpos];
i = lpos;
}
else
break; // we know it's the last one, so just exit the loop
}
else
break;
}
array[i] = key;
}
/*
ANT_HEAP::MAX_HEAPIFY()
-----------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::max_heapify(long long pos)
{
long long left = left_pos(pos);
long long right = right_pos(pos);
long long largest;
if ((left < size) && (compare(array[left], array[pos]) > 0))
largest = left;
else
largest = pos;
if ((right < size) && (compare(array[right], array[largest]) > 0))
largest = right;
if (largest != pos)
{
swap(&array[pos], &array[largest]);
max_heapify(largest);
}
}
/*
ANT_HEAP::MAX_HEAPIFY()
-----------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::max_heapify(long long pos, long long hsize)
{
long long left = left_pos(pos);
long long right = right_pos(pos);
long long largest;
if ((left < hsize) && (compare(array[left], array[pos]) > 0))
largest = left;
else
largest = pos;
if ((right < hsize) && (compare(array[right], array[largest]) > 0))
largest = right;
if (largest != pos)
{
swap(&array[largest], &array[pos]);
max_heapify(largest, hsize);
}
}
/*
ANT_HEAP::BUILD_MAX_HEAP()
--------------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::build_max_heap()
{
for (long long i = size/2-1; i >= 0; i--)
max_heapify(i);
}
/*
ANT_HEAP::TEXT_RENDER()
-----------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::text_render(long long i)
{
#ifdef NEVER
printf("i: %lld, ", i);
for (long long i = 0; i < size; i++)
printf("%lld ", array[i]);
printf("\n");
#endif
}
/*
ANT_HEAP::MAX_HEAPSORT()
------------------------
Sort in ascending order
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::max_heapsort()
{
build_max_heap();
for (long long i = size-1; i >= 1; i--)
{
swap(&array[0], &array[i]);
max_heapify(0, i);
}
}
/*
ANT_HEAP::MIN_HEAPIFY()
-----------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::min_heapify(long long pos)
{
long long left = left_pos(pos);
long long right = right_pos(pos);
long long smallest;
if ((left < size) && (compare(array[left], array[pos]) < 0))
smallest = left;
else
smallest = pos;
if ((right < size) && (compare(array[right], array[smallest]) < 0))
smallest = right;
if (smallest != pos)
{
swap(&array[pos], &array[smallest]);
min_heapify(smallest);
}
}
/*
ANT_HEAP::MIN_HEAPIFY()
-----------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::min_heapify(long long pos, long long hsize)
{
long long left = left_pos(pos);
long long right = right_pos(pos);
long long smallest;
if ((left < hsize) && (compare(array[left], array[pos])) < 0)
smallest = left;
else
smallest = pos;
if ((right < hsize) && (compare(array[right], array[smallest]) < 0))
smallest = right;
if (smallest != pos)
{
swap(&array[pos], &array[smallest]);
min_heapify(smallest, hsize);
}
}
/*
ANT_HEAP::BUILD_MIN_HEAP()
--------------------------
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::build_min_heap()
{
for (long long i = size/2 - 1; i >= 0; i--)
min_heapify(i);
}
/*
ANT_HEAP::MIN_HEAPSORT()
------------------------
Sort in descending order
*/
template <typename T, typename _Compare> void ANT_heap<T, _Compare>::min_heapsort()
{
build_min_heap();
for (long long i = size-1; i >= 1; i--)
{
swap(&array[0], &array[i]);
min_heapify(0, i);
}
}
/*
ANT_HEAP::get_second_smallest()
-------------------------------
*/
template <typename T, typename _Compare> T ANT_heap<T, _Compare>::get_second_smallest() {
T the_left, the_right;
if (size < 2) {
perror("error calling heapk::get_second_smallest\n");
exit(2);
} else if (size == 2) {
//printf("0 left pos[%ld]: %ld\n", left_pos(0), array[left_pos(0)]);
return array[left_pos(0)];
} else {
//printf("0 left pos[%ld]: %ld\n", left_pos(0), array[left_pos(0)]);
//printf("0 right pos[%ld]: %ld\n", right_pos(0), array[right_pos(0)]);
the_left = array[left_pos(0)];
the_right = array[right_pos(0)];
return the_left <= the_right ? the_left : the_right;
}
}
#endif