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
/
Copy pathp23_0312.c
237 lines (219 loc) · 4.68 KB
/
p23_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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 23/0312
// Binary Tree using Linked List
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *lchild, *rchild;
};
struct node *root = NULL;
void exit_free(struct node *r);
struct node *search(struct node *r, int key){
struct node *x = NULL;
if(r->data == key){
return r;
}
else if(r->lchild != NULL){
x = search(r->lchild, key);
}
if(x == NULL && r->rchild != NULL){
x = search(r->rchild, key);
}
return x;
}
// Additional function to obtain parent
// of a key node
struct node *parent(struct node *r, struct node *key){
struct node *x = NULL;
if(r->lchild == key || r->rchild == key){
return r;
}
else if(r->lchild != NULL){
x = parent(r->lchild, key);
}
if(x == NULL && r->rchild != NULL){
x = parent(r->rchild, key);
}
return x;
}
void insert(int key, int item){
struct node *i = search(root, key);
if(i != NULL && (i->lchild == NULL || i->rchild == NULL)){
struct node *x = malloc(sizeof(struct node));
x -> data = item;
x -> lchild = NULL;
x -> rchild = NULL;
printf("\tChild Option: 1. Left 2. Right");
printf("\n\tEnter Choice : ");
int ch;
scanf("%d", &ch);
if(ch == 1 && i->lchild == NULL){
i->lchild = x;
}
else if(ch == 2 && i->rchild == NULL){
i->rchild = x;
}
else{
printf("Could Not Insert. Child Not Empty.");
free(x);
return;
}
}
else{
printf("\tInsertion Unsuccessful. Choose Leaf Node");
return;
}
printf("\tInsertion Successful.");
}
void del(int key){
struct node *ptr = search(root, key), *par;
if(ptr == NULL){
printf("\tCould Not Find %d", key);
return;
}
// Primary check to see if node
// is deletable. lc = rc = X
// Root also handled.
if(ptr -> lchild != NULL || ptr -> rchild != NULL){
printf("\tDeletion Unsuccessful. Choose a leaf node.");
return;
}
// Never expecting parent to be NULL
// unless ptr is root
par = parent(root, ptr);
if(par == NULL){
root = NULL;
}
else if(par->lchild == ptr){
par->lchild = NULL;
}
else{
par->rchild = NULL;
}
free(ptr);
printf("\tDeletion Successful.");
}
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);
}
}
void build(struct node *r){
int ch, item;
printf(" Does Node [%d] have left subtree [1-yes]: ", r->data);
scanf("%d", &ch);
if(ch == 1){
struct node *n = malloc(sizeof(struct node));
printf(" >>Enter Data: ");
scanf("%d", &item);
n -> data = item;
n -> lchild = NULL;
n -> rchild = NULL;
r -> lchild = n;
build(n);
}
printf(" Does Node [%d] have right subtree [1-yes]: ", r->data);
scanf("%d", &ch);
if(ch == 1){
struct node *n = malloc(sizeof(struct node));
printf(" >>Enter Data: ");
scanf("%d", &item);
n -> data = item;
n -> lchild = NULL;
n -> rchild = NULL;
r -> rchild = n;
build(n);
}
}
void main(){
int n, ch;
printf("--- Binary Tree Using Linked List ---");
while(1){
if(root == NULL){
printf("\n\n*** Build Binary Tree ***");
printf("\nEnter Root Element: ");
scanf("%d", &n);
struct node *x = malloc(sizeof(struct node));
x->data = n;
x->lchild = NULL;
x->rchild = NULL;
root = x;
build(root);
}
printf("\n\n*** Binary Tree Options ***");
printf("\n1. Search 2. Traverse 3. Insert 4. Delete 5. Exit\nChoice: ");
scanf("%d", &ch);
switch(ch){
case 1:{
printf("\tEnter element to search: ");
scanf("%d", &n);
if(search(root, n) == NULL){
printf("\tCould Not Find Element.");
}
else{
printf("\tElement Found.");
}
break;
}
case 2:{
printf("\t*** Traversal Option ***");
printf("\n\t1. Inorder 2. Preorder 3. Postorder");
printf("\n\tEnter Choice: ");
scanf("%d", &ch);
printf("\t\t");
switch(ch){
case 1: inorder(root); break;
case 2: preorder(root); break;
case 3: postorder(root); break;
}
break;
}
case 3:{
int pnode, item;
printf("\tEnter Parent Node: ");
scanf("%d", &pnode);
printf("\tEnter item to insert: ");
scanf("%d", &item);
insert(pnode, item);
break;
}
case 4:{
printf("\tEnter item to delete: ");
scanf("%d", &n);
del(n);
break;
}
default: exit_free(root); return;
}
}
}
void exit_free(struct node *r){
struct node *ptr = r;
if(ptr != NULL){
exit_free(ptr->lchild);
exit_free(ptr->rchild);
free(ptr);
}
}