-
Notifications
You must be signed in to change notification settings - Fork 0
/
123-avl_remove.c
executable file
·155 lines (143 loc) · 2.98 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "binary_trees.h"
avl_t *avl_remove(avl_t *root, int value);
bst_t *bst_remove(bst_t *root, int value);
int remove_type(bst_t *root);
int successor(bst_t *node);
void bal(avl_t **tree);
/**
* avl_remove - Removes a node from an AVL tree.
*
* @root: A pointer to the root node of the tree for removing a node.
* @value: The value to remove in the tree.
*
* Return: A pointer to the new root node of the tree after
* removing the desired value, and after rebalancing
*/
avl_t *avl_remove(avl_t *root, int value)
{
avl_t *root_a = (avl_t *) bst_remove((bst_t *) root, value);
if (root_a == NULL)
return (NULL);
bal(&root_a);
return (root_a);
}
/**
* bst_remove - Remove a node from a BST tree.
*
* @root: Root of the tree.
* @value: Node with this value to remove.
*
* Return: The tree changed
*/
bst_t *bst_remove(bst_t *root, int value)
{
int type = 0;
if (root == NULL)
return (NULL);
if (value < root->n)
bst_remove(root->left, value);
else if (value > root->n)
bst_remove(root->right, value);
else if (value == root->n)
{
type = remove_type(root);
if (type != 0)
bst_remove(root->right, type);
}
else
return (NULL);
return (root);
}
/**
* remove_type - Function that removes a node depending of its children.
*
* @root: Node to remove.
*
* Return: 0 if it has no children or other value if it has
*/
int remove_type(bst_t *root)
{
int new_value = 0;
if (!root->left && !root->right)
{
if (root->parent->right == root)
root->parent->right = NULL;
else
root->parent->left = NULL;
free(root);
return (0);
}
else if ((!root->left && root->right) || (!root->right && root->left))
{
if (!root->left)
{
if (root->parent->right == root)
root->parent->right = root->right;
else
root->parent->left = root->right;
root->right->parent = root->parent;
}
if (!root->right)
{
if (root->parent->right == root)
root->parent->right = root->left;
else
root->parent->left = root->left;
root->left->parent = root->parent;
}
free(root);
return (0);
}
else
{
new_value = successor(root->right);
root->n = new_value;
return (new_value);
}
}
/**
* successor - Get the next successor i mean the min node in the right subtree.
*
* @node: Tree to check.
*
* Return: The min value of this tree.
*/
int successor(bst_t *node)
{
int left = 0;
if (node == NULL)
{
return (0);
}
else
{
left = successor(node->left);
if (left == 0)
{
return (node->n);
}
return (left);
}
}
/**
* bal - Measures balance factor of a AVL.
*
* @tree: Tree to go through.
*
* Return: Balanced factor.
*/
void bal(avl_t **tree)
{
int bval;
if (tree == NULL || *tree == NULL)
return;
if ((*tree)->left == NULL && (*tree)->right == NULL)
return;
bal(&(*tree)->left);
bal(&(*tree)->right);
bval = binary_tree_balance((const binary_tree_t *)*tree);
if (bval > 1)
*tree = binary_tree_rotate_right((binary_tree_t *)*tree);
else if (bval < -1)
*tree = binary_tree_rotate_left((binary_tree_t *)*tree);
}