-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_using_arrays.c
More file actions
223 lines (186 loc) · 4.92 KB
/
binary_tree_using_arrays.c
File metadata and controls
223 lines (186 loc) · 4.92 KB
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
#include<stdio.h>
#include<math.h>
int a[100] = {[0 ... 99] = -1};
int size, h, loc = 0;
void display()
{
int i;
for(i = 1; i <= size; i++)
{
if(a[i] != -1)
printf("%d ", a[i]);
else
printf("- ");
}
}
void buildtree(int i, int item)
{
int newL, newR, ch;
if(i != 0)
{
a[i] = item;
printf("Does %d have left child? - 1. Yes, 2. No: ", a[i]);
scanf("%d", &ch);
if(ch == 1)
{
printf("Enter data of left child: ");
scanf("%d", &newL);
buildtree(2 * i, newL);
}
else
buildtree(0, 0);
printf("Does %d have right child? - 1. Yes, 2. No: ", a[i]);
scanf("%d", &ch);
if(ch == 1)
{
printf("Enter data of right child: ");
scanf("%d", &newR);
buildtree(2 * i + 1, newR);
}
else
buildtree(0, 0);
}
}
int search(int i, int key)
{
if(a[i] == -1 || a[i] == key)
{
if(a[i] == key)
loc = i;
return i;
}
else
{
if(a[search(2 * i, key)] == -1)
search(2 * i + 1, key);
}
}
void insert(int key)
{
int ch, item;
search(1, key);
if(a[loc] != key)
{
printf("Parent node not found.");
return;
}
if(a[2 * loc] == -1 || a[2 * loc + 1] == -1)
{
printf("Insert as - 1. Left child, 2. Right child: ");
scanf("%d", &ch);
if(ch == 1)
{
if(a[2 * loc] == -1)
{
printf("Enter data to insert: ");
scanf("%d", &item);
if(2 * loc > size)
{
h++;
size = pow(2, h + 1) - 1;
}
a[2 * loc] = item;
}
else
printf("Left child is not empty.");
}
else
{
if(a[2 * loc + 1] == -1)
{
printf("Enter data to insert: ");
scanf("%d", &item);
if(2 * loc + 1 > size)
{
h++;
size = pow(2, h + 1) - 1;
}
a[2 * loc + 1] = item;
}
else
printf("Right child is not empty.");
}
}
else
printf("Left and right children are not empty.");
}
void delete(int key)
{
int k, flag = 0;
search(1, key);
if(a[loc] == key)
{
if(a[2 * loc] == -1 && a[2 * loc + 1] == -1)
{
a[loc] = -1;
for(k = pow(2, h); k <= size; k++)
{
if(a[k] != -1)
flag = 1;
}
if(flag == 0)
{
h--;
size = pow(2, h + 1) - 1;
}
}
else
printf("%d is not a leaf node.", key);
}
else
printf("Node does not exist.");
}
void main()
{
int i, ch, data;
printf("Enter height of the tree: ");
scanf("%d", &h);
size = pow(2, h + 1) - 1;
printf("Enter root: ");
scanf("%d", &data);
buildtree(1, data);
printf("Binary Tree: ");
display();
do
{
printf("\n\tMENU");
printf("\n1. Insert\t2. Delete\n3. Search\t4. Exit");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter parent node of new node: ");
scanf("%d", &data);
insert(data);
printf("\nBinary Tree:\t");
display();
break;
case 2: if(h >= 0)
{
printf("\nEnter node to delete: ");
scanf("%d", &data);
delete(data);
if(h >= 0)
{
printf("\nBinary Tree:\t");
display();
}
else
printf("Tree is empty.");
}
else
printf("Tree is empty.");
break;
case 3: printf("\nEnter node to search: ");
scanf("%d", & data);
search(1, data);
if(a[loc] == data)
printf("\nNode found.");
else
printf("\nNode not found.");
printf("\nBinary Tree:\t");
display();
break;
}
}while(ch >= 1 && ch <= 3);
}