2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000-2005 Ivo Timmermans,
5 2000-2015 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 (https://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) {
90 /* Internal helper functions */
92 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);
113 if(pl < 2 || r >> pl - 2) {
121 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node) {
125 avl_node_t **superparent;
128 parent = node->parent;
132 parent->left ? &parent->left : &parent->right : &tree->root;
134 switch(avl_check_balance(node)) {
139 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
142 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
144 node->left = child->right;
147 node->left->parent = node;
151 node->parent = child;
152 *superparent = child;
153 child->parent = parent;
155 node->count = AVL_CALC_COUNT(node);
156 child->count = AVL_CALC_COUNT(child);
159 node->depth = AVL_CALC_DEPTH(node);
160 child->depth = AVL_CALC_DEPTH(child);
163 gchild = child->right;
164 node->left = gchild->right;
167 node->left->parent = node;
170 child->right = gchild->left;
173 child->right->parent = child;
176 gchild->right = node;
178 gchild->right->parent = gchild;
179 gchild->left = child;
181 gchild->left->parent = gchild;
183 *superparent = gchild;
184 gchild->parent = parent;
186 node->count = AVL_CALC_COUNT(node);
187 child->count = AVL_CALC_COUNT(child);
188 gchild->count = AVL_CALC_COUNT(gchild);
191 node->depth = AVL_CALC_DEPTH(node);
192 child->depth = AVL_CALC_DEPTH(child);
193 gchild->depth = AVL_CALC_DEPTH(gchild);
203 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
206 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
208 node->right = child->left;
211 node->right->parent = node;
215 node->parent = child;
216 *superparent = child;
217 child->parent = parent;
219 node->count = AVL_CALC_COUNT(node);
220 child->count = AVL_CALC_COUNT(child);
223 node->depth = AVL_CALC_DEPTH(node);
224 child->depth = AVL_CALC_DEPTH(child);
227 gchild = child->left;
228 node->right = gchild->left;
231 node->right->parent = node;
234 child->left = gchild->right;
237 child->left->parent = child;
242 gchild->left->parent = gchild;
243 gchild->right = child;
245 gchild->right->parent = gchild;
247 *superparent = gchild;
248 gchild->parent = parent;
250 node->count = AVL_CALC_COUNT(node);
251 child->count = AVL_CALC_COUNT(child);
252 gchild->count = AVL_CALC_COUNT(gchild);
255 node->depth = AVL_CALC_DEPTH(node);
256 child->depth = AVL_CALC_DEPTH(child);
257 gchild->depth = AVL_CALC_DEPTH(gchild);
265 node->count = AVL_CALC_COUNT(node);
268 node->depth = AVL_CALC_DEPTH(node);
276 /* (De)constructors */
278 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete) {
281 tree = xmalloc_and_zero(sizeof(avl_tree_t));
282 tree->compare = compare;
283 tree->delete = delete;
288 void avl_free_tree(avl_tree_t *tree) {
292 avl_node_t *avl_alloc_node(void) {
293 return xmalloc_and_zero(sizeof(avl_node_t));
296 void avl_free_node(avl_tree_t *tree, avl_node_t *node) {
297 if(node->data && tree->delete) {
298 tree->delete(node->data);
306 void *avl_search(const avl_tree_t *tree, const void *data) {
309 node = avl_search_node(tree, data);
311 return node ? node->data : NULL;
314 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result) {
317 node = avl_search_closest_node(tree, data, result);
319 return node ? node->data : NULL;
322 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data) {
325 node = avl_search_closest_smaller_node(tree, data);
327 return node ? node->data : NULL;
330 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data) {
333 node = avl_search_closest_greater_node(tree, data);
335 return node ? node->data : NULL;
338 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data) {
342 node = avl_search_closest_node(tree, data, &result);
344 return result ? NULL : node;
347 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
363 c = tree->compare(data, node->data);
397 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
402 node = avl_search_closest_node(tree, data, &result);
411 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
416 node = avl_search_closest_node(tree, data, &result);
425 /* Insertion and deletion */
427 avl_node_t *avl_insert(avl_tree_t *tree, void *data) {
428 avl_node_t *closest, *new;
432 new = avl_alloc_node();
434 avl_insert_top(tree, new);
436 closest = avl_search_closest_node(tree, data, &result);
440 new = avl_alloc_node();
442 avl_insert_before(tree, closest, new);
446 new = avl_alloc_node();
448 avl_insert_after(tree, closest, new);
466 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node) {
471 avl_insert_top(tree, node);
473 closest = avl_search_closest_node(tree, node->data, &result);
477 avl_insert_before(tree, closest, node);
481 avl_insert_after(tree, closest, node);
499 void avl_insert_top(avl_tree_t *tree, avl_node_t *node) {
500 node->prev = node->next = node->parent = NULL;
501 tree->head = tree->tail = tree->root = node;
504 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
508 avl_insert_after(tree, tree->tail, node);
510 avl_insert_top(tree, node);
517 node->parent = before;
518 node->prev = before->prev;
521 avl_insert_after(tree, before->prev, node);
526 before->prev->next = node;
534 avl_rebalance(tree, before);
537 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node) {
540 avl_insert_before(tree, tree->head, node);
542 avl_insert_top(tree, node);
549 avl_insert_before(tree, after->next, node);
554 node->parent = after;
555 node->next = after->next;
558 after->next->prev = node;
566 avl_rebalance(tree, after);
569 avl_node_t *avl_unlink(avl_tree_t *tree, void *data) {
572 node = avl_search_node(tree, data);
575 avl_unlink_node(tree, node);
581 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node) {
583 avl_node_t **superparent;
584 avl_node_t *subst, *left, *right;
588 node->prev->next = node->next;
590 tree->head = node->next;
594 node->next->prev = node->prev;
596 tree->tail = node->prev;
599 parent = node->parent;
603 parent->left ? &parent->left : &parent->right : &tree->root;
609 *superparent = right;
612 right->parent = parent;
618 left->parent = parent;
623 if(!subst) { // This only happens if node is not actually in a tree at all.
630 balnode = subst->parent;
631 balnode->right = subst->left;
634 balnode->right->parent = balnode;
638 left->parent = subst;
641 subst->right = right;
642 subst->parent = parent;
643 right->parent = subst;
644 *superparent = subst;
647 avl_rebalance(tree, balnode);
649 node->next = node->prev = node->parent = node->left = node->right = NULL;
659 void avl_delete_node(avl_tree_t *tree, avl_node_t *node) {
660 avl_unlink_node(tree, node);
661 avl_free_node(tree, node);
664 void avl_delete(avl_tree_t *tree, void *data) {
667 node = avl_search_node(tree, data);
670 avl_delete_node(tree, node);
674 /* Fast tree cleanup */
676 void avl_delete_tree(avl_tree_t *tree) {
677 avl_node_t *node, *next;
679 for(node = tree->head; node; node = next) {
681 avl_free_node(tree, node);
689 void avl_foreach(const avl_tree_t *tree, avl_action_t action) {
690 avl_node_t *node, *next;
692 for(node = tree->head; node; node = next) {
698 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action) {
699 avl_node_t *node, *next;
701 for(node = tree->head; node; node = next) {
710 unsigned int avl_count(const avl_tree_t *tree) {
711 return AVL_NODE_COUNT(tree->root);
714 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index) {
721 c = AVL_L_COUNT(node);
725 } else if(index > c) {
736 unsigned int avl_index(const avl_node_t *node) {
740 index = AVL_L_COUNT(node);
742 while((next = node->parent)) {
743 if(node == next->right) {
744 index += AVL_L_COUNT(next) + 1;
754 unsigned int avl_depth(const avl_tree_t *tree) {
755 return AVL_NODE_DEPTH(tree->root);