aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-07-08 22:05:12 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-07-08 22:05:12 -0400
commite4a56e626030b728a7dcada8e84a77d1da3b00ae (patch)
treeb3b8ec17db56f49ef30a935ba25aed4a4b4b4481
parent86a0b0c0c9cf0674b55477a52ae80ef5635bbf64 (diff)
Add rebalance function for map trees
-rw-r--r--collections/map/map.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/collections/map/map.c b/collections/map/map.c
index 021743d..cbb3b79 100644
--- a/collections/map/map.c
+++ b/collections/map/map.c
@@ -63,6 +63,33 @@ struct map_node **node;
return;
}
+void map_rebal_tree(root)
+struct map_node **root;
+{
+ struct map_node *tmp;
+
+ if(! *root)
+ return;
+
+ if (map_height((*root)->left) > map_height((*root)->right)) {
+ if(map_bal_factor((*root)->left) >= 0) {
+ map_rotate_left(root);
+ } else {
+ map_rotate_left(root);
+ map_rotate_right(root);
+ }
+ } else {
+ if (map_bal_factor((*root)->right) >= 0) {
+ map_rotate_right(root);
+ } else {
+ map_rotate_right(root);
+ map_rotate_left(root);
+ }
+ }
+
+ return;
+}
+
map* map_new(cmp)
cmp_func cmp;
{