2 * collectd - src/utils_avltree.c
3 * Copyright (C) 2006,2007 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils_avltree.h"
32 #define BALANCE(n) ((((n)->left == NULL) ? 0 : (n)->left->height) \
33 - (((n)->right == NULL) ? 0 : (n)->right->height))
44 struct c_avl_node_s *left;
45 struct c_avl_node_s *right;
46 struct c_avl_node_s *parent;
48 typedef struct c_avl_node_s c_avl_node_t;
53 int (*compare) (const void *, const void *);
57 struct c_avl_iterator_s
67 static void verify_tree (c_avl_node_t *n)
72 verify_tree (n->left);
73 verify_tree (n->right);
75 assert ((BALANCE (n) >= -1) && (BALANCE (n) <= 1));
76 assert ((n->parent == NULL) || (n->parent->right == n) || (n->parent->left == n));
77 } /* void verify_tree */
79 # define verify_tree(n) /**/
82 static void free_node (c_avl_node_t *n)
95 static int calc_height (c_avl_node_t *n)
103 height_left = (n->left == NULL) ? 0 : n->left->height;
104 height_right = (n->right == NULL) ? 0 : n->right->height;
106 return (((height_left > height_right)
108 : height_right) + 1);
109 } /* int calc_height */
111 static c_avl_node_t *search (c_avl_tree_t *t, const void *key)
119 cmp = t->compare (key, n->key);
134 * / \ /_c\ ==> / a\ / \
136 * / a\ /_b\ /_b\ /_c\
139 static c_avl_node_t *rotate_right (c_avl_tree_t *t, c_avl_node_t *x)
146 assert (x->left != NULL);
160 assert ((p == NULL) || (p->left == x) || (p->right == x));
163 else if (p->left == x)
168 x->height = calc_height (x);
169 y->height = calc_height (y);
172 } /* void rotate_right */
178 * /_a\ / \ ==> / \ / c\
180 * /_b\ / c\ /_a\ /_b\
183 static c_avl_node_t *rotate_left (c_avl_tree_t *t, c_avl_node_t *x)
190 assert (x->right != NULL);
204 assert ((p == NULL) || (p->left == x) || (p->right == x));
207 else if (p->left == x)
212 x->height = calc_height (x);
213 y->height = calc_height (y);
216 } /* void rotate_left */
218 static c_avl_node_t *rotate_left_right (c_avl_tree_t *t, c_avl_node_t *x)
220 rotate_left (t, x->left);
221 return (rotate_right (t, x));
222 } /* void rotate_left_right */
224 static c_avl_node_t *rotate_right_left (c_avl_tree_t *t, c_avl_node_t *x)
226 rotate_right (t, x->right);
227 return (rotate_left (t, x));
228 } /* void rotate_right_left */
230 static void rebalance (c_avl_tree_t *t, c_avl_node_t *n)
238 assert ((b_top >= -2) && (b_top <= 2));
242 assert (n->right != NULL);
243 b_bottom = BALANCE (n->right);
244 assert ((b_bottom >= -1) && (b_bottom <= 1));
246 n = rotate_right_left (t, n);
248 n = rotate_left (t, n);
252 assert (n->left != NULL);
253 b_bottom = BALANCE (n->left);
254 assert ((b_bottom >= -1) && (b_bottom <= 1));
256 n = rotate_left_right (t, n);
258 n = rotate_right (t, n);
262 int height = calc_height (n);
263 if (height == n->height)
268 assert (n->height == calc_height (n));
271 } /* while (n != NULL) */
272 } /* void rebalance */
274 static c_avl_node_t *c_avl_node_next (c_avl_node_t *n)
276 c_avl_node_t *r; /* return node */
283 /* If we can't descent any further, we have to backtrack to the first
284 * parent that's bigger than we, i. e. who's _left_ child we are. */
285 if (n->right == NULL)
288 while ((r != NULL) && (r->parent != NULL))
296 /* n->right == NULL && r == NULL => t is root and has no next
297 * r->left != n => r->right = n => r->parent == NULL */
298 if ((r == NULL) || (r->left != n))
300 assert ((r == NULL) || (r->parent == NULL));
305 assert (r->left == n);
312 while (r->left != NULL)
317 } /* c_avl_node_t *c_avl_node_next */
319 static c_avl_node_t *c_avl_node_prev (c_avl_node_t *n)
321 c_avl_node_t *r; /* return node */
328 /* If we can't descent any further, we have to backtrack to the first
329 * parent that's smaller than we, i. e. who's _right_ child we are. */
333 while ((r != NULL) && (r->parent != NULL))
341 /* n->left == NULL && r == NULL => t is root and has no next
342 * r->right != n => r->left = n => r->parent == NULL */
343 if ((r == NULL) || (r->right != n))
345 assert ((r == NULL) || (r->parent == NULL));
350 assert (r->right == n);
357 while (r->right != NULL)
362 } /* c_avl_node_t *c_avl_node_prev */
364 static int _remove (c_avl_tree_t *t, c_avl_node_t *n)
366 assert ((t != NULL) && (n != NULL));
368 if ((n->left != NULL) && (n->right != NULL))
370 c_avl_node_t *r; /* replacement node */
371 if (BALANCE (n) > 0) /* left subtree is higher */
373 assert (n->left != NULL);
374 r = c_avl_node_prev (n);
377 else /* right subtree is higher */
379 assert (n->right != NULL);
380 r = c_avl_node_next (n);
383 assert ((r->left == NULL) || (r->right == NULL));
392 assert ((n->left == NULL) || (n->right == NULL));
394 if ((n->left == NULL) && (n->right == NULL))
396 /* Deleting a leave is easy */
397 if (n->parent == NULL)
399 assert (t->root == n);
404 assert ((n->parent->left == n)
405 || (n->parent->right == n));
406 if (n->parent->left == n)
407 n->parent->left = NULL;
409 n->parent->right = NULL;
411 rebalance (t, n->parent);
416 else if (n->left == NULL)
418 assert (BALANCE (n) == -1);
419 assert ((n->parent == NULL) || (n->parent->left == n) || (n->parent->right == n));
420 if (n->parent == NULL)
422 assert (t->root == n);
425 else if (n->parent->left == n)
427 n->parent->left = n->right;
431 n->parent->right = n->right;
433 n->right->parent = n->parent;
435 if (n->parent != NULL)
436 rebalance (t, n->parent);
441 else if (n->right == NULL)
443 assert (BALANCE (n) == 1);
444 assert ((n->parent == NULL) || (n->parent->left == n) || (n->parent->right == n));
445 if (n->parent == NULL)
447 assert (t->root == n);
450 else if (n->parent->left == n)
452 n->parent->left = n->left;
456 n->parent->right = n->left;
458 n->left->parent = n->parent;
460 if (n->parent != NULL)
461 rebalance (t, n->parent);
472 } /* void *_remove */
477 c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *))
484 if ((t = malloc (sizeof (*t))) == NULL)
488 t->compare = compare;
494 void c_avl_destroy (c_avl_tree_t *t)
502 int c_avl_insert (c_avl_tree_t *t, void *key, void *value)
508 if ((new = malloc (sizeof (*new))) == NULL)
528 cmp = t->compare (nptr->key, new->key);
537 if (nptr->right == NULL)
549 else /* if (cmp > 0) */
552 if (nptr->left == NULL)
566 verify_tree (t->root);
569 } /* int c_avl_insert */
571 int c_avl_remove (c_avl_tree_t *t, const void *key, void **rkey, void **rvalue)
587 status = _remove (t, n);
588 verify_tree (t->root);
591 } /* void *c_avl_remove */
593 int c_avl_get (c_avl_tree_t *t, const void *key, void **value)
609 int c_avl_pick (c_avl_tree_t *t, void **key, void **value)
614 if ((key == NULL) || (value == NULL))
620 while ((n->left != NULL) || (n->right != NULL))
627 else if (n->right == NULL)
633 if (n->left->height > n->right->height)
642 else if (p->left == n)
655 } /* int c_avl_pick */
657 c_avl_iterator_t *c_avl_get_iterator (c_avl_tree_t *t)
659 c_avl_iterator_t *iter;
664 iter = calloc (1, sizeof (*iter));
670 } /* c_avl_iterator_t *c_avl_get_iterator */
672 int c_avl_iterator_next (c_avl_iterator_t *iter, void **key, void **value)
676 if ((iter == NULL) || (key == NULL) || (value == NULL))
679 if (iter->node == NULL)
681 for (n = iter->tree->root; n != NULL; n = n->left)
688 n = c_avl_node_next (iter->node);
699 } /* int c_avl_iterator_next */
701 int c_avl_iterator_prev (c_avl_iterator_t *iter, void **key, void **value)
705 if ((iter == NULL) || (key == NULL) || (value == NULL))
708 if (iter->node == NULL)
710 for (n = iter->tree->root; n != NULL; n = n->left)
711 if (n->right == NULL)
717 n = c_avl_node_prev (iter->node);
728 } /* int c_avl_iterator_prev */
730 void c_avl_iterator_destroy (c_avl_iterator_t *iter)
735 int c_avl_size (c_avl_tree_t *t)