-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf.c
322 lines (321 loc) · 8.03 KB
/
bf.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct tree//to shorten name
{
int info;
struct tree *left;
struct tree *right;
}node;
node * create(int n);
node * search(node *root,int ele);
void insert(node *root,int ele,int *num);
void preorder(node *root);
void inorder(node *root);
void postorder(node *root);
void del(node *root,int ele,int *num);
int height(node *pos);
int balfac(node *pos);
void complete(node *root);
int main()
{
int i,n,ele,b,num;
node *root,*p;
node *pos=NULL;
char ch;
root=NULL;
p=NULL;
int *a;//to store address of no. of elements
printf("\n\nCreate your Binary Search Tree (^_^)\n");
printf("\nEnter number of elements in the tree --> ");
scanf("%d",&num);
root=create(num);
a=#
printf("\nTree successfully created...\n");
while(1)//so that programme runs any no. of times
{
printf("\n\n **MAIN MENU:**\n");
printf("Press 1 to create your Tree\n");//menu
printf("Press 2 to search an element\n");
printf("Press 3 to insert an element\n");
printf("Press 4 to delete an entry\n");
printf("Press 5 for Pre-Order traversal\n");
printf("Press 6 for In-Order traversal\n");
printf("Press 7 for Post-Order traversal\n");
printf("Press h to find height of a node\n");
printf("Press 8 to find balance factor of a node\n");
printf("Press 9 to check tree is complete or not\n");
printf("Press 0 to exit from the programme\n");
printf("\nEnter your choice --> ");
scanf("%s",&ch);
switch(ch)
{
case '1':printf("\n Enter the size of the Tree --> ");
scanf("%d",&num);
root=create(num);
a=#
printf("Tree successfully created...\n");
break;
case '2':printf("\nEnter the value to be searched --> ");
scanf("%d",&ele);
pos=search(root,ele);
if(pos==NULL)
printf("%d Not Found in this tree!!\n",ele);
else
printf("%d Found...",ele);
break;
case '3':printf("\nEnter the value to be inserted --> ");
scanf("%d",&ele);
insert(root,ele,a);
printf("Value inserted in the Tree...\n");
break;
case '4':printf("\nEnter the value to be deleted --> ");
scanf("%d",&ele);
del(root,ele,a);
printf("Value Successfully Deleted..\n");
break;
case '5':printf(" Tree -->\n");
preorder(root);
break;
case '6':printf(" Tree -->\n");
inorder(root);
break;
case '7':printf(" Tree -->\n");
postorder(root);
break;
case 'h':printf("Enter info whose height is to be found --> ");
scanf("%d",&ele);
pos=search(root,ele);
if(pos==NULL)
{
printf("%d Not Found in this tree!!\n",ele);
break;
}
else
{
b=height(pos);
printf("height of node containing %d is %d",ele,b);
break;
}
case '8':printf("Enter info whose balance factor is to be found --> ");
scanf("%d",&ele);
pos=search(root,ele);
if(pos==NULL)
{
printf("%d Not Found in this tree!!\n",ele);
break;
}
else
{
b=balfac(pos);
printf("Balance Factor of node containing %d is %d",ele,b);
break;
}
case '9':complete(root);
break;
case '0':exit(0);
default :printf("Invalid Choice (-_-)\n");
}
printf("\n\nPress a key to continue --> ");
getchar();
printf("\n");
}
return 0;
}
node * create(int n)
{
int i,ele;
node *root,*p,*q,*m;
root=NULL;
for(i=1;i<=n;i++)
{
q=root;
m=(node *)malloc(sizeof(node));
printf("Enter Data [%d] --> ",i);
scanf("%d",&ele);
m->info=ele;
m->left=NULL;
m->right=NULL;
if(root==NULL)
{
root=m;
p=root;
q=root;
}
else
{
while(q!=NULL)
{
p=q;//To store address of last node
if(q->info>ele)
q=q->left;
else
q=q->right;
}
if(p->info<ele)
p->right=m;
else if(p->info>ele)
p->left=m;
else
{
printf(" **This value already exits (-_-)\n");
i--;
}
}
}
return root;
}
node * search(node *root,int ele)
{
node *p,*q;
q=root;
p=NULL;
while(q!=NULL && q->info!=ele)
{
if(ele<q->info)
q=q->left;
else
q=q->right;
}
return q;
}
void insert(node *root,int ele,int *num)
{
node *p,*q,*m;
p=q=root;
m=(node *)malloc(sizeof(node));
m->info=ele;
while(p!=NULL)
{
q=p;
if(p->info>ele)
p=p->left;
else if(ele>p->info)
p=p->right;
else
{
printf("This value already exists (-_-)\n");
break;
}
}
if(p==NULL)
{
if(ele<q->info)
{
q->left=m;
}
else if(ele>q->info)
{
q->right=m;
}
*num=*num+1;
}
}
void del(node *root,int ele,int *num)
{
node *p,*q,*s,*r;
p=root;
while((p!=NULL) && (p->info!=ele))
{
q=p;
if(p->info>ele)
p=p->left;
else
p=p->right;
}
if(p==NULL)
printf("Information not Found (-_-)\n");
else if(p->info==ele)
{
if((p->left==NULL) && (p->right==NULL))
{
if(q->info>ele)
q->left=NULL;
else
q->right=NULL;
}
if((p->left==NULL) && (p->right!=NULL))
{
if(q->info>ele)
q->left=p->right;
else
q->right=p->right;
}
if((p->left!=NULL) && (p->right==NULL))
{
if(q->info>ele)
q->left=p->left;
else
q->right=p->left;
}
if((p->left!=NULL) && (p->right!=NULL))
{
r=p->left;
while(r->right!=NULL)
{
s=r;
r=r->right;
}
if(r->left==NULL)
p->info=r->info;
else if(r->left!=NULL)
{
p->info=r->info;
s->right=r->left;
}
}
*num=*num-1;
}
}
void preorder(node *p)
{
if(p!=NULL)
{
printf("%d ",p->info);
preorder(p->left);
preorder(p->right);
}
}
void inorder(node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d ",p->info);
inorder(p->right);
}
}
void postorder(node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d ",p->info);
}
}
int balfac(node *pos)
{
int lh,rh;
lh=height(pos->left);
rh=height(pos->right);
return (lh-rh);
}
void complete(node *root)
{
}
int height(node *pos)
{
int lh,rh;
if(pos == NULL || (pos->left==NULL && pos->right==NULL))
return 0;
else
{
lh=height(pos->left);
rh=height(pos->right);
if (lh > rh)
return(lh+1);
else
return(rh+1);
}
}