-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.c
281 lines (262 loc) · 5.07 KB
/
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
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
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node* right;
struct node* left;
}node;
node* parent;
node* root=NULL;
node* ptr;
int flag;
int cas=0;
void insert(int data){
node* newnode=(node*)malloc(sizeof(node));
newnode->data=data;
newnode->right=NULL;
newnode->left=NULL;
if(newnode==NULL){
printf("No Space !!");
return;
}
if(root==NULL){
root=newnode;
}
else{
ptr=root;
int flag=0;
while(ptr!=NULL && flag==0){
if(data>ptr->data){
parent=ptr;
ptr=ptr->right;
}
else if(data<ptr->data){
parent=ptr;
ptr=ptr->left;
}
else flag==1;
}
if(flag==0){
if(data>parent->data){
parent->right=newnode;
}
else{
parent->left=newnode;
}
}
else{
printf("Element exists!\n");
return;
}
}
}
/*void delete(int x){
//int x,k;
//printf("Enter the data to be deleted : ");
//scanf("%d",&x);
struct node *ptr;
if(root==NULL)
printf("Empty Treee ");
else
{
ptr=root;
int flag=0;
while((ptr!=NULL)&&(flag==0))
{
if(x>ptr->data)
{
parent=ptr;
ptr=ptr->right;
}
else if(x<ptr->data)
{
parent=ptr;
ptr=ptr->left;
}
else
{
flag=1;
}
}
if(flag==0)
printf("Value Not found");
else if((ptr->left==NULL)&&(ptr->right==NULL))
{
printf("case 1");
if(parent->left==ptr)
parent->left=NULL;
else
parent->right=NULL;
free(ptr);
}
else if((ptr->left!=NULL)&&(ptr->right!=NULL))
{
node* s=ptr->right;
printf("case 3");
while(s->left!=NULL)
{
s=s->left;
}
int i=s->data;
delete(s->data);
ptr->data=i;
//free(ptr);
}
else
{
printf("case 2");
if(parent->left==ptr)
{
if(ptr->left==NULL)
parent->left=ptr->right;
else
parent->left=ptr->left;
}
else if(parent->right==ptr)
{
if(ptr->left==NULL)
parent->right=ptr->right;
else
parent->right=ptr->left;
}
free(ptr);
}
}
}*/
void delete(int key){
if(root==NULL){
printf("BST EMPTY!\n");
return;
}
ptr=root;
flag=0;
while((ptr!=NULL) && (flag==0)){
if(key>ptr->data){
parent=ptr;
ptr=ptr->right;
}
else if(key<ptr->data){
parent=ptr;
ptr=ptr->left;
}
else flag=1;
}
if (flag==0) {
printf("Value Not found\n");
return;
}
if ((ptr->left==NULL) && (ptr->right==NULL)) cas=1;
else if ((ptr->left!=NULL) && (ptr->right!=NULL)) cas=3;
else cas=2;
if (cas==1){
if(parent->right==ptr) parent->right==NULL;
else parent->left==NULL;
free(ptr);
}
else if (cas==2){
if(parent->left==ptr){
if(ptr->left==NULL) {
parent->left=ptr->right;
}
else{
parent->left=ptr->left;
}
}
else if (parent->right==ptr){
if(ptr->right==NULL) {
parent->right=ptr->left;
}
else {
parent->right==ptr->right;
}
}
free(ptr);
}
else if(cas==3){
node* succ=ptr->right;
while(succ->left!=NULL){
succ=succ->left;
}
int i=succ->data;
delete(succ->data);
ptr->data=i;
}
}
void search(int key){
if(root==NULL){
printf("No such BST!");
return;
}
ptr=root;
int flag=0;
while(ptr!=NULL && flag==0){
if(key>ptr->data) ptr=ptr->right;
else if (key<ptr->data) ptr=ptr->left;
else flag=1;
}
if(flag==1) printf("Found\n");
else printf("NOT Found\n");
}
void preorder(node* n){
if(n!=NULL){
printf("%d ",n->data);
preorder(n->left);
preorder(n->right);
}
}
void inorder(node* n){
if (n!=NULL){
inorder(n->left);
printf("%d ",n->data);
inorder(n->right);
}
}
void postorder(node* n){
if (n!=NULL){
postorder(n->left);
postorder(n->right);
printf("%d ",n->data);
}
}
void traverse(node* n){
if (n==NULL){
printf("EMPTY!\n");
return;
}
printf("Preorder:\n");
preorder(n);
printf("\n");
printf("Inorder:\n");
inorder(n);
printf("\n");
printf("Postorder:\n");
postorder(n);
printf("\n");
}
void main(){
int op,data;
printf("\tMENU\n");
while(1){
printf("1.Insert\n2.Delete\n3.Traverse\n4.Search\n5.Exit\nSelect the operation:");
scanf("%d",&op);
switch (op) {
case 1: printf("Enter the data to be inserted:");
scanf("%d",&data);
insert(data);
break;
case 2: printf("Enter the data to be deleted:");
scanf("%d",&data);
delete(data);
break;
case 3: traverse(root);
break;
case 4: printf("Enter the item to be searched:");
scanf("%d",&data);
search(data);
break;
case 5: exit(0);
break;
default: printf("Invalid Selection!\n");
}
}
}