-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path123-avl_remove.c
102 lines (93 loc) · 2.14 KB
/
123-avl_remove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "binary_trees.h"
int max(int a, int b);
avl_t *minNode(avl_t *tree);
/**
* max - function that returns the highest value
* @a: value
* @b: value
* Return: a if higher otherwise b
*/
int max(int a, int b)
{
return ((a > b) ? a : b);
}
/**
* minNode - Given a non-empty binary search tree, return the node with
* minimum key value found in that tree.
* @tree: binary tree
* Return: minimum key value found in that tree.
*/
avl_t *minNode(avl_t *tree)
{
avl_t *curr = tree;
/* loop down to find the leftmost leaf */
while (curr->left != NULL)
curr = curr->left;
return (curr);
}
/**
* avl_bal_and_rot - a function that rebalances an avl tree after deletion
* @root: pointer to the root node of the tree
* Return: new root node of the tree
*/
avl_t *avl_bal_and_rot(avl_t **root)
{
int balance;
if (!root || !(*root))
return (NULL);
balance = binary_tree_balance(*root);
if (balance > 1)
{
if (binary_tree_balance((*root)->left) < 0)
(*root)->left = binary_tree_rotate_left((*root)->left);
return (binary_tree_rotate_right(*root));
}
if (balance < -1)
{
if (binary_tree_balance((*root)->right) > 0)
(*root)->right = binary_tree_rotate_right((*root)->right);
return (binary_tree_rotate_left(*root));
}
return (*root);
}
/**
* avl_remove - a function that removes a node from an avl tree and rebalances
* it
* @value: value in the node
* @root: pointer to the root node of the tree to remove the node
* Return: pointet to the new root node after removing the value
*/
avl_t *avl_remove(avl_t *root, int value)
{
avl_t *swap;
if (root == NULL)
return (root);
if (value < root->n)
root->left = avl_remove(root->left, value);
else if (value > root->n)
root->right = avl_remove(root->right, value);
else
{
if ((root->left == NULL) || (root->right == NULL))
{
swap = root->left ? root->left :
root->right;
if (swap == NULL)
{
swap = root;
root = NULL;
}
else
*root = *swap;
free(swap);
}
else
{
swap = minNode(root->right);
root->n = swap->n;
root->right = avl_remove(root->right, swap->n);
}
}
root = avl_bal_and_rot(&root);
return (root);
}