diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-08 22:05:12 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-08 22:05:12 -0400 |
commit | e4a56e626030b728a7dcada8e84a77d1da3b00ae (patch) | |
tree | b3b8ec17db56f49ef30a935ba25aed4a4b4b4481 /collections/map | |
parent | 86a0b0c0c9cf0674b55477a52ae80ef5635bbf64 (diff) |
Add rebalance function for map trees
Diffstat (limited to 'collections/map')
-rw-r--r-- | collections/map/map.c | 27 |
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; { |