1
+
2
+ #include < stdio.h>
3
+ #include < stdlib.h>
4
+
5
+ struct node
6
+ {
7
+ int key;
8
+ struct node *left, *right;
9
+ };
10
+
11
+ // A utility function to create a new BST node
12
+ struct node *newNode (int item)
13
+ {
14
+ struct node *temp = (struct node *)malloc (sizeof (struct node ));
15
+ temp->key = item;
16
+ temp->left = temp->right = NULL ;
17
+ return temp;
18
+ }
19
+
20
+ // A utility function to do inorder traversal of BST
21
+ void inorder (struct node *root)
22
+ {
23
+ if (root != NULL )
24
+ {
25
+ inorder (root->left );
26
+ printf (" %d " , root->key );
27
+ inorder (root->right );
28
+ }
29
+ }
30
+
31
+ /* A utility function to insert a new node with given key in BST */
32
+ struct node * insert (struct node * node, int key)
33
+ {
34
+ /* If the tree is empty, return a new node */
35
+ if (node == NULL ) return newNode (key);
36
+
37
+ /* Otherwise, recur down the tree */
38
+ if (key < node->key )
39
+ node->left = insert (node->left , key);
40
+ else
41
+ node->right = insert (node->right , key);
42
+
43
+ /* return the (unchanged) node pointer */
44
+ return node;
45
+ }
46
+
47
+ /* Given a non-empty binary search tree, return the node with minimum
48
+ key value found in that tree. Note that the entire tree does not
49
+ need to be searched. */
50
+ struct node * minValueNode (struct node * node)
51
+ {
52
+ struct node * current = node;
53
+
54
+ /* loop down to find the leftmost leaf */
55
+ while (current->left != NULL )
56
+ current = current->left ;
57
+
58
+ return current;
59
+ }
60
+
61
+ /* Given a binary search tree and a key, this function deletes the key
62
+ and returns the new root */
63
+ struct node * deleteNode (struct node * root, int key)
64
+ {
65
+ // base case
66
+ if (root == NULL ) return root;
67
+
68
+ // If the key to be deleted is smaller than the root's key,
69
+ // then it lies in left subtree
70
+ if (key < root->key )
71
+ root->left = deleteNode (root->left , key);
72
+
73
+ // If the key to be deleted is greater than the root's key,
74
+ // then it lies in right subtree
75
+ else if (key > root->key )
76
+ root->right = deleteNode (root->right , key);
77
+
78
+ // if key is same as root's key, then This is the node
79
+ // to be deleted
80
+ else
81
+ {
82
+ // node with only one child or no child
83
+ if (root->left == NULL )
84
+ {
85
+ struct node *temp = root->right ;
86
+ free (root);
87
+ return temp;
88
+ }
89
+ else if (root->right == NULL )
90
+ {
91
+ struct node *temp = root->left ;
92
+ free (root);
93
+ return temp;
94
+ }
95
+
96
+ // node with two children: Get the inorder successor (smallest
97
+ // in the right subtree)
98
+ struct node * temp = minValueNode (root->right );
99
+
100
+ // Copy the inorder successor's content to this node
101
+ root->key = temp->key ;
102
+
103
+ // Delete the inorder successor
104
+ root->right = deleteNode (root->right , temp->key );
105
+ }
106
+ return root;
107
+ }
108
+
109
+ // Driver Program to test above functions
110
+ int main ()
111
+ {
112
+ /* Let us create following BST
113
+ 50
114
+ / \
115
+ 30 70
116
+ / \ / \
117
+ 20 40 60 80 */
118
+ struct node *root = NULL ;
119
+ root = insert (root, 50 );
120
+ root = insert (root, 30 );
121
+ root = insert (root, 20 );
122
+ root = insert (root, 40 );
123
+ root = insert (root, 70 );
124
+ root = insert (root, 60 );
125
+ root = insert (root, 80 );
126
+
127
+ printf (" Inorder traversal of the given tree \n " );
128
+ inorder (root);
129
+
130
+ printf (" \n Delete 20\n " );
131
+ root = deleteNode (root, 20 );
132
+ printf (" Inorder traversal of the modified tree \n " );
133
+ inorder (root);
134
+
135
+ printf (" \n Delete 30\n " );
136
+ root = deleteNode (root, 30 );
137
+ printf (" Inorder traversal of the modified tree \n " );
138
+ inorder (root);
139
+
140
+ printf (" \n Delete 50\n " );
141
+ root = deleteNode (root, 50 );
142
+ printf (" Inorder traversal of the modified tree \n " );
143
+ inorder (root);
144
+
145
+
0 commit comments