This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p24_0312.c
317 lines (293 loc) · 5.36 KB
/
p24_0312.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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 24/0312
// Binary Search Tree using Linked List
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *lchild, *rchild;
};
struct node *root = NULL;
void insert();
void inorder_nr();
void inorder(struct node *r);
void postorder(struct node *r);
void preorder(struct node *r);
void search();
void del(int item);
void exit_free(struct node *r);
void main()
{
while(1){
printf("\n\n*** Tree Options ***");
printf("\n1. Insert 2. Traverse 3. Search 4. Delete 5. Exit");
int ch;
printf("\nEnter Choice: ");
scanf("%d", &ch);
switch(ch){
case 1: insert(); break;
case 2:{
if(root == NULL){
printf("\tEmpty Binary Search Tree");
break;
}
printf("\t*** Traversal Option ***");
printf("\n\t1. Inorder(NR) 2. Inorder 3. Preorder 4. Postorder");
printf("\n\tEnter Choice: ");
scanf("%d", &ch);
printf("\t\t");
switch(ch){
case 1: inorder_nr(); break;
case 2: inorder(root); break;
case 3: preorder(root); break;
case 4: postorder(root); break;
}
break;
}
case 3:{
if(root == NULL){
printf("\tEmpty Binary Search Tree");
break;
}
search();
break;
}
case 4:{
if(root == NULL){
printf("\tEmpty Binary Search Tree");
break;
}
int item;
printf("\tEnter item to delete : ");
scanf("%d", &item);
del(item);
break;
}
default: exit_free(root); return;
}
}
}
void insert(){
struct node *x = malloc(sizeof(struct node));
printf("\tEnter data to insert: ");
scanf("%d", &(x->data));
x->lchild = NULL;
x->rchild = NULL;
if(root == NULL){
root = x;
printf("\tSuccessfully Inserted.");
return;
}
struct node *ptr, *parent;
ptr = root;
while(ptr != NULL){
parent = ptr;
if(x->data > ptr->data){
ptr = ptr->rchild;
}
else if(x->data < ptr->data){
ptr = ptr->lchild;
}
else{
printf("\tDuplicate Element. Could Not Insert.");
free(x);
return;
}
}
if(x->data > parent->data){
parent->rchild = x;
}
else{
parent->lchild = x;
}
printf("\tSuccessfully Inserted.");
}
void inorder(struct node *r){
struct node *ptr = r;
if(ptr != NULL){
inorder(ptr->lchild);
printf("%d ", ptr->data);
inorder(ptr->rchild);
}
}
void preorder(struct node *r){
struct node *ptr = r;
if(ptr != NULL){
printf("%d ", ptr->data);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}
void postorder(struct node *r){
struct node *ptr = r;
if(ptr != NULL){
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ", ptr->data);
}
}
// *** Stack using SLL ***
// START -----------------
struct snode{
struct node *data;
struct snode *link;
};
struct snode *TOP = NULL;
struct snode *temp;
void push(struct node *item){
int d;
struct snode *a = malloc(sizeof(struct snode));
a->data = item;
a->link = TOP;
TOP = a;
}
struct node *pop(){
if(TOP == NULL){
printf("\tStack Underflow");
}
else{
temp = TOP;
TOP = TOP->link;
struct node *item = temp->data;
free(temp);
return item;
}
}
// END -------------------
// *** Stack using SLL ***
void inorder_nr(){
// NR = Non Recursive
struct node *ptr = root;
while(TOP != NULL || ptr != NULL){
if(ptr != NULL){
push(ptr);
ptr = ptr->lchild;
}
else{
ptr = pop();
printf("%d ", ptr->data);
ptr = ptr->rchild;
}
}
}
void search(){
int item;
printf("\tEnter item to search : ");
scanf("%d", &item);
struct node *ptr = root;
while(ptr != NULL){
if(item > ptr -> data){
ptr = ptr->rchild;
}
else if(item < ptr->data){
ptr = ptr->lchild;
}
else{
printf("\t%d found.", item);
return;
}
}
printf("\t%d could not be found.", item);
}
struct node *succ(struct node *ptr){
ptr = ptr->rchild; //mutate
while(ptr->lchild != NULL){
ptr = ptr->lchild;
}
return ptr;
}
void del(int item){
int flag = 0;
struct node *ptr = root, *parent = root;
while(ptr != NULL){
if(item == ptr->data){
flag = 1;
break;
}
// ALGO EDIT R->R->R
// To avoid parent == ptr
parent = ptr;
if(item > ptr -> data){
ptr = ptr->rchild;
}
else{
ptr = ptr->lchild;
}
}
if(flag == 0){
printf("\t%d could not be found.", item);
return;
}
// Three cases as is from ppt algorithm
int ch;
if(ptr->lchild == NULL && ptr->rchild == NULL){
ch = 1;
}
else if(ptr->lchild != NULL && ptr->rchild != NULL){
ch = 3;
}
else{
ch = 2;
}
switch(ch){
case 1:{
if(ptr == root){
root = NULL; // Add condition
}
else if(parent->lchild == ptr){
parent->lchild = NULL;
}
else{
parent->rchild = NULL;
}
free(ptr);
break;
}
case 2:{
if(ptr == root){
if(ptr->lchild == NULL){
root = ptr->rchild;
}
else{
root = ptr->lchild;
}
}
else if(parent->lchild == ptr){
if(ptr->lchild == NULL){
parent->lchild = ptr->rchild;
}
else{
parent->lchild = ptr->lchild;
}
}
else{
if(ptr->lchild == NULL){
parent->rchild = ptr->rchild;
}
else{
parent->rchild = ptr->lchild;
}
}
free(ptr);
break;
}
case 3:{
struct node *sp = succ(ptr);
int sd = sp->data;
del(sd);
ptr->data = sd;
return; // Just to avoid printing feedback twice
}
}
printf("\tSuccessfully deleted.");
}
void exit_free(struct node *r){
struct node *ptr = r;
if(ptr != NULL){
exit_free(ptr->lchild);
exit_free(ptr->rchild);
free(ptr);
}
}