-
Notifications
You must be signed in to change notification settings - Fork 2
/
avl.cpp
416 lines (358 loc) · 12.1 KB
/
avl.cpp
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
/*
* avl package
*
* Copyright (c) 1988-1993, The Regents of the University of California.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of the University of California not
* be used in advertising or publicity pertaining to distribution of
* the software without specific, written prior permission. The University
* of California makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* THE UNIVERSITY OF CALIFORNIA DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR
* ANY SPECIAL, 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.
*/
// Modified for Gmsh (C++ and 64 bit compatibility)
#include "GmshConfig.h"
#if !defined(HAVE_NO_STDINT_H)
#include <stdint.h>
#elif defined(HAVE_NO_INTPTR_T)
typedef unsigned long intptr_t;
#endif
#include <stdio.h>
#include "avl.h"
#include "MallocUtils.h"
#define ALLOC(type, number) (type *) Malloc((unsigned) sizeof(type) * number)
#define FREE(item) (void) Free(item)
#define XRNMAX(a,b) ((a) > (b) ? (a) : (b))
#define HEIGHT(node) (node == NIL(avl_node) ? -1 : (node)->height)
#define BALANCE(node) (HEIGHT((node)->right) - HEIGHT((node)->left))
#define compute_height(node) { \
int x=HEIGHT(node->left), y=HEIGHT(node->right); \
(node)->height = XRNMAX(x,y) + 1; \
}
#define COMPARE(key, nodekey, compare) \
((compare == avl_numcmp) ? \
(intptr_t) key - (intptr_t) nodekey : \
(*compare)(key, nodekey))
static void avl_record_gen_forward(avl_node *node, avl_generator *gen);
static void avl_record_gen_backward(avl_node *node, avl_generator *gen);
static avl_node *find_rightmost(avl_node **node_p);
static void do_rebalance(avl_node ***stack_nodep, int stack_n);
static void rotate_left(avl_node **node_p);
static void rotate_right(avl_node **node_p);
static void free_entry(avl_node *node, void (*key_free)(void *key),
void (*value_free)(void *value));
static avl_node *new_node(void *key, void *value);
static int do_check_tree(avl_node *node, int (*compar)(const void *key1, const void *key2),
int *error);
avl_tree *avl_init_table(int (*compar)(const void *key1, const void *key2))
{
avl_tree *tree;
tree = ALLOC(avl_tree, 1);
tree->root = NIL(avl_node);
tree->compar = compar;
tree->num_entries = 0;
return tree;
}
int avl_lookup(avl_tree *tree, void *key, void **value_p)
{
avl_node *node;
int (*compare)(const void*, const void *) = tree->compar, diff;
node = tree->root;
while (node != NIL(avl_node)) {
diff = COMPARE(key, node->key, compare);
if (diff == 0) {
/* got a match, give the user a 'value' only if non-null */
if (value_p != NIL(void *)) *value_p = node->value;
return 1;
}
node = (diff < 0) ? node->left : node->right;
}
return 0;
}
int avl_insert(avl_tree *tree, void *key, void *value)
{
avl_node **node_p, *node;
int stack_n = 0;
int (*compare)(const void*, const void *) = tree->compar;
avl_node **stack_nodep[32];
int diff, status;
node_p = &tree->root;
/* walk down the tree (saving the path); stop at insertion point */
status = 0;
while ((node = *node_p) != NIL(avl_node)) {
stack_nodep[stack_n++] = node_p;
diff = COMPARE(key, node->key, compare);
if (diff == 0) status = 1;
node_p = (diff < 0) ? &node->left : &node->right;
}
/* insert the item and re-balance the tree */
*node_p = new_node(key, value);
do_rebalance(stack_nodep, stack_n);
tree->num_entries++;
tree->modified = 1;
return status;
}
int avl_delete(avl_tree *tree, void **key_p, void **value_p)
{
avl_node **node_p, *node, *rightmost;
int stack_n = 0;
void *key = *key_p;
int (*compare)(const void*, const void*) = tree->compar, diff;
avl_node **stack_nodep[32];
node_p = &tree->root;
/* Walk down the tree saving the path; return if not found */
while ((node = *node_p) != NIL(avl_node)) {
diff = COMPARE(key, node->key, compare);
if (diff == 0) goto delete_item;
stack_nodep[stack_n++] = node_p;
node_p = (diff < 0) ? &node->left : &node->right;
}
return 0; /* not found */
/* prepare to delete node and replace it with rightmost of left tree */
delete_item:
*key_p = node->key;
if (value_p != 0) *value_p = node->value;
if (node->left == NIL(avl_node)) {
*node_p = node->right;
} else {
rightmost = find_rightmost(&node->left);
rightmost->left = node->left;
rightmost->right = node->right;
rightmost->height = -2; /* mark bogus height for do_rebal */
*node_p = rightmost;
stack_nodep[stack_n++] = node_p;
}
FREE(node);
/* work our way back up, re-balancing the tree */
do_rebalance(stack_nodep, stack_n);
tree->num_entries--;
tree->modified = 1;
return 1;
}
static void avl_record_gen_forward(avl_node *node, avl_generator *gen)
{
if (node != NIL(avl_node)) {
avl_record_gen_forward(node->left, gen);
gen->nodelist[gen->count++] = node;
avl_record_gen_forward(node->right, gen);
}
}
static void avl_record_gen_backward(avl_node *node, avl_generator *gen)
{
if (node != NIL(avl_node)) {
avl_record_gen_backward(node->right, gen);
gen->nodelist[gen->count++] = node;
avl_record_gen_backward(node->left, gen);
}
}
avl_generator *avl_init_gen(avl_tree *tree, int dir)
{
avl_generator *gen;
/* what a hack */
gen = ALLOC(avl_generator, 1);
gen->tree = tree;
gen->nodelist = ALLOC(avl_node *, avl_count(tree));
gen->count = 0;
if (dir == AVL_FORWARD) {
avl_record_gen_forward(tree->root, gen);
} else {
avl_record_gen_backward(tree->root, gen);
}
gen->count = 0;
/* catch any attempt to modify the tree while we generate */
tree->modified = 0;
return gen;
}
int avl_gen(avl_generator *gen, void **key_p, void **value_p)
{
avl_node *node;
if (gen->count == gen->tree->num_entries) {
return 0;
} else {
node = gen->nodelist[gen->count++];
if (key_p != NIL(void *)) *key_p = node->key;
if (value_p != NIL(void *)) *value_p = node->value;
return 1;
}
}
void avl_free_gen(avl_generator *gen)
{
FREE(gen->nodelist);
FREE(gen);
}
static avl_node *find_rightmost(avl_node **node_p)
{
avl_node *node;
int stack_n = 0;
avl_node **stack_nodep[32];
node = *node_p;
while (node->right != NIL(avl_node)) {
stack_nodep[stack_n++] = node_p;
node_p = &node->right;
node = *node_p;
}
*node_p = node->left;
do_rebalance(stack_nodep, stack_n);
return node;
}
static void do_rebalance(avl_node ***stack_nodep, int stack_n)
{
avl_node **node_p, *node;
int hl, hr;
int height;
/* work our way back up, re-balancing the tree */
while (--stack_n >= 0) {
node_p = stack_nodep[stack_n];
node = *node_p;
hl = HEIGHT(node->left); /* watch for NIL */
hr = HEIGHT(node->right); /* watch for NIL */
if ((hr - hl) < -1) {
rotate_right(node_p);
} else if ((hr - hl) > 1) {
rotate_left(node_p);
} else {
height = XRNMAX(hl, hr) + 1;
if (height == node->height) break;
node->height = height;
}
}
}
static void rotate_left(avl_node **node_p)
{
avl_node *old_root = *node_p, *new_root, *new_right;
if (BALANCE(old_root->right) >= 0) {
*node_p = new_root = old_root->right;
old_root->right = new_root->left;
new_root->left = old_root;
} else {
new_right = old_root->right;
*node_p = new_root = new_right->left;
old_root->right = new_root->left;
new_right->left = new_root->right;
new_root->right = new_right;
new_root->left = old_root;
compute_height(new_right);
}
compute_height(old_root);
compute_height(new_root);
}
static void rotate_right(avl_node **node_p)
{
avl_node *old_root = *node_p, *new_root, *new_left;
if (BALANCE(old_root->left) <= 0) {
*node_p = new_root = old_root->left;
old_root->left = new_root->right;
new_root->right = old_root;
} else {
new_left = old_root->left;
*node_p = new_root = new_left->right;
old_root->left = new_root->right;
new_left->right = new_root->left;
new_root->left = new_left;
new_root->right = old_root;
compute_height(new_left);
}
compute_height(old_root);
compute_height(new_root);
}
int avl_extremum(avl_tree *tree, int side, void **value_p)
{
avl_node *node;
node = tree->root;
if (node == NIL(avl_node)) return 0;
if (side == AVL_MOST_LEFT)
while (node->left != NIL(avl_node)) node = node->left;
else
while (node->right != NIL(avl_node)) node = node->right;
if (value_p != NIL(void *)) {
*value_p = node->value;
return 1;
}
return 0;
}
static void free_entry(avl_node *node, void (*key_free)(void *key), void (*value_free)(void *value))
{
if (node != NIL(avl_node)) {
free_entry(node->left, key_free, value_free);
free_entry(node->right, key_free, value_free);
if (key_free != 0) (*key_free)(node->key);
if (value_free != 0) (*value_free)(node->value);
FREE(node);
}
}
void avl_free_table(avl_tree *tree, void (*key_free)(void *key), void (*value_free)(void *value))
{
free_entry(tree->root, key_free, value_free);
FREE(tree);
}
int avl_count(avl_tree *tree)
{
return tree->num_entries;
}
static avl_node *new_node(void *key, void *value)
{
avl_node *newn;
newn = ALLOC(avl_node, 1);
newn->key = key;
newn->value = value;
newn->height = 0;
newn->left = newn->right = NIL(avl_node);
return newn;
}
int avl_numcmp(const void *x, const void*y)
{
return (intptr_t) x - (intptr_t) y;
}
int avl_check_tree(avl_tree *tree)
{
int error = 0;
(void) do_check_tree(tree->root, tree->compar, &error);
return error;
}
static int do_check_tree(avl_node *node,
int (*compar)(const void *key1, const void *key2), int *error)
{
int l_height, r_height, comp_height, bal;
if (node == NIL(avl_node)) {
return -1;
}
r_height = do_check_tree(node->right, compar, error);
l_height = do_check_tree(node->left, compar, error);
comp_height = XRNMAX(l_height, r_height) + 1;
bal = r_height - l_height;
if (comp_height != node->height) {
(void) printf("Bad height for %p: computed=%d stored=%d\n",
(void*)node, comp_height, node->height);
++*error;
}
if (bal > 1 || bal < -1) {
(void) printf("Out of balance at node %p, balance = %d\n",
(void*)node, bal);
++*error;
}
if (node->left != NIL(avl_node) &&
(*compar)(node->left->key, node->key) > 0) {
(void) printf("Bad ordering between %p and %p",
(void*)node, (void*)node->left);
++*error;
}
if (node->right != NIL(avl_node) &&
(*compar)(node->key, node->right->key) > 0) {
(void) printf("Bad ordering between %p and %p",
(void*)node, (void*)node->right);
++*error;
}
return comp_height;
}