2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000-2005 Ivo Timmermans,
5 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
6 2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
24 Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
25 instead of depths, to add the ->next and ->prev and to generally obfuscate
26 the code. Mail me if you found a bug.
28 Cleaned up and incorporated some of the ideas from the red-black tree
29 library for inclusion into tinc (http://www.tinc-vpn.org/) by
30 Guus Sliepen <guus@tinc-vpn.org>.
39 #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
40 #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
41 #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
42 #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
46 #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
47 #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
48 #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
49 #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
53 static int lg(unsigned int u) __attribute__ ((__const__));
55 static int lg(unsigned int u)
89 /* Internal helper functions */
91 static int avl_check_balance(const avl_node_t *node)
96 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
98 return d < -1 ? -1 : d > 1 ? 1 : 0;
101 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
102 * d = d<-1?-1:d>1?1:0;
106 pl = lg(AVL_L_COUNT(node));
107 r = AVL_R_COUNT(node);
112 if(pl < 2 || r >> pl - 2)
119 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
124 avl_node_t **superparent;
129 parent = node->parent;
133 parent->left ? &parent->left : &parent->right : &tree->root;
135 switch (avl_check_balance(node)) {
139 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
141 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
143 node->left = child->right;
145 node->left->parent = node;
148 node->parent = child;
149 *superparent = child;
150 child->parent = parent;
152 node->count = AVL_CALC_COUNT(node);
153 child->count = AVL_CALC_COUNT(child);
156 node->depth = AVL_CALC_DEPTH(node);
157 child->depth = AVL_CALC_DEPTH(child);
160 gchild = child->right;
161 node->left = gchild->right;
164 node->left->parent = node;
165 child->right = gchild->left;
168 child->right->parent = child;
169 gchild->right = node;
172 gchild->right->parent = gchild;
173 gchild->left = child;
176 gchild->left->parent = gchild;
177 *superparent = gchild;
179 gchild->parent = parent;
181 node->count = AVL_CALC_COUNT(node);
182 child->count = AVL_CALC_COUNT(child);
183 gchild->count = AVL_CALC_COUNT(gchild);
186 node->depth = AVL_CALC_DEPTH(node);
187 child->depth = AVL_CALC_DEPTH(child);
188 gchild->depth = AVL_CALC_DEPTH(gchild);
196 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
198 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
200 node->right = child->left;
202 node->right->parent = node;
204 node->parent = child;
205 *superparent = child;
206 child->parent = parent;
208 node->count = AVL_CALC_COUNT(node);
209 child->count = AVL_CALC_COUNT(child);
212 node->depth = AVL_CALC_DEPTH(node);
213 child->depth = AVL_CALC_DEPTH(child);
216 gchild = child->left;
217 node->right = gchild->left;
220 node->right->parent = node;
221 child->left = gchild->right;
224 child->left->parent = child;
228 gchild->left->parent = gchild;
229 gchild->right = child;
232 gchild->right->parent = gchild;
234 *superparent = gchild;
235 gchild->parent = parent;
237 node->count = AVL_CALC_COUNT(node);
238 child->count = AVL_CALC_COUNT(child);
239 gchild->count = AVL_CALC_COUNT(gchild);
242 node->depth = AVL_CALC_DEPTH(node);
243 child->depth = AVL_CALC_DEPTH(child);
244 gchild->depth = AVL_CALC_DEPTH(gchild);
251 node->count = AVL_CALC_COUNT(node);
254 node->depth = AVL_CALC_DEPTH(node);
261 /* (De)constructors */
263 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
267 tree = xmalloc_and_zero(sizeof(avl_tree_t));
268 tree->compare = compare;
269 tree->delete = delete;
274 void avl_free_tree(avl_tree_t *tree)
279 avl_node_t *avl_alloc_node(void)
281 return xmalloc_and_zero(sizeof(avl_node_t));
284 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
286 if(node->data && tree->delete)
287 tree->delete(node->data);
294 void *avl_search(const avl_tree_t *tree, const void *data)
298 node = avl_search_node(tree, data);
300 return node ? node->data : NULL;
303 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
307 node = avl_search_closest_node(tree, data, result);
309 return node ? node->data : NULL;
312 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
316 node = avl_search_closest_smaller_node(tree, data);
318 return node ? node->data : NULL;
321 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
325 node = avl_search_closest_greater_node(tree, data);
327 return node ? node->data : NULL;
330 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
335 node = avl_search_closest_node(tree, data, &result);
337 return result ? NULL : node;
340 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
355 c = tree->compare(data, node->data);
383 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
389 node = avl_search_closest_node(tree, data, &result);
397 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
403 node = avl_search_closest_node(tree, data, &result);
411 /* Insertion and deletion */
413 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
415 avl_node_t *closest, *new;
419 new = avl_alloc_node();
421 avl_insert_top(tree, new);
423 closest = avl_search_closest_node(tree, data, &result);
427 new = avl_alloc_node();
429 avl_insert_before(tree, closest, new);
433 new = avl_alloc_node();
435 avl_insert_after(tree, closest, new);
453 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
459 avl_insert_top(tree, node);
461 closest = avl_search_closest_node(tree, node->data, &result);
465 avl_insert_before(tree, closest, node);
469 avl_insert_after(tree, closest, node);
487 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
489 node->prev = node->next = node->parent = NULL;
490 tree->head = tree->tail = tree->root = node;
493 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
498 avl_insert_after(tree, tree->tail, node);
500 avl_insert_top(tree, node);
505 node->parent = before;
506 node->prev = before->prev;
509 avl_insert_after(tree, before->prev, node);
514 before->prev->next = node;
521 avl_rebalance(tree, before);
524 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
528 avl_insert_before(tree, tree->head, node);
530 avl_insert_top(tree, node);
535 avl_insert_before(tree, after->next, node);
540 node->parent = after;
541 node->next = after->next;
544 after->next->prev = node;
551 avl_rebalance(tree, after);
554 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
558 node = avl_search_node(tree, data);
561 avl_unlink_node(tree, node);
566 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
569 avl_node_t **superparent;
570 avl_node_t *subst, *left, *right;
574 node->prev->next = node->next;
576 tree->head = node->next;
578 node->next->prev = node->prev;
580 tree->tail = node->prev;
582 parent = node->parent;
586 parent->left ? &parent->left : &parent->right : &tree->root;
591 *superparent = right;
594 right->parent = parent;
599 left->parent = parent;
607 balnode = subst->parent;
608 balnode->right = subst->left;
611 balnode->right->parent = balnode;
614 left->parent = subst;
617 subst->right = right;
618 subst->parent = parent;
619 right->parent = subst;
620 *superparent = subst;
623 avl_rebalance(tree, balnode);
625 node->next = node->prev = node->parent = node->left = node->right = NULL;
635 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
637 avl_unlink_node(tree, node);
638 avl_free_node(tree, node);
641 void avl_delete(avl_tree_t *tree, void *data)
645 node = avl_search_node(tree, data);
648 avl_delete_node(tree, node);
651 /* Fast tree cleanup */
653 void avl_delete_tree(avl_tree_t *tree)
655 avl_node_t *node, *next;
657 for(node = tree->head; node; node = next) {
659 avl_free_node(tree, node);
667 void avl_foreach(const avl_tree_t *tree, avl_action_t action)
669 avl_node_t *node, *next;
671 for(node = tree->head; node; node = next) {
677 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action)
679 avl_node_t *node, *next;
681 for(node = tree->head; node; node = next) {
690 unsigned int avl_count(const avl_tree_t *tree)
692 return AVL_NODE_COUNT(tree->root);
695 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
703 c = AVL_L_COUNT(node);
707 } else if(index > c) {
718 unsigned int avl_index(const avl_node_t *node)
723 index = AVL_L_COUNT(node);
725 while((next = node->parent)) {
726 if(node == next->right)
727 index += AVL_L_COUNT(next) + 1;
735 unsigned int avl_depth(const avl_tree_t *tree)
737 return AVL_NODE_DEPTH(tree->root);