-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathBinarySearchTree.c
294 lines (282 loc) · 6.67 KB
/
BinarySearchTree.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//Govind A
/* BINARY SEARCH TREE
datastrucure used is LinkedList
*/
#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *RCHILD;
struct node *LCHILD;
};
struct node *root = NULL;
struct node *getnode()
{
struct node *new = (struct node *)malloc(sizeof(struct node));
return new;
}
// Function to insert at the front of the linked list
int Insert(int item)
{
int flag = 0;
struct node *ptr = getnode();
ptr = root;
struct node *ptr1 = getnode();
if(ptr == NULL)
{
root = (struct node*) malloc(sizeof(struct node));
root->data = item;
root->LCHILD = NULL;
root ->RCHILD = NULL;
printf("The element %d is inserted at the root of the binary search tree.\n",item);
}
else
{
while(ptr!=NULL && flag==0)
{
if(item < ptr->data)
{
ptr1=ptr;
ptr = ptr->LCHILD;
}
else if(item > ptr->data)
{
ptr1=ptr;
ptr = ptr->RCHILD;
}
else if(item == ptr->data)
{
flag = 1;
printf("The data is already present at a node.");
}
}
if(ptr==NULL)
{
struct node *new = getnode();
new->data = item;
new->LCHILD = NULL;
new->RCHILD = NULL;
if(ptr1->data < item)
{
ptr1->RCHILD = new;
}
else
{
ptr1->LCHILD = new;
}
printf("The element %d is inserted.\n",item);
}
}
}
struct node* Search(struct node *root,int item)
{
if(root == NULL)
{
printf("The tree is empty.");
}
else if(item == root->data)
{
printf("The data is present in the tree.");
}
else if(item < root->data)
{
return Search(root->LCHILD,item);
}
else
{
return Search(root->RCHILD,item);
}
}
struct node* SUCC(struct node *ptr)
{
struct node* ptr1 = getnode();
ptr1 = ptr->RCHILD;
if(ptr1 != NULL)
{
while(ptr1->LCHILD != NULL)
{
ptr1 = ptr1->LCHILD;
}
}
return(ptr1);
}
// Function to delete from the front of the linked list
struct node* Delete(int item)
{
int data,flag=0;
struct node *ptr = root;
struct node *ptr1 = getnode();
struct node *ptr2 = getnode();
while(ptr!=NULL && flag==0)
{
if(item < ptr->data)
{
ptr1=ptr;
ptr = ptr->LCHILD;
}
else if(item > ptr->data)
{
ptr1=ptr;
ptr = ptr->RCHILD;
}
else if(ptr->data == item)
{
flag = 1;
}
}
if(flag==0)
{
printf("The item is not present in the tree.");
}
//Decide the case of deletePosition
if(ptr->LCHILD==NULL && ptr->RCHILD==NULL) //case 1 : Node has no child
{
if(ptr1->LCHILD == ptr)
{
ptr1->LCHILD = NULL;
}
else
{
ptr1->RCHILD = NULL;
}
return(ptr);
}
else
{
if(ptr->LCHILD != NULL && ptr->RCHILD != NULL) //case 3 : Node has both children
{
ptr2 = SUCC(ptr); //find the inorder successor of the Node
ptr->data = ptr2->data;
free(ptr2);
}
else //case 2 : Node has only one children
{
if(ptr1->LCHILD == ptr)
{
if(ptr->LCHILD == NULL)
{
ptr1->LCHILD = ptr->RCHILD;
}
else
{
ptr1->LCHILD = ptr->LCHILD;
}
}
else
{
if(ptr1->RCHILD == ptr)
{
if(ptr->LCHILD == NULL)
{
ptr1->RCHILD = ptr->RCHILD;
}
else
{
ptr1->RCHILD = ptr->LCHILD;
}
}
}
return(ptr);
}
}
}
void inorderTraverse(struct node *root)
{
if(root==NULL)
{
return;
}
inorderTraverse(root->LCHILD);
printf("%d->",root->data);
inorderTraverse(root->RCHILD);
}
void preorderTraverse(struct node *root)
{
if(root==NULL)
{
return;
}
else
{
printf("%d->",root->data);
preorderTraverse(root->LCHILD);
preorderTraverse(root->RCHILD);
}
}
void postorderTraverse(struct node *root)
{
if(root==NULL)
{
return;
}
postorderTraverse(root->LCHILD);
postorderTraverse(root->RCHILD);
printf("%d->",root->data);
}
int main()
{
int option,choice,data,key,value;
while(1)
{
printf("\nBinary Search Tree operations...\n1.Insertion\n2.Deletion\n3.Traversal\n4.Search");
printf("\nEnter option : ");
scanf("%d",&option);
switch(option)
{
case 1:
{
printf("\tEnter the data to be inserted : ");
scanf("%d",&data);
Insert(data);
break;
}
case 2:
{
printf("\tEnter the data to be deleted : ");
scanf("%d",&data);
Delete(data);
break;
}
case 3:
{
printf("\tTraversal options..\n\t1.Inorder\n\t2.Preorder\n\t3.Postorder\n\tEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
inorderTraverse(root);
break;
}
case 2:
{
preorderTraverse(root);
break;
}
case 3:
{
postorderTraverse(root);
break;
}
default:
{
break;
}
}
break;
}
case 4:
{
printf("Enter the node to be searched : ");
scanf("%d",&key);
Search(root,key);
break;
}
default:
{
printf("Enter the correct option.\n");
}
}
}
}