-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_tree.c
251 lines (251 loc) · 6.58 KB
/
bst_tree.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
#include<stdio.h>
struct bst
{
int data;
struct bst* lchild;
struct bst* rchild;
};
int height(struct bst* t)
{
SetColor(11);
int hleft,hright;
if(t==NULL)
return 0;
hleft=height(t->lchild);
hright=height(t->rchild);
if(hleft>hright)
return 1+hleft;
else
return 1+hright;
}
void successor(struct bst* t)
{
SetColor(11);
struct bst* p=t;
t=t->rchild;
while(t->lchild!=NULL)
t=t->lchild;
printf("Successor is %d",t->data);
p=p->lchild;
while(p->rchild!=NULL)
p=p->rchild;
printf("\nPredecessor is %d\n\n",p->data);
}
struct bst* inorder(struct bst* t)
{
if(t==NULL)
return t;
t->lchild=inorder(t->lchild);
printf("%d ",t->data);
t->rchild=inorder(t->rchild);
}
struct bst* preorder(struct bst* t)
{
if(t==NULL)
return t;
printf("%d ",t->data);
t->lchild=preorder(t->lchild);
t->rchild=preorder(t->rchild);
}
int findmin(struct bst* t)
{
while(t->lchild!=NULL)
t=t->lchild;
return t->data;
}
struct bst* postorder(struct bst* t)
{
if(t==NULL)
return t;
t->lchild=postorder(t->lchild);
t->rchild=postorder(t->rchild);
printf("%d ",t->data);
}
struct bst* create(int x)
{
struct bst* temp=(struct bst*)malloc(sizeof(struct bst));
temp->data=x;
temp->lchild=NULL;
temp->rchild=NULL;
return temp;
};
struct bst* insert(struct bst* t,int x)
{
if(t==NULL)
{
t=create(x);
return t;
}
if(x<t->data)
t->lchild=insert(t->lchild,x);
else if(x>t->data)
t->rchild=insert(t->rchild,x);
return t;
}
struct bst* search(struct bst* t,int x)
{
SetColor(11);
if(t==NULL)
return NULL;
else if(t->data==x)
return t;
else if(x<t->data)
search(t->lchild,x);
else if(x>t->data)
search(t->rchild,x);
}
struct bst* delete(struct bst* t,int x)
{
SetColor(11);
int data;
if(t==NULL)
return;
else if(x<t->data)
t->lchild=delete(t->lchild,x);
else if(x>t->data)
t->rchild=delete(t->lchild,x);
else
{
if(t->lchild==NULL&&t->rchild==NULL)
{
printf("Node deleted successfully\n\n");
return NULL;
}
else if(t->lchild==NULL)
{
printf("Node deleted successfully\n\n");
return t->rchild;
}
else if(t->rchild==NULL)
{
printf("Node deleted successfully\n\n");
return t->lchild;
}
else
{
data=findmin(t->rchild);
t->data=data;
t->rchild=delete(t->rchild,t->data);
}
}
return t;
}
void ancestor(int x,struct bst *t)
{
SetColor(11);
int flag=0;
printf("Ancestors are ");
if(t==NULL)
{
printf("No element in the tree");
return;
}
while(x!=t->data)
{
printf("%d ",t->data);
if(x<t->data)
t=t->lchild;
else if(x>t->data)
t=t->rchild;
}
if(t->lchild!=NULL&&t->rchild!=NULL)
printf("\nDescendants are %d and %d\n\n",t->lchild->data,t->rchild->data);
else if(t->lchild==NULL&&t->rchild!=NULL)
printf("\nDescendant is %d\n\n",t->rchild->data);
else if(t->lchild!=NULL&&t->rchild==NULL)
printf("\nDescendant is %d\n\n",t->lchild->data);
else
printf("\nNo descendants\n\n");
}
void bst_tree()
{
struct bst *root=NULL;
struct bst* t;
int c,x;
while(1)
{
int i=0,n;
system("cls");
SetColor(11);
printf("\t\t\t******BINARY SEARCH TREE******\n\n\n");
SetColor(7);
printf("\t\t1.Insert a node\n");
printf("\t\t2.Search a node\n");
printf("\t\t3.Pre-order display\n");
printf("\t\t4.In-order display\n");
printf("\t\t5.Post-order display\n");
printf("\t\t6.Delete a node\n");
printf("\t\t7.Height of tree\n");
printf("\t\t8.Ancestor and Descendant of a node\n");
printf("\t\t9.Successor and Predecessor of a node\n");
printf("\t\t10.Exit\n\n");
printf("\t\tEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:printf("\nEnter the no. of values: ");
scanf("%d",&n);
while(i<n)
{
printf("Enter the value: ");
scanf("%d",&x);
root=insert(root,x);
i++;
}
SetColor(11);
printf("\nIN-ORDER DISPLAY OF TREE:\n");
root=inorder(root);
printf("\n\n");
system("pause");
break;
case 2:printf("\nEnter the value you want to search: ");
scanf("%d",&x);
t=search(root,x);
if(t==NULL)
printf("NOT FOUND\n\n");
else
printf("FOUND\n\n");
system("pause");
break;
case 3:SetColor(11);
printf("\nPRE-ORDER DISPLAY OF TREE:\n");
root=preorder(root);
printf("\n\n");
system("pause");
break;
case 4:SetColor(11);
printf("\nIN-ORDER DISPLAY OF TREE:\n");
root=inorder(root);
printf("\n\n");
system("pause");
break;
case 5:SetColor(11);
printf("\nPOST-ORDER DISPLAY OF TREE:\n");
root=postorder(root);
printf("\n\n");
system("pause");
break;
case 6:printf("\nEnter the value you want to delete: ");
scanf("%d",&x);
root=delete(root,x);
system("pause");
break;
case 7: x=height(root);
printf("\n\nHeight of tree is %d\n\n",x);
system("pause");
break;
case 8: printf("\nEnter the node value: ");
scanf("%d",&x);
ancestor(x,root);
system("pause");
break;
case 9: printf("\nEnter the node value: ");
scanf("%d",&x);
t=search(root,x);
successor(t);
system("pause");
break;
case 10: exit(1);
}
}
}