summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8e4ecdb)
raw | patch | inline | side by side (parent: 8e4ecdb)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 21 Nov 2006 21:08:23 +0000 (22:08 +0100) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 21 Nov 2006 21:08:23 +0000 (22:08 +0100) |
I a module like this, bugs are to be expected ;)
src/utils_avltree.c | patch | blob | history |
diff --git a/src/utils_avltree.c b/src/utils_avltree.c
index c3e83ae8a5ad7c66419996b5d87dba917401fad4..3665760bca88b1f5f1b2f871f1ebb64d0863a862 100644 (file)
--- a/src/utils_avltree.c
+++ b/src/utils_avltree.c
if (height_new == n->height)
break;
+ n->height = height_new;
+
cmp = height_right - height_left;
if (cmp < -1)
{
n->parent = l;
n->left = lr;
- if (n == t->root)
+ if (lr != NULL)
+ lr->parent = n;
+
+ if (l->parent == NULL)
+ {
+ assert (t->root == n);
t->root = l;
+ }
+ else
+ {
+ assert ((l->parent->left == n) || (l->parent->right == n));
+ if (l->parent->left == n)
+ l->parent->left = l;
+ else
+ l->parent->right = l;
+ }
}
else if (cmp > 1)
{
n->parent = r;
n->right = rl;
- if (n == t->root)
+ if (rl != NULL)
+ rl->parent = n;
+
+ if (r->parent == NULL)
+ {
+ assert (t->root == n);
t->root = r;
+ }
+ else
+ {
+ assert ((r->parent->left == n) || (r->parent->right == n));
+ if (r->parent->left == n)
+ r->parent->left = r;
+ else
+ r->parent->right = r;
+ }
}
else
{
n = n->parent;
}
+
+ assert ((n == NULL) || (n->parent == NULL)
+ || (n->parent->left == n)
+ || (n->parent->right == n));
} /* while (n != NULL) */
} /* void rebalance */
return (r);
}
-static void *remove (avl_tree_t *t, avl_node_t *n)
+static void *_remove (avl_tree_t *t, avl_node_t *n)
{
void *ret;
n->parent->left = NULL;
else
n->parent->right = NULL;
+
+ rebalance (t, n->parent);
}
free_node (n);
n->key = r->key;
n->value = r->value;
- remove (t, r);
+ _remove (t, r);
}
return (ret);
-} /* void *remove */
+} /* void *_remove */
/*
* public functions
new->key = key;
new->value = value;
- new->height = 0;
+ new->height = 1;
new->left = NULL;
new->right = NULL;
}
} /* while (42) */
- rebalance (t, new->parent);
+ assert ((new->parent != NULL)
+ && ((new->parent->left == new)
+ || (new->parent->right == new)));
return (0);
} /* int avl_insert */
if (n == NULL)
return (NULL);
- return (remove (t, n));
+ return (_remove (t, n));
} /* void *avl_remove */
void *avl_get (avl_tree_t *t, void *key)