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)
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;
127 parent = node->parent;
131 parent->left ? &parent->left : &parent->right : &tree->root;
133 switch (avl_check_balance(node)) {
137 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
139 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
141 node->left = child->right;
143 node->left->parent = node;
146 node->parent = child;
147 *superparent = child;
148 child->parent = parent;
150 node->count = AVL_CALC_COUNT(node);
151 child->count = AVL_CALC_COUNT(child);
154 node->depth = AVL_CALC_DEPTH(node);
155 child->depth = AVL_CALC_DEPTH(child);
158 gchild = child->right;
159 node->left = gchild->right;
162 node->left->parent = node;
163 child->right = gchild->left;
166 child->right->parent = child;
167 gchild->right = node;
169 gchild->right->parent = gchild;
170 gchild->left = child;
172 gchild->left->parent = gchild;
174 *superparent = gchild;
175 gchild->parent = parent;
177 node->count = AVL_CALC_COUNT(node);
178 child->count = AVL_CALC_COUNT(child);
179 gchild->count = AVL_CALC_COUNT(gchild);
182 node->depth = AVL_CALC_DEPTH(node);
183 child->depth = AVL_CALC_DEPTH(child);
184 gchild->depth = AVL_CALC_DEPTH(gchild);
192 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
194 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
196 node->right = child->left;
198 node->right->parent = node;
200 node->parent = child;
201 *superparent = child;
202 child->parent = parent;
204 node->count = AVL_CALC_COUNT(node);
205 child->count = AVL_CALC_COUNT(child);
208 node->depth = AVL_CALC_DEPTH(node);
209 child->depth = AVL_CALC_DEPTH(child);
212 gchild = child->left;
213 node->right = gchild->left;
216 node->right->parent = node;
217 child->left = gchild->right;
220 child->left->parent = child;
223 gchild->left->parent = gchild;
224 gchild->right = child;
226 gchild->right->parent = gchild;
228 *superparent = gchild;
229 gchild->parent = parent;
231 node->count = AVL_CALC_COUNT(node);
232 child->count = AVL_CALC_COUNT(child);
233 gchild->count = AVL_CALC_COUNT(gchild);
236 node->depth = AVL_CALC_DEPTH(node);
237 child->depth = AVL_CALC_DEPTH(child);
238 gchild->depth = AVL_CALC_DEPTH(gchild);
245 node->count = AVL_CALC_COUNT(node);
248 node->depth = AVL_CALC_DEPTH(node);
255 /* (De)constructors */
257 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
261 tree = xmalloc_and_zero(sizeof(avl_tree_t));
262 tree->compare = compare;
263 tree->delete = delete;
268 void avl_free_tree(avl_tree_t *tree)
273 avl_node_t *avl_alloc_node(void)
275 return xmalloc_and_zero(sizeof(avl_node_t));
278 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
280 if(node->data && tree->delete)
281 tree->delete(node->data);
288 void *avl_search(const avl_tree_t *tree, const void *data)
292 node = avl_search_node(tree, data);
294 return node ? node->data : NULL;
297 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
301 node = avl_search_closest_node(tree, data, result);
303 return node ? node->data : NULL;
306 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
310 node = avl_search_closest_smaller_node(tree, data);
312 return node ? node->data : NULL;
315 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
319 node = avl_search_closest_greater_node(tree, data);
321 return node ? node->data : NULL;
324 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
329 node = avl_search_closest_node(tree, data, &result);
331 return result ? NULL : node;
334 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
349 c = tree->compare(data, node->data);
377 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
383 node = avl_search_closest_node(tree, data, &result);
391 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
397 node = avl_search_closest_node(tree, data, &result);
405 /* Insertion and deletion */
407 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
409 avl_node_t *closest, *new;
413 new = avl_alloc_node();
415 avl_insert_top(tree, new);
417 closest = avl_search_closest_node(tree, data, &result);
421 new = avl_alloc_node();
423 avl_insert_before(tree, closest, new);
427 new = avl_alloc_node();
429 avl_insert_after(tree, closest, new);
447 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
453 avl_insert_top(tree, node);
455 closest = avl_search_closest_node(tree, node->data, &result);
459 avl_insert_before(tree, closest, node);
463 avl_insert_after(tree, closest, node);
481 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
483 node->prev = node->next = node->parent = NULL;
484 tree->head = tree->tail = tree->root = node;
487 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
492 avl_insert_after(tree, tree->tail, node);
494 avl_insert_top(tree, node);
499 node->parent = before;
500 node->prev = before->prev;
503 avl_insert_after(tree, before->prev, node);
508 before->prev->next = node;
515 avl_rebalance(tree, before);
518 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
522 avl_insert_before(tree, tree->head, node);
524 avl_insert_top(tree, node);
529 avl_insert_before(tree, after->next, node);
534 node->parent = after;
535 node->next = after->next;
538 after->next->prev = node;
545 avl_rebalance(tree, after);
548 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
552 node = avl_search_node(tree, data);
555 avl_unlink_node(tree, node);
560 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
563 avl_node_t **superparent;
564 avl_node_t *subst, *left, *right;
568 node->prev->next = node->next;
570 tree->head = node->next;
572 node->next->prev = node->prev;
574 tree->tail = node->prev;
576 parent = node->parent;
580 parent->left ? &parent->left : &parent->right : &tree->root;
585 *superparent = right;
588 right->parent = parent;
593 left->parent = parent;
597 if(!subst) // This only happens if node is not actually in a tree at all.
603 balnode = subst->parent;
604 balnode->right = subst->left;
607 balnode->right->parent = balnode;
610 left->parent = subst;
613 subst->right = right;
614 subst->parent = parent;
615 right->parent = subst;
616 *superparent = subst;
619 avl_rebalance(tree, balnode);
621 node->next = node->prev = node->parent = node->left = node->right = NULL;
631 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
633 avl_unlink_node(tree, node);
634 avl_free_node(tree, node);
637 void avl_delete(avl_tree_t *tree, void *data)
641 node = avl_search_node(tree, data);
644 avl_delete_node(tree, node);
647 /* Fast tree cleanup */
649 void avl_delete_tree(avl_tree_t *tree)
651 avl_node_t *node, *next;
653 for(node = tree->head; node; node = next) {
655 avl_free_node(tree, node);
663 void avl_foreach(const avl_tree_t *tree, avl_action_t action)
665 avl_node_t *node, *next;
667 for(node = tree->head; node; node = next) {
673 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action)
675 avl_node_t *node, *next;
677 for(node = tree->head; node; node = next) {
686 unsigned int avl_count(const avl_tree_t *tree)
688 return AVL_NODE_COUNT(tree->root);
691 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
699 c = AVL_L_COUNT(node);
703 } else if(index > c) {
714 unsigned int avl_index(const avl_node_t *node)
719 index = AVL_L_COUNT(node);
721 while((next = node->parent)) {
722 if(node == next->right)
723 index += AVL_L_COUNT(next) + 1;
731 unsigned int avl_depth(const avl_tree_t *tree)
733 return AVL_NODE_DEPTH(tree->root);