-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path33-bst-operations.c
266 lines (231 loc) · 6.1 KB
/
33-bst-operations.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
typedef struct tree TREE;
TREE * insert_into_bst(TREE *, int);
void inorder(TREE *);
void preorder(TREE *);
void postorder(TREE *);
TREE * delete_from_bst(TREE *, int);
/*
Function Name: insert_into_bst
Input Params: Root of the tree and data item to be inserted
Return Type: Updated root of the tree
Description: Inserts a node into a binary search tree at
appropriate position
*/
TREE * insert_into_bst(TREE * root, int data)
{
TREE *newnode,*currnode,*parent;
// Dynamically allocate the memory using malloc
newnode=(TREE*)malloc(sizeof(TREE));
// Check if the memory allocation was successful
if(newnode==NULL)
{
printf("Memory allocation failed\n");
return NULL;
}
// Initialize the tree node elements
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
// When the first insertion happens which is the root node
if(root == NULL)
{
root = newnode;
printf("Root node inserted into tree\n");
return root;
}
// Traverse through the desired part of the tree using
// currnode and parent pointers
currnode = root;
parent = NULL;
while(currnode != NULL)
{
parent = currnode;
if(newnode->data < currnode->data)
currnode = currnode->left;
else
currnode = currnode->right;
}
// Attach the node at appropriate place using parent
if(newnode->data < parent->data)
parent->left = newnode;
else
parent->right = newnode;
// print the successful insertion and return root
printf("Node inserted successfully into the tree\n");
return root;
}
/*
Function Name: inorder
Input Params: Root of the tree
Return Type: void
Description: Recursively visits the tree in the order of
Left, Root, Right
*/
void inorder(TREE *troot)
{
if(troot != NULL)
{
inorder(troot->left);
printf("%d\t",troot->data);
inorder(troot->right);
}
}
/*
Function Name: preorder
Input Params: Root of the tree
Return Type: void
Description: Recursively visits the tree in the order of
Root, Left, Right
*/
void preorder(TREE *troot)
{
if(troot != NULL)
{
printf("%d\t",troot->data);
preorder(troot->left);
preorder(troot->right);
}
}
/*
Function Name: postorder
Input Params: Root of the tree
Return Type: void
Description: Recursively visits the tree in the order of
Left, Right, Root
*/
void postorder(TREE *troot)
{
if(troot != NULL)
{
postorder(troot->left);
postorder(troot->right);
printf("%d\t",troot->data);
}
}
/*
Function Name: delete_from_bst
Input Params: Root of the tree, item data to be deleted
Return Type: Updated root of the tree
Description: Deletes the specified data and re-adjusts the
tree structure according to bst tree constraints
*/
TREE * delete_from_bst(TREE * root, int data)
{
TREE * currnode, *parent, *successor, *p;
// Check if the tree is empty
if(root == NULL)
{
printf("Tree is empty\n");
return root;
}
// Traverse and reach the appropriate part of the tree
parent = NULL;
currnode = root;
while (currnode != NULL && data != currnode->data)
{
parent = currnode;
if(data < currnode->data)
currnode = currnode->left;
else
currnode = currnode->right;
}
// If the data is not present in the tree
if(currnode == NULL) {
printf("Item not found\n");
return root;
}
// Check and manipulate if either left subtree is absent,
// or right subtree is absent
// or both are present
if(currnode->left == NULL)
p = currnode->right;
else if (currnode->right == NULL)
p = currnode->left;
else
{
// Process of finding the inorder successor
successor = currnode->right;
while(successor->left != NULL)
successor = successor->left;
successor->left = currnode->left;
p = currnode->right;
}
// The case of root deletion
if (parent == NULL) {
free(currnode);
return p;
}
if(currnode == parent ->left)
parent->left = p;
else
parent->right = p;
free(currnode);
return root;
}
int main()
{
TREE * root;
root = NULL;
int choice = 0;
int data = 0;
int count = 0;
while(1)
{
printf("\n******** Menu *************\n");
printf("1-Insert into BST\n");
printf("2-Inorder Traversal\n");
printf("3-Preorder Traversal\n");
printf("4-Postorder Traversal\n");
printf("5-Delete from BST\n");
printf("Any other option to exit\n");
printf("*****************************\n");
printf("Enter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter the item to insert\n");
scanf("%d", &data);
root = insert_into_bst(root, data);
break;
case 2: if(root == NULL)
printf("Tree is empty\n");
else
{
printf("Inorder Traversal is...\n");
inorder(root);
}
break;
case 3: if(root == NULL)
printf("Tree is empty\n");
else
{
printf("Preorder Traversal is...\n");
preorder(root);
}
break;
case 4: if(root == NULL)
printf("Tree is empty\n");
else
{
printf("Postorder Traversal is...\n");
postorder(root);
}
break;
case 5: printf("Enter the item to be deleted\n");
scanf("%d", &data);
root = delete_from_bst(root, data);
break;
default: printf("Exciting Code.\n");
exit(0);
}
}
return 0;
}