-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeArray.cpp
232 lines (180 loc) · 3.38 KB
/
BinaryTreeArray.cpp
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
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#define max 100
static char t[max];
int level, s;
void create(int parent);
void print();
void printArray();
void preorder(int root);
void inorder(int root);
void postorder(int root);
void insert();
void deletion(int in);
int search(char val);
int main()
{
int choice;
printf("Creation Time!!! \n");
char val;
do
{
printf("\nEnter the number of levels \n");
scanf("%d",&level);
s=pow(2,(level+1))-1;
}while(s>max);
printf("\nEnter the value for the root node \n");
fflush(stdin);
scanf(" %c",&t[0]);
create(0);
printf("Tree Created, Mothafucka!!! \n");
while(1)
{
printf("Pick an option, rasta! \n 1.Good ol' print \n 2.Print array, as is \n 3. Preorder print \n 4.Inorder print \n 5.Postorder print \n 6.Insert an element \n 7.Delete an element \n 8.Search for an element \n 9.Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1: print();
break;
case 2: printArray();
break;
case 3: preorder(0);
break;
case 4: inorder(0);
break;
case 5: postorder(0);
break;
case 6: insert();
break;
case 7: printf("Enter the element to be deleted \n");
fflush(stdin);
scanf(" %c",&val);
if(search(val)==-1)
printf("Element not found");
else
deletion(search(val));
break;
case 8: printf("Enter the value to be searched \n");
fflush(stdin);
scanf(" %c",&val);
printf("The element is found at the index %d \n",search(val));
break;
case 9: exit(0);
default: printf("Wrong choice, biatch! Pick again!!!");
}
}
}
void create(int parent)
{
if(parent<s) //NOT less than or equal to!!!
{
if(t[parent]!='#')
{
printf("\n Enter the left child for %c : \n",t[parent]);
fflush(stdin);
scanf(" %c",&t[2*parent +1]);
create(2*parent +1);
printf(" \nEnter the right child for %c : \n",t[parent]);
fflush(stdin);
scanf(" %c",&t[2*parent +2]);
create(2*parent +2);
}
else
{
t[2*parent+1]='#';
create(2*parent +1);
t[2*parent +2]='#';
create(2*parent +2);
}
}
}
void print()
{
int i=0;
printf("(\t Node \t,\t Index \t ) \n");
for(;i<s;i++)
if(t[i]!='#')
printf("(\t %c \t,\t %d \t ) \n",t[i],i);
}
void printArray()
{
int i=0;
printf("Index \t Value \n");
for(;i<s;i++)
printf("%d \t %c \n",i,t[i]);
}
void preorder(int root)
{
if(t[root]=='#')
return;
printf("%c",t[root]);
printf("\t");
preorder(2*root+1);
preorder(2*root+2);
}
void inorder(int root)
{
if(t[root]=='#')
return;
inorder(2*root+1);
printf("%c",t[root]);
printf("\t");
inorder(2*root+2);
}
void postorder(int root)
{
if(t[root]=='#')
return;
postorder(2*root+1);
postorder(2*root+2);
printf("%c \t",t[root]);
}
void insert()
{
int i=0;
for(;i<s;i++)
{
if(t[i]=='#')
break;
}
if(i==s)
{
printf("No more elements can be inserted! Max no. of nodes for the specified no. of levels reached! \n");
return;
}
printf("Enter the value to be inserted \n");
fflush(stdin);
scanf(" %c",&t[i]);
printf("\n Value inserted");
}
int search(char val)
{
int i=0;
for(;i<s;i++)
{
if(t[i]==val)
break;
}
if(i==s)
return -1;
return i;
}
void deletion(int in)
{
int i=s-1;
if(t[2*in+1]=='#' && t[2*in+2]=='#')
{
t[in]='#';
printf("Deleted!");
return;
}
for(;i>in;i--)
{
if(t[i]!='#')
break;
}
t[in]=t[i];
t[i]='#';
printf("Deleted!");
}