-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.c
More file actions
286 lines (275 loc) · 6.06 KB
/
BinarySearchTree.c
File metadata and controls
286 lines (275 loc) · 6.06 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct tree_node {
int data;
struct tree_node* left;
struct tree_node* right;
}node;
node *root;
void insert(int);
void find(int,node**,node**);
void display_inorder(node*);
void delete_item(int);
void search(int);
void case_a(node *, node *);
void case_b(node *, node *);
void case_c(node *, node *);
int main()
{
int choice,item,itemToDelete,itemToSearch;
while(1)
{
printf("\t\t**Operations on Tree**\t\t\n");
printf("Enter 1 to Insert\n");
printf("Enter 2 for Continous Insertion\n");
printf("Enter 3 to Delete\n");
printf("Enter 4 to Search\n");
printf("Enter 5 to Display\n");
printf("Enter 6 to Exit\n");
printf("Your Choice: ");
scanf("%d",&choice);
printf("\n");
switch(choice)
{
case 1: printf("Enter the item to insert: ");
printf("\n");
scanf("%d",&item);
insert(item);
break;
case 2: printf("Enter the no of items to insert: ");
int n;
scanf("%d",&n);
printf("\n");
int i;
printf("Enter %d items one by one: ",n);
printf("\n");
for(i=0;i<n;i++)
{
scanf("%d",&item);
insert(item);
}
break;
case 3: printf("Enter the element to delete: ");
printf("\n");
scanf("%d",&itemToDelete);
delete_item(itemToDelete);
break;
case 4: //DBUC69
printf("Enter the item to search: ");
printf("\n");
scanf("%d",&itemToSearch);
search(itemToSearch);
break;
case 5: display_inorder(root);
break;
case 6: exit(0);
break;
default: printf("Please enter a valid choice!\n");
}
}
}
void insert(int item)
{
node *temp,*parent,*location;
find(item, &parent, &location);
if(location!=NULL)
{
printf("Item already present!");
return;
}
//Creating an empty node and putting item into it
temp=(node*)malloc(sizeof(node));
temp->data=item;
temp->left=NULL;
temp->right=NULL;
if(parent==NULL)
{
root=temp;
}
else
{
if(item < parent->data)
parent -> left = temp;
else
parent -> right=temp;
}
}
void find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if(root==NULL) /*Tree is empty*/
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->data)/*Item is already at root*/
{
*loc=root;
*par=NULL;
return;
}
if(item<root->data)
ptr=root->left;/*Item smaller than root and thus its link will point to left*/
else
ptr=root->right;/*Item greater than root and thus its link will point to right*/
ptrsave=root;//Initially the parent is declared as root
while(ptr!=NULL)
{
if(item==ptr->data)//For elements already present in the tree
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
*loc=NULL;//Item Not Found
*par=ptrsave;
}
void delete_item(int item)
{
node *parent,*location;
find(item,&parent,&location);
if(root == NULL) /* Tree empty*/
{
printf("Tree is already empty");
return;
}
if(location == NULL) /* Item not present in tree*/
{
printf("Sorry! Item not present in tree");
return;
}
/*node to be deleted is leaf node*/
if(location->left==NULL && location->right==NULL)
case_a(parent,location);
/*node to be deleted has only one child*/
if(location->left!=NULL && location->right==NULL)
case_b(parent,location);
if(location->left==NULL && location->right!=NULL)
case_b(parent,location);
/*node to be deleted has two children*/
if(location->left!=NULL && location->right!=NULL)
case_c(parent,location);
free(location);
}
void case_a(node *par, node *loc )
{
if(par == NULL) /*item to be deleted is root node*/
root=NULL;
else
{
if(loc == par -> left)
par -> left = NULL;
else
par -> right = NULL;
}
}
/*Case in which one child is present*/
void case_b(node *par,node *loc)
{
node *child;
/*Initialize child*/
if(loc -> left != NULL) /*item to be deleted has left */
child = loc -> left;
else /*item to be deleted has right */
child = loc -> right;
if(par == NULL ) /*Item to be deleted is root node*/
root = child;
else
{
if( loc == par -> left) /*item is left of its parent*/
par -> left = child;
else /*item is right of its parent*/
par -> right = child;
}
}
/*Case in which two children are present*/
void case_c(node *par,node *loc)
{
node *ptr,*ptrsave,*suc,*parsuc;
/*Find inorder successor and its parent*/
ptrsave = loc;
ptr = loc -> right;
while(ptr -> left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc=ptr;
parsuc=ptrsave;
if(suc -> left == NULL && suc -> right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if(par == NULL) /*if item to be deleted is root node */
root = suc;
else
{
if(loc == par -> left)
par -> left = suc;
else
{
par -> right = suc;
suc -> left = loc -> left;
suc -> right = loc -> right;
}
}
}
//DBUC69
void search(int item)
{
node *parent,*location;
find(item,&parent,&location);
if(root==NULL)
{
printf("The tree is empty!\n");
}
else if(location==NULL)
{
printf("Sorry %d is not present in the tree!\n",item);
}
else if(parent==NULL)
{
if(root->data==item)
{
printf("%d is present at the root\n",item);
}
}
else if(item==location->data)
{
if(parent->left!=NULL)
printf("Item is present in the tree and is the left child of parent node %d",parent->data);
else
printf("Item is present in the tree and is the right child of parent node %d",parent->data);
}
/*else if(item==parent->right->data)
{
printf("Item is present in the tree and the parent node of item is %d",parent->data);
}*/
else
{
printf("Item is not present in the tree");
}
}
void display_inorder(node *r)
{
if(r!=NULL)
{
display_inorder(r->left);
if(r->data==root->data)
{
printf("Root->");
printf("%d\n",r->data);
}
else
printf(" %d\n",r->data);
display_inorder(r->right);
}
}