-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114-bst_remove.c
99 lines (90 loc) · 2.64 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
#include "binary_trees.h"
bst_t *inorder_successor(bst_t *root);
bst_t *bst_delete(bst_t *root, bst_t *node);
bst_t *bst_remove_recursive(bst_t *root, bst_t *node, int value);
bst_t *bst_remove(bst_t *root, int value);
/**
* inorder_successor - Returns the minimum value of a binary search tree.
* @root: A pointer to the root node of the BST to search.
*
* Return: The minimum value in @tree.
*/
bst_t *inorder_successor(bst_t *root)
{
while (root->left != NULL)
root = root->left;
return (root);
}
/**
* bst_delete - Deletes a node from a binary search tree.
* @root: A pointer to the root node of the BST.
* @node: A pointer to the node to delete from the BST.
*
* Return: A pointer to the new root node after deletion.
*/
bst_t *bst_delete(bst_t *root, bst_t *node)
{
bst_t *parent = node->parent, *successor = NULL;
/* No children or right-child only */
if (node->left == NULL)
{
if (parent != NULL && parent->left == node)
parent->left = node->right;
else if (parent != NULL)
parent->right = node->right;
if (node->right != NULL)
node->right->parent = parent;
free(node);
return (parent == NULL ? node->right : root);
}
/* Left-child only */
if (node->right == NULL)
{
if (parent != NULL && parent->left == node)
parent->left = node->left;
else if (parent != NULL)
parent->right = node->left;
if (node->left != NULL)
node->left->parent = parent;
free(node);
return (parent == NULL ? node->left : root);
}
/* Two children */
successor = inorder_successor(node->right);
node->n = successor->n;
return (bst_delete(root, successor));
}
/**
* bst_remove_recursive - Removes a node from a binary search tree recursively.
* @root: A pointer to the root node of the BST to remove a node from.
* @node: A pointer to the current node in the BST.
* @value: The value to remove from the BST.
*
* Return: A pointer to the root node after deletion.
*/
bst_t *bst_remove_recursive(bst_t *root, bst_t *node, int value)
{
if (node != NULL)
{
if (node->n == value)
return (bst_delete(root, node));
if (node->n > value)
return (bst_remove_recursive(root, node->left, value));
return (bst_remove_recursive(root, node->right, value));
}
return (NULL);
}
/**
* bst_remove - Removes a node from a binary search tree.
* @root: A pointer to the root node of the BST to remove a node from.
* @value: The value to remove in the BST.
*
* Return: A pointer to the new root node after deletion.
*
* Description: If the node to be deleted has two children, it
* is replaced with its first in-order successor.
*/
bst_t *bst_remove(bst_t *root, int value)
{
return (bst_remove_recursive(root, root, value));
}