-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path114-bst_remove.c
executable file
·147 lines (127 loc) · 2.65 KB
/
114-bst_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
#include <stdlib.h>
#include <stdio.h>
#include "binary_trees.h"
bst_t *remove_l(bst_t *root);
bst_t *remove_r(bst_t *root);
bst_t *remove_l_r(bst_t *root);
/**
* bst_remove - Removes a node from a Binary Search Tree
* @root: Pointer to the root node of the tree where you will remove a node
* @value: The value to remove in the tree
*
* Return: Pointer to the new root node of the tree
* after removing the desired value
*/
bst_t *bst_remove(bst_t *root, int value)
{
bst_t *new = NULL, *node = root, *result = NULL;
if (!root)
return (NULL);
while (node && node->n != value)
{
if (node->n < value)
node = node->right;
else
node = node->left;
}
if (node)
{
if (!node->parent)
{
if (!node->left && node->right)
{
node->right->parent = node->parent;
result = node->right;
free(node);
return (result);
}
if (!node->right && node->left)
{
node->left->parent = node->parent;
result = node->left;
free(node);
return (result);
}
if (!node->right && !node->left)
{
free(node);
return (NULL);
}
}
if (node->left && node->right)
{
new = remove_l_r(node);
if (new->parent)
new = root;
}
else
{
new = root;
if (!node->parent)
new = node->left ? node->left : node->right;
if (!node->left)
free(remove_l(node));
else if (!node->right)
free(remove_r(node));
}
}
return (new);
}
/**
* remove_l - Remove node has right and left is NULL
* @root: Pointer to the root node of the tree where you will remove a node
*
* Return: Pointer to the new node
*/
bst_t *remove_l(bst_t *root)
{
if (root->parent->left == root)
root->parent->left = root->right;
else
root->parent->right = root->right;
if (root->right)
root->right->parent = root->parent;
return (root);
}
/**
* remove_r - Remove node has left and right is NULL
* @root: Pointer to the root node of the tree where you will remove a node
*
* Return: Pointer to the new node
*/
bst_t *remove_r(bst_t *root)
{
if (root->parent->left == root)
root->parent->left = root->left;
else
root->parent->right = root->left;
if (root->left)
root->left->parent = root->parent;
return (root);
}
/**
* remove_l_r - Remove node has left and right
* @root: Pointer to the root node of the tree where you will remove a node
*
* Return: Pointer to the new node
*/
bst_t *remove_l_r(bst_t *root)
{
bst_t *tmp = root->right;
while (tmp)
{
if (!tmp->left)
{
root->n = tmp->n;
if (tmp->parent == root)
tmp->parent->right = tmp->right;
else
tmp->parent->left = tmp->right;
tmp->right->parent = root;
free(tmp);
return (root);
}
tmp = tmp->left;
}
return (tmp);
}