-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114-bst_remove.c
75 lines (69 loc) · 1.62 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
#include "binary_trees.h"
bst_t *minValueNode(bst_t *tree);
/**
* 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:value to remove in the tree
* Return: pointer to the new root node of the tree after removal of value
* Node containing a value equals to value must be removed and freed
* Node with two children:replace with its 1st in-order successor
**/
bst_t *bst_remove(bst_t *root, int value)
{
bst_t *tmp, *parent;
if (!root)
return (root);
else if (value < root->n)
/* search in the left */
root->left = bst_remove(root->left, value);
else if (value > root->n)
/* search in the right */
root->right = bst_remove(root->right, value);
else /* when value = root->n */
{
/* no child or 1 child */
if (root->left == NULL && root->right == NULL)
{
free(root);
root = NULL;
}
else if (root->left == NULL)
{
tmp = root;
parent = root->parent;
root = root->right;
root->parent = parent;
free(tmp);
}
else if (root->right == NULL)
{
tmp = root;
parent = root->parent;
root = root->left;
root->parent = parent;
free(tmp);
}
/* 2 children */
/* Inorder successor: smallest element in right subtree */
else
{
tmp = minValueNode(root->right);
root->n = tmp->n;
root->right = bst_remove(root->right, tmp->n);
}
}
return (root);
}
/**
* minValueNode - finds the smallest element in BST subtree
* @root: pointer to the roor if the tree
* Return: smallest element
*/
bst_t *minValueNode(bst_t *root)
{
while (root->left != NULL)
{
root = root->left;
}
return (root);
}