-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
391 lines (356 loc) · 9.76 KB
/
heap.c
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
// #include "heap.h"
typedef int (*__compar_fn_t)(const void *, const void *);
#include <stdio.h>
#include <stdlib.h>
// this provide an heap with satellite information of void pointer.
// #define PRINT_FULLNESS
int biggerthan(const void *a, const void *b) {
int a1 = *(int *)a;
int b1 = *(int *)b;
if (a1 > b1) return -1;
if (a1 < b1) return 1;
return 0;
}
int smallerthan(const void *a, const void *b) {
int a1 = *(int *)a;
int b1 = *(int *)b;
if (a1 < b1) return -1;
if (a1 > b1) return 1;
return 0;
}
// The supporting data structure should have as key the same key (to extract
// the min) and the index position as satellite information, to return the pos
typedef struct {
void *data;
int pos;
} augmented;
augmented *augm_create(void *data, int pos) {
augmented *a = (augmented *)malloc(sizeof(augmented));
*a = (augmented){.data = data, .pos = pos};
return a;
}
int smallerthan_aug(const void *a, const void *b) {
// this compare the augmented struct
int a1 = *(int *)(((augmented *)a)->data);
int b1 = *(int *)(((augmented *)b)->data);
if (a1 < b1) return -1;
if (a1 > b1) return 1;
return 0;
}
// this is used for storing indexes.
// compare function is always the same: min preceeds a greater value.
// Another way to state it: it is a Min-Heap
typedef struct {
// int (*cmp)(void *, void *);
int *data;
int size;
int capacity;
} MinHeapInt;
MinHeapInt *minheapint_create(int capacity) {
MinHeapInt *h = (MinHeapInt *)malloc(sizeof(MinHeapInt));
*h = (MinHeapInt){.capacity = capacity,
.data = (int *)calloc(sizeof(int *), capacity),
.size = 0};
return h;
}
void minheapint_free(MinHeapInt **hp) {
MinHeapInt *h = *hp;
free(h->data);
free(h);
*hp = NULL;
}
void minheapint_heapify(MinHeapInt *h, int i) {
// comment
int first = i;
int l = (i << 1) | 1; // i * 2 + 1
int r = (i << 1) + 2;
if (l < h->size && h->data[l] < h->data[i]) {
// comemnt
first = l;
}
if (r < h->size && h->data[r] < h->data[first]) {
first = r;
}
if (first != i) {
int a = h->data[first];
h->data[first] = h->data[i];
h->data[i] = a;
minheapint_heapify(h, first);
}
}
void minheapint_insert(MinHeapInt *h, int e) {
h->data[h->size] = e;
h->size++;
if (h->size > 1) {
for (int i = h->size / 2 - 1; i >= 0; i--) minheapint_heapify(h, i);
}
}
int minheapint_extract(MinHeapInt *h) {
//
if (h->size > 0) {
int res = h->data[0];
h->size--;
h->data[0] = h->data[h->size];
minheapint_heapify(h, 0);
return res;
}
return -1;
}
static int block_size = 512;
typedef struct {
// compare(a, b) must return less than, equal, greater than if a is lt, eq, gt
// b, respectively. Then the order is ascending: thus, it is a min-heap.
// The order defines preceeding and subsequent element: first preceeds second
// and third, and so on. The property of the heap is: for each node that
// has children the parent preceeds its children.
__compar_fn_t compare;
// here are the datas as void *
void **data;
int size;
int capacity;
} Heap;
void heap_print(Heap *h, char *(*format)(const void *)) {
// print infos
printf("HEAP size: %d ; capacity: %d\n", h->size, h->capacity);
for (int i = 0; i < h->size; i++) {
char *f = format(h->data[i]);
printf("[%d]: %s\n", i, f);
free(f);
}
}
// this is used to reppresent element and its position in the heap
// for convenience to search the next
typedef struct {
int pos;
void *data;
} ElemPos;
Heap *heap_create(int (*compare)(const void *, const void *), int capacity) {
Heap *h = malloc(sizeof(Heap));
*h = (Heap){
.compare = compare,
.capacity = capacity,
.data = calloc(sizeof(void *), capacity),
.size = 0,
};
return h;
}
void heap_free(Heap **hr) {
// no pici
Heap *h = *hr;
free(h->data);
free(h);
*hr = NULL;
}
void heap_heapify(Heap *h, int i) {
// comment
int first = i;
int l = (i << 1) | 1; // i * 2 + 1
int r = (i << 1) + 2;
if (l < h->size && h->compare(h->data[l], h->data[i]) < 0) {
// comemnt
first = l;
}
if (r < h->size && h->compare(h->data[r], h->data[first]) < 0) {
first = r;
}
if (first != i) {
void *a = h->data[first];
h->data[first] = h->data[i];
h->data[i] = a;
heap_heapify(h, first);
}
}
void heap_insert(Heap *h, void *data) {
// do something sensible in O(log n)
h->data[h->size] = data;
h->size++;
if (h->size > 1) {
for (int i = h->size / 2 - 1; i >= 0; i--) heap_heapify(h, i);
}
if (h->capacity < h->size + 10) {
// che hai?
h->capacity += block_size;
h->data = realloc(h->data, sizeof(void *) * h->capacity);
}
}
void *heap_get_next(Heap *h, void *data) {
// do something sensible in O(log n)
printf("N: %d data %p", h->size, data);
return NULL;
}
void *heap_get_prev(Heap *h, void *data) {
// do something sensible in O(log n)
printf("N: %d data %p", h->size, data);
return NULL;
}
void *heap_rm_elem(Heap *h, void *data) {
// do something sensible in O(log n)
printf("N: %d data %p", h->size, data);
return NULL;
}
void *heap_extract(Heap *h) {
// do something sensible in O(1)
// Not possible: O(log n)
if (h->size > 0) {
void *res = h->data[0];
h->size--;
h->data[0] = h->data[h->size];
heap_heapify(h, 0);
return res;
}
return NULL;
}
int heap_find_elem_pos(Heap *h, void *data) {
// assuming min level equal 1
int start = 0;
int l = 1;
for (int span = 1; span <= h->size; span *= 2) {
// or it can be multiplied by 2 ...
int all_after = 1;
// after in the order defined by compare
for (int i = start; i < l; i++) {
//
int cmp = h->compare(h->data[i], data);
if (cmp == 0) {
return i;
}
if (cmp < 0) {
all_after = 0;
// a block to include: [i*2, i*2+1] and descendants
// it can be used an heap to store those indexes ...
}
}
// when are all_after, and the element was not found in preceeding elements,
// then it does not exists
if (all_after) {
return -1;
}
start = span;
l++;
}
return -1;
}
int heap_find_elem_pos3(Heap *h, void *data) {
// The supporting data structure should have as key the same key (to extract
// the min) and the index position as satellite information, to return the pos
Heap *h1 = heap_create(smallerthan_aug, h->size);
augmented *a = augm_create(h->data[0], 0);
heap_insert(h1, a);
int step = 0;
while (h1->size > 0) {
step++;
augmented *tocmp = heap_extract(h1);
int idx = tocmp->pos;
int cmp = h->compare(h->data[idx], data);
#ifdef PRINT_FULLNESS
printf("at pos %d .. el: %d cmp: %d Fullness: %d\n", idx,
*(int *)h->data[idx], cmp, h1->size);
#endif
if (cmp == 0) {
heap_free(&h1);
printf("STEPS: %d\n", step);
return idx;
}
if (cmp < 0) {
// insert in h1 the children
int left_i = (idx)*2 + 1;
int right_i = (idx)*2 + 2;
if (left_i < h->size && h->compare(h->data[left_i], data) <= 0) {
#ifdef PRINT_FULLNESS
printf("inserting %d\n", left_i);
#endif
augmented *a = augm_create(h->data[left_i], left_i);
heap_insert(h1, a);
}
if (right_i < h->size && h->compare(h->data[right_i], data) <= 0) {
#ifdef PRINT_FULLNESS
printf("inserting %d\n", right_i);
#endif
augmented *a = augm_create(h->data[right_i], right_i);
heap_insert(h1, a);
}
}
}
heap_free(&h1);
return -1;
}
int heap_find_elem_pos2(Heap *h, void *data) {
// The supporting data structure should have as key the same key (to extract
// the min) and the index position as satellite information, to return the pos
MinHeapInt *h1 = minheapint_create(h->size);
minheapint_insert(h1, 0);
int step = 0;
while (h1->size > 0) {
step++;
int idx = minheapint_extract(h1);
int cmp = h->compare(h->data[idx], data);
#ifdef PRINT_FULLNESS
printf("at pos %d .. el: %d cmp: %d Fullness: %d\n", idx,
*(int *)h->data[idx], cmp, h1->size);
#endif
if (cmp == 0) {
minheapint_free(&h1);
printf("STEPS: %d\n", step);
return idx;
}
if (cmp < 0) {
// insert in h1 the children
int left_i = (idx)*2 + 1;
int right_i = (idx)*2 + 2;
if (left_i < h->size && h->compare(h->data[left_i], data) <= 0) {
#ifdef PRINT_FULLNESS
printf("inserting %d\n", left_i);
#endif
minheapint_insert(h1, left_i);
}
if (right_i < h->size && h->compare(h->data[right_i], data) <= 0) {
#ifdef PRINT_FULLNESS
printf("inserting %d\n", right_i);
#endif
minheapint_insert(h1, right_i);
}
}
}
minheapint_free(&h1);
return -1;
}
inline void *heap_at_pos(Heap *h, int pos) {
// return element at pos
return h->data[pos];
}
char *formatint(const void *data) {
// data
int a1 = *(int *)data;
char *out = (char *)malloc(12 * sizeof(char));
sprintf(out, "%d", a1);
return out;
}
// benchmarking
// How long time is needed to create an heap of 100k elements? (random)
// How long is needed to create an arra of 100k, the order it?
// Hint: stable input, so generate a file and read it
int main_no() {
Heap *h = heap_create(biggerthan, 100);
int *e = (int *)malloc(sizeof(int));
*e = 190;
heap_insert(h, (void *)e);
int a = 9, b = 10, c = 45; //, d=22, e=11, f=5, g=7, h=1;
heap_insert(h, &a);
heap_print(h, formatint);
heap_insert(h, &b);
heap_print(h, formatint);
heap_insert(h, &c);
heap_print(h, formatint);
int B[] = {11, 22, 33, 44, 55, 66, 77, 64};
for (int i = 0; i < 8; i++) {
heap_insert(h, &B[i]);
}
printf("prova heap\n");
heap_print(h, formatint);
int pose = heap_find_elem_pos3(h, &a);
printf("position of %d is %d\n", a, pose);
int pose2 = heap_find_elem_pos2(h, &a);
printf("2position of %d is %d\n", a, pose2);
heap_free(&h);
return 0;
}