aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-07-21 15:46:40 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-07-21 15:46:40 -0400
commitf3ffb0da78314e40615192ff719e28e145870894 (patch)
treef7cd21b8c16495e7cf6e1ca594cc75d47cb40127
parent9cf77ca0c27cdcbae0142d97ce8b32e74f1b1df9 (diff)
Add AVL checks to map remove
-rw-r--r--collections/map/map.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/collections/map/map.c b/collections/map/map.c
index 5fda2a5..015a3cb 100644
--- a/collections/map/map.c
+++ b/collections/map/map.c
@@ -462,9 +462,18 @@ void *key;
tmp->parent = tmp->right = tmp->left = NULL;
free(tmp);
- *root = next;
+ if (!(*root = next) && !next_parent)
+ return ret;
- //CHECK BALANCE
+ for (tmp = next_parent; tmp && tmp->parent; tmp = tmp->parent) {
+ bal = map_bal_factor(tmp);
+ if (bal <= -2 || bal >= 2) {
+ if (tmp == tmp->parent->left) {
+ map_rebal_tree(&(tmp->parent->left));
+ } else if (tmp == tmp->parent->right)
+ map_rebal_tree(&(tmp->parent->right));
+ }
+ }
return ret;
}
@@ -472,10 +481,20 @@ void* map_remove(root, key)
map *root;
void *key;
{
+ void *ret;
+ int bal;
+
if (!root)
return NULL;
- return map_remove_aux(&root->root, root->cmp, key);
+ ret = map_remove_aux(&root->root, root->cmp, key);
+
+ bal = map_bal_factor(root->root);
+ if (bal <= -2 || bal >= 2) {
+ map_rebal_tree(&root->root);
+ }
+
+ return ret;
}
void map_clear_aux(root)