-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
238 lines (190 loc) · 3.43 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
#include <stdlib.h>
#include "heap.h"
#define sfree(p) safe_free((void**)&(p))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
static heap_node_t *
heap_node_create(heap_key_t k, void *v);
static void
heap_node_destroy(heap_node_t *n);
static void
max_heapify(heap_t *h, size_t i);
static void
safe_free(void **pp);
static heap_node_t *
heap_node_create(heap_key_t k, void *v)
{
heap_node_t *n = malloc(sizeof(heap_node_t));
if (!n)
{
return NULL;
} // end if
n->k = k;
n->v = v;
n->container = NULL;
return n;
} // end heap_node_create()
static void
heap_node_destroy(heap_node_t *n)
{
if (!n)
{
return;
} // end if
if (n->v && n->container && n->container->dtor)
{
n->container->dtor(n->v);
} // end if
sfree(n);
} // end heap_node_destroy()
heap_t *
heap_create(size_t capacity, data_dtor_func_t dtor)
{
if (!dtor)
{
return NULL;
} // end if
size_t _capacity = MAX(HEAP_MIN_CAPACITY, capacity);
heap_t *h = malloc(sizeof(heap_t));
if (!h)
{
return NULL;
} // end if
h->nodes = malloc(sizeof(heap_node_t *) * _capacity);
if (!h->nodes)
{
sfree(h);
return NULL;
} // end if
h->capacity = _capacity;
h->size = 0;
h->dtor = dtor;
return h;
} // end heap_create()
void
heap_destroy(heap_t *h)
{
if (!h)
{
return;
} // end if
if (h->nodes)
{
size_t i = 0;
for (; i < h->size; i++)
{
heap_node_destroy(h->nodes[i]);
} // end for
} // end if
sfree(h);
} // end heap_destory()
int
heap_insert(heap_t *h, heap_key_t k, void *v)
{
if (!h || h->size == h->capacity || !h->nodes)
{
return -1;
} // end if
heap_node_t *n = heap_node_create(k, v);
if (!n)
{
return -1;
} // end if
(h->size)++;
n->container = h;
h->nodes[h->size -1] = n;
size_t i = h->size - 1;
while (i > 0 && h->nodes[(i - 1) / 2]->k < h->nodes[i]->k)
{
heap_node_t *tmp = h->nodes[i];
h->nodes[i] = h->nodes[(i - 1) / 2];
h->nodes[(i - 1) / 2] = tmp;
i = (i - 1) / 2;
} // end while
return 0;
} // end heap_insert()
heap_node_t *
_heap_get_max(heap_t *h)
{
if (!h || !h->nodes)
{
return NULL;
} // end if
return h->nodes[0];
} // end _heap_get_max()
void *
heap_get_max(heap_t *h)
{
heap_node_t *r = _heap_get_max(h);
if (r)
{
return r->v;
} // end if
return NULL;
} // end heap_get_max()
static void
max_heapify(heap_t *h, size_t i)
{
if (!h || !h->nodes)
{
return;
} // end if
size_t l = 2 * i + 1;
size_t r = 2 * i + 2; // index problem???
size_t largest = i;
if (l <= h->size && h->nodes[l] > h->nodes[r])
{
largest = l;
} // end if
if (r <= h->size && h->nodes[r] > h->nodes[largest])
{
largest = r;
} // end if
if (largest != i)
{
heap_node_t *tmp = h->nodes[i];
h->nodes[i] = h->nodes[largest];
h->nodes[largest] = tmp;
max_heapify(h, largest);
} // end if
} // end max_heapify()
void
heap_destroy_max(heap_t *h)
{
if (!h || !h->nodes || !h->size)
{
return;
} // end if
heap_node_destroy(h->nodes[0]);
h->nodes[0] = h->nodes[h->size - 1];
h->nodes[h->size - 1] = NULL;
(h->size)--;
max_heapify(h, 0);
return;
} // end heap_destory_max()
size_t
heap_get_size(heap_t *h)
{
if (!h)
{
return -1;
} // end if
return h->size;
} // end heap_get_size()
size_t
heap_get_capacity(heap_t *h)
{
if (!h)
{
return -1;
} // end if
return h->capacity;
} // end heap_get_capacity()
static void
safe_free(void **pp)
{
if (pp != NULL && *pp != NULL)
{
free(*pp);
*pp = NULL;
} // end if
} // end safe_free()