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 pathp22_0312.c
196 lines (183 loc) · 3.61 KB
/
p22_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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 22/0312
// Binary Tree using Array
#include<stdio.h>
#define N 20
// Using character array
// 0 represent NUL
char A[N + 1];
void init(){
// Setting all values to NUL
for(int i = 0; i < N + 1; i++){
A[i] = 0;
}
}
int search(int index, char key){
int i = index, x;
if(A[i] == key){
return i;
}
else if(2 * i <= N){
x = search(2 * i, key);
}
else{
return 0;
}
if(x == 0 && 2 * i + 1 <= N){
x = search(2 * i + 1, key);
}
return x;
}
void insert(char key, char item){
int l = search(1, key);
if(l == 0){
printf("\tElement Not Found");
return;
}
if(A[2*l] == 0 || A[2*l + 1] == 0){
printf("\tChild Option: 1. Left 2. Right");
printf("\n\tEnter Choice : ");
int ch;
__fpurge(stdin);
scanf("%d", &ch);
if(ch == 1 && A[2*l] == 0){
A[2 * l] = item;
}
else if(ch == 2 && A[2 * l + 1] == 0){
A[2 * l + 1] = item;
}
else{
printf("\tInsertion Unsuccessful");
return;
}
}
else{
printf("\tInsertion Unsuccessful. Choose Leaf Node");
return;
}
printf("\tInsertion Successful");
}
void del(char key){
int l = search(1, key);
if(l == 0){
printf("\tElement Not Found");
}
else if(A[2*l] == 0 && A[2*l + 1] == 0){
A[l] = 0;
printf("\tDeletion Successful");
}
else{
printf("\tDeletion Unsuccessful. Node is not a leaf node");
}
}
void build(int l, char item){
if(l != 0){
char ch;
A[l] = item;
printf(" Does Node [%c] have left subtree [1-yes]: ", A[l]);
__fpurge(stdin);
scanf("%c", &ch);
if(ch == '1'){
printf(" >>Enter Item: ");
__fpurge(stdin);
scanf("%c", &ch);
build(2 * l, ch);
}
printf(" Does Node [%c] have right subtree [1-yes]: ", A[l]);
__fpurge(stdin);
scanf("%c", &ch);
if(ch == '1'){
printf(" >>Enter Item: ");
__fpurge(stdin);
scanf("%c", &ch);
build(2 * l + 1, ch);
}
}
}
void inorder(int index){
if(index <= N && A[index] != 0){
inorder(2 * index);
printf("%c ", A[index]);
inorder(2 * index + 1);
}
}
void preorder(int index){
if(index <= N && A[index] != 0){
printf("%c ", A[index]);
preorder(2 * index);
preorder(2 * index + 1);
}
}
void postorder(int index){
if(index <= N && A[index] != 0){
postorder(2 * index);
postorder(2 * index + 1);
printf("%c ", A[index]);
}
}
void main(){
init();
char ch;
printf("--- Binary Tree using Array with MAXSIZE %d ---", N);
int i;
while(1){
if(A[1] == 0){
printf("\n\n### Build Binary Tree ###\nEnter Root Item: ");
__fpurge(stdin);
scanf("%c", &ch);
build(1, ch);
}
printf("\n\n*** Binary Tree Options ***");
printf("\n1. Search 2. Traverse 3. Insert 4. Delete 5. Exit\nChoice: ");
__fpurge(stdin);
scanf("%d", &i);
switch(i){
case 1:{
printf("\tEnter element to search: ");
__fpurge(stdin);
scanf("%c", &ch);
if(search(1, ch) == 0){
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", &i);
printf("\t\t");
switch(i){
case 1: inorder(1); break;
case 2: preorder(1); break;
case 3: postorder(1); break;
}
break;
}
case 3:{
char pnode, item;
printf("\tEnter Parent Node: ");
__fpurge(stdin);
scanf("%c", &pnode);
printf("\tEnter item to insert: ");
__fpurge(stdin);
scanf("%c", &item);
insert(pnode, item);
break;
}
case 4:{
printf("\tEnter item to delete: ");
__fpurge(stdin);
scanf("%c", &ch);
del(ch);
break;
}
default: return;
}
}
}