forked from fragglet/c-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinomial-heap.c
More file actions
477 lines (333 loc) · 9.81 KB
/
binomial-heap.c
File metadata and controls
477 lines (333 loc) · 9.81 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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
Copyright (c) 2005-2008, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "binomial-heap.h"
/* malloc() / free() testing */
#ifdef ALLOC_TESTING
#include "alloc-testing.h"
#endif
typedef struct _BinomialTree BinomialTree;
struct _BinomialTree
{
BinomialHeapValue value;
unsigned short order;
unsigned short refcount;
BinomialTree **subtrees;
};
struct _BinomialHeap
{
BinomialHeapType heap_type;
BinomialHeapCompareFunc compare_func;
unsigned int num_values;
BinomialTree **roots;
unsigned int roots_length;
};
static int binomial_heap_cmp(BinomialHeap *heap,
BinomialHeapValue data1,
BinomialHeapValue data2)
{
if (heap->heap_type == BINOMIAL_HEAP_TYPE_MIN) {
return heap->compare_func(data1, data2);
} else {
return -(heap->compare_func(data1, data2));
}
}
static void binomial_tree_ref(BinomialTree *tree)
{
if (tree != NULL) {
++tree->refcount;
}
}
static void binomial_tree_unref(BinomialTree *tree)
{
int i;
if (tree == NULL) {
return;
}
/* Subtract a reference */
--tree->refcount;
/* If this removed the last reference, unreference all subtrees
* and free. */
if (tree->refcount == 0) {
for (i=0; i<tree->order; ++i) {
binomial_tree_unref(tree->subtrees[i]);
}
free(tree->subtrees);
free(tree);
}
}
static BinomialTree *binomial_tree_merge(BinomialHeap *heap,
BinomialTree *tree1,
BinomialTree *tree2)
{
BinomialTree *new_tree;
BinomialTree *tmp;
int i;
/* Order tree1 and tree2 so that tree1 is the tree with the
* smallest root */
if (binomial_heap_cmp(heap, tree1->value, tree2->value) > 0) {
/* Swap tree1 and tree2 */
tmp = tree1;
tree1 = tree2;
tree2 = tmp;
}
/* Allocate a new tree */
new_tree = malloc(sizeof(BinomialTree));
if (new_tree == NULL) {
return NULL;
}
new_tree->refcount = 0;
new_tree->order = (unsigned short) (tree1->order + 1);
/* Take the smallest value of the two trees */
new_tree->value = tree1->value;
/* Copy subtrees of the smallest tree. The last entry in the
* array is the larger tree */
new_tree->subtrees = malloc(sizeof(BinomialTree *) * new_tree->order);
if (new_tree->subtrees == NULL) {
free(new_tree);
return NULL;
}
memcpy(new_tree->subtrees, tree1->subtrees,
sizeof(BinomialTree *) * tree1->order);
new_tree->subtrees[new_tree->order - 1] = tree2;
/* Add a reference to each of the subtrees we have referenced */
for (i=0; i<new_tree->order; ++i) {
binomial_tree_ref(new_tree->subtrees[i]);
}
return new_tree;
}
/* Used to perform an "undo" when an error occurs during
* binomial_heap_merge. Go through the list of roots so far and remove
* references that have been added. */
static void binomial_heap_merge_undo(BinomialTree **new_roots,
unsigned int count)
{
unsigned int i;
for (i=0; i<=count; ++i) {
binomial_tree_unref(new_roots[i]);
}
free(new_roots);
}
/* Merge the data in the 'other' heap into the 'heap' heap.
* Returns non-zero if successful. */
static int binomial_heap_merge(BinomialHeap *heap, BinomialHeap *other)
{
BinomialTree **new_roots;
unsigned int new_roots_length;
BinomialTree *vals[3];
int num_vals;
BinomialTree *carry;
BinomialTree *new_carry;
unsigned int max;
unsigned int i;
/* Find the maximum length of the two heaps. Add one because
* after merging we may have one more value to carry over. */
if (heap->roots_length > other->roots_length) {
max = heap->roots_length + 1;
} else {
max = other->roots_length + 1;
}
/* Allocate an array for the new roots */
new_roots = malloc(sizeof(BinomialTree *) * max);
if (new_roots == NULL) {
return 0;
}
/* Go through one entry at a time. This works kind of like a
* ripple-carry adder. */
new_roots_length = 0;
carry = NULL;
for (i=0; i<max; ++i) {
/* Build up 'vals' as a list of all the values we must
* merge at this step. */
num_vals = 0;
/* If there is a value in 'heap', add it */
if (i < heap->roots_length && heap->roots[i] != NULL) {
vals[num_vals] = heap->roots[i];
++num_vals;
}
/* If there is a value in 'other', add it */
if (i < other->roots_length && other->roots[i] != NULL) {
vals[num_vals] = other->roots[i];
++num_vals;
}
/* If there is a carried value from the previous iteration,
* add it */
if (carry != NULL) {
vals[num_vals] = carry;
++num_vals;
}
/* When num_vals == 1 or 3, we store a value. */
if ((num_vals & 1) != 0) {
/* Save the last value into new_roots. */
new_roots[i] = vals[num_vals - 1];
binomial_tree_ref(new_roots[i]);
new_roots_length = i + 1;
} else {
/* No value to store at this iteration */
new_roots[i] = NULL;
}
/* When num_vals == 2 or 3, we must carry over to the
* next iteration */
if ((num_vals & 2) != 0) {
/* Merge the first two values and carry to the
* next iteration */
new_carry = binomial_tree_merge(heap,
vals[0],
vals[1]);
if (new_carry == NULL) {
/* Remove references that we have added
* (freeing any BinomialTree structures
* that were created in the process) */
binomial_heap_merge_undo(new_roots, i);
/* Unreference the carry variable */
binomial_tree_unref(carry);
return 0;
}
} else {
/* Nothing to carry */
new_carry = NULL;
}
/* Unreference previous carried value */
binomial_tree_unref(carry);
/* Assign the new value of carry, and add a reference */
carry = new_carry;
binomial_tree_ref(carry);
}
/* Unreference all values in the old 'roots' array, freeing unused
* BinomialTree structures as necessary. */
for (i=0; i<heap->roots_length; ++i) {
if (heap->roots[i] != NULL) {
binomial_tree_unref(heap->roots[i]);
}
}
/* Free the old roots array and use the new one */
free(heap->roots);
heap->roots = new_roots;
heap->roots_length = new_roots_length;
/* Merged successfully */
return 1;
}
BinomialHeap *binomial_heap_new(BinomialHeapType heap_type,
BinomialHeapCompareFunc compare_func)
{
BinomialHeap *new_heap;
/* Allocate a new heap */
new_heap = calloc(1, sizeof(BinomialHeap));
if (new_heap == NULL) {
return NULL;
}
/* Initialise and return */
new_heap->heap_type = heap_type;
new_heap->compare_func = compare_func;
return new_heap;
}
void binomial_heap_free(BinomialHeap *heap)
{
unsigned int i;
/* Unreference all trees in the heap. This should free
* back all subtrees. */
for (i=0; i<heap->roots_length; ++i) {
binomial_tree_unref(heap->roots[i]);
}
/* Free the heap itself */
free(heap->roots);
free(heap);
}
int binomial_heap_insert(BinomialHeap *heap, BinomialHeapValue value)
{
BinomialHeap fake_heap;
BinomialTree *new_tree;
int result;
/* Allocate an order 0 tree for storing the new value */
new_tree = malloc(sizeof(BinomialTree));
if (new_tree == NULL) {
return 0;
}
/* Fill in values. This has an initial reference count of 1 that
* the "fake" heap holds; this will be removed at the end of
* this function. */
new_tree->value = value;
new_tree->order = 0;
new_tree->refcount = 1;
new_tree->subtrees = NULL;
/* Build a fake heap structure for merging */
fake_heap.heap_type = heap->heap_type;
fake_heap.compare_func = heap->compare_func;
fake_heap.num_values = 1;
fake_heap.roots = &new_tree;
fake_heap.roots_length = 1;
/* Perform the merge */
result = binomial_heap_merge(heap, &fake_heap);
if (result != 0) {
++heap->num_values;
}
/* Remove reference to the new tree. */
binomial_tree_unref(new_tree);
return result;
}
BinomialHeapValue binomial_heap_pop(BinomialHeap *heap)
{
BinomialTree *least_tree;
BinomialHeap fake_heap;
BinomialHeapValue result;
unsigned int i;
unsigned int least_index;
if (heap->num_values == 0) {
return BINOMIAL_HEAP_NULL;
}
/* Find the tree with the lowest root value */
least_index = UINT_MAX;
for (i=0; i<heap->roots_length; ++i) {
if (heap->roots[i] == NULL) {
continue;
}
if (least_index == UINT_MAX
|| binomial_heap_cmp(heap,
heap->roots[i]->value,
heap->roots[least_index]->value) < 0) {
least_index = i;
}
}
/* Remove the least_tree from the heap. */
least_tree = heap->roots[least_index];
heap->roots[least_index] = NULL;
/* Construct a fake heap containing the data in the least tree */
fake_heap.heap_type = heap->heap_type;
fake_heap.compare_func = heap->compare_func;
fake_heap.roots = least_tree->subtrees;
fake_heap.roots_length = least_tree->order;
/* Merge subtrees of least tree back into the heap */
if (binomial_heap_merge(heap, &fake_heap)) {
/* Merge successful */
/* Remove reference to least tree */
result = least_tree->value;
binomial_tree_unref(least_tree);
/* Update the number of values */
--heap->num_values;
return result;
} else {
/* Add the least tree back */
heap->roots[least_index] = least_tree;
/* Pop failed */
return BINOMIAL_HEAP_NULL;
}
}
unsigned int binomial_heap_num_entries(BinomialHeap *heap)
{
return heap->num_values;
}