-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.c
More file actions
308 lines (295 loc) · 8.58 KB
/
Copy pathBinarySearchTree.c
File metadata and controls
308 lines (295 loc) · 8.58 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
typedef struct numbers
{
int data;
struct numbers * left;
struct numbers * right;
}tree;
void printoption()
{
//-------------------------------------------Print the options avalible for the user-------------------------------------------
printf("Choose an option in order to proceed :-\n");
printf("1) Traverse the tree in-order.\n");
printf("2) Traverse the tree in pre-order.\n");
printf("3) Traverse the tree in post-order.\n");
printf("4) Insert a number into the tree.\n");
printf("5) Delete a number from the tree.\n");
printf("6) Search for a number inside the tree.\n");
printf("7) Exit the program.\n");
}
tree * creatept(int no)
{
//-------------------------------------------Creation of Node-------------------------------------------
tree * pt;
pt=(tree*)malloc(sizeof(tree));
pt->data=no;
pt->left=NULL;
pt->right=NULL;
return(pt);
}
bool insertpt(tree * node, int no)
{
//-------------------------------------------Insertion-------------------------------------------
if(node->data==no)
{
return(false);
}
else if(node->data<no)
{
if(node->right==NULL)
{
tree * pt;
pt=creatept(no);
node->right=pt;
return(true);
}
else
{
return(insertpt(node->right,no));
}
}
else
{
if(node->left==NULL)
{
tree * pt;
pt=creatept(no);
node->left=pt;
return(true);
}
else
{
return(insertpt(node->left,no));
}
}
}
tree * getinput()
{
//-------------------------------------------Input-------------------------------------------
printf("Enter the number of numbers to be entered into the tree :- \t");
int n,i,no;
scanf("%d",&n);
printf("Enter the numbers one by one :-\n");
tree *pt, *root;
for(i=0;i<n;i++)
{
scanf("%d",&no);
pt=creatept(no);
if(i==0)
{
root=pt;
}
else
{
insertpt(root,no);
}
}
return(root);
}
void preorder(tree * node)
{
//-------------------------------------------Printing Nodes in pre-order-------------------------------------------
if(node!=NULL)
{
printf("%d\n",node->data);
preorder(node->left);
preorder(node->right);
}
}
void inorder(tree * node)
{
//-------------------------------------------Printing Nodes in in-order-------------------------------------------
if(node!=NULL)
{
inorder(node->left);
printf("%d\n",node->data);
inorder(node->right);
}
}
void postorder(tree * node)
{
//-------------------------------------------Printing Nodes in post-order-------------------------------------------
if(node!=NULL)
{
postorder(node->left);
postorder(node->right);
printf("%d\n",node->data);
}
}
void freept(tree * node)
{
//-------------------------------------------Free the declared nodes-------------------------------------------
if(node!=NULL)
{
freept(node->left);
freept(node->right);
free(node);
}
}
tree * searchpt(tree * node, int no)
{
//-------------------------------------------Searching-------------------------------------------
if(node==NULL)
{
return(node);
}
else
{
if(node->data==no)
{
return(node);
}
else if (node->data>no)
{
return(searchpt(node->left,no));
}
else
{
return(searchpt(node->right,no));
}
}
}
int * traversept(tree * node)
{
//-------------------------------------------Traversing-------------------------------------------
static int i=0;
static int *arr;
if(i!=0)
{
arr[i]=node->data;
i++;
}
arr=traversept(node->left);
arr=traversept(node->right);
return(arr);
}
/*
bool deletept(tree * node, int no)
{
//-------------------------------------------Deletion-------------------------------------------
if(node==NULL)
{
return(false);
}
else
{
if(node->data<no)
{
return(deletept(node->right,no));
}
else if(node->data>no)
{
return(deletept(node->left,no));
}
else
{
tree * ptr;
ptr=node;
static int *arr;
arr=traversept(node);
int i,n;
n=sizeof(arr)/sizeof(int);
bool f;
for(i=0;i<n;i++)
{
f=insertpt(ptr,arr[i]);
}
return(true);
}
}
}
*/
void timelapse()
{
int i=0;
for(i=0;i<1000;i++);
}
int main()
{
printf("-------------------------------------------Start of Program-------------------------------------------\n");
tree * root;
root = getinput();
int cho,i=1,no;
bool f;
do
{
printf("-------------------------------------------Round - %d-------------------------------------------\n",i);
i++;
printoption();
printf("Enter your choice :-\t");
scanf("%d",&cho);
switch(cho)
{
case 1 :
printf("-------------------------------------------Traversing in In-Order-------------------------------------------\n");
printf("The tree traversed in an in-order format is as follows :- \n");
inorder(root);
break;
case 2 :
printf("-------------------------------------------Traversing in Pre-Order-------------------------------------------\n");
printf("The tree traversed in an pre-order format is as follows :- \n");
preorder(root);
break;
case 3 :
printf("-------------------------------------------Traversing in Post-Order-------------------------------------------\n");
printf("The tree traversed in an post-order format is as follows :- \n");
postorder(root);
break;
case 4 :
printf("-------------------------------------------Insertion-------------------------------------------\n");
printf("Enter the nuber to be inserted :-\t\n");
scanf("%d",&no);
f=insertpt(root,no);
if(f==true)
{
printf("The number %d has been inserted into the tree.\n",no);
}
else
{
printf("The number %d cannot be inserted into the tree as it is already present in the tree.\n",no);
}
break;
/*case 5 :
printf("-------------------------------------------Deletion-------------------------------------------\n");
printf("Enter the nuber to be deleted :-\t\n");
scanf("%d",&no);
f=deletept(root,no);
if(f==true)
{
printf("The number %d has been deleted from the tree.\n",no);
}
else
{
printf("The number %d cannot be deleted from the tree as it is not present in the tree.\n",no);
}
break;*/
case 6 :
printf("-------------------------------------------Searching-------------------------------------------\n");
printf("Enter the nuber to be searched :-\t\n");
scanf("%d",&no);
tree * pt;
pt=searchpt(root,no);
if(pt!=NULL)
{
printf("The number %d has been found in the tree at the address %p.\n",no,pt);
}
else
{
printf("The number %d cannot be found in the tree.\n",no);
}
break;
case 7 :
printf("\t\tThank You\n");
printf("-------------------------------------------End of Program-------------------------------------------\n");
freept(root);
break;
default :
printf("-------------------------------------------Invalid Input-------------------------------------------\n");
printf("You have entered an invalid input.\n");
printf("Please choose a valid option.\n");
}
timelapse();
}while(cho!=7);
}