-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path114-bst_remove.c
More file actions
136 lines (120 loc) · 2.7 KB
/
114-bst_remove.c
File metadata and controls
136 lines (120 loc) · 2.7 KB
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
#include "binary_trees.h"
/**
* r_case - removes a node from a Binary Search Tree for node->right case
* @root: tree root
* @node: node to delete
*
* Return: pointer the tree root
*/
bst_t *r_case(bst_t *node, bst_t *root)
{
node->right->left = node->left;
node->right->parent = node->parent;
if (node->parent)
{
if (node == node->parent->left)
node->parent->left = node->right;
if (node == node->parent->right)
node->parent->right = node->right;
}
if (node->left)
node->left->parent = node->right;
if (root == node)
root = node->right;
free(node);
return (root);
}
/**
* r_l_case - removes a node from a Binary Search Tree for node->right->l case
* @root: tree root
* @node: node to delete
*
* Return: pointer the tree root
*/
bst_t *r_l_case(bst_t *node, bst_t *root)
{
node->right->left->right = node->right;
node->right->left->parent = node->parent;
node->right->left->left = node->left;
if (node->left)
node->left->parent = node->right->left;
node->right->parent = node->right->left;
if (root == node)
root = node->right->left;
else
{
if (node == node->parent->left)
node->parent->left = node->right->left;
if (node == node->parent->right)
node->parent->right = node->right->left;
}
node->right->left = NULL;
free(node);
return (root);
}
/**
* binary_tree_is_leaf - checks if a node is a leaf
* @node: pointer to the node to check
*
* Return: 1 if node is a leaf, otherwise 0
*/
int binary_tree_is_leaf(const binary_tree_t *node)
{
int leaf = 0;
if (node && !(node->left) && !(node->right))
leaf = 1;
return (leaf);
}
/**
* bst_search - searches for a value in a Binary Search Tree
* @tree: tree root
* @value: node value
*
* Return: pointer the found node
*/
bst_t *bst_search(const bst_t *tree, int value)
{
if (tree && value < tree->n)
return (bst_search(tree->left, value));
if (tree && value > tree->n)
return (bst_search(tree->right, value));
return ((bst_t *)tree);
}
/**
* bst_remove - removes a node from a Binary Search Tree
* @root: tree root
* @value: node value
*
* Return: pointer the tree root
*/
bst_t *bst_remove(bst_t *root, int value)
{
bst_t *node;
node = bst_search(root, value);
if (node != NULL)
{
if (binary_tree_is_leaf(node) == 1)
{
if (node == node->parent->left)
node->parent->left = NULL;
if (node == node->parent->right)
node->parent->right = NULL;
free(node);
return (root);
}
if (node->right && node->right->left)
root = r_l_case(node, root);
else if (node->right)
root = r_case(node, root);
else
{
if (node->parent)
node->parent->right = node->left;
node->left->parent = node->parent;
if (root == node)
root = node->left;
free(node);
}
}
return (root);
}