-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_tree.c
339 lines (281 loc) · 7.32 KB
/
bin_tree.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include "headers/conio2.h"
#include "headers/bst.h"
#include "headers/cmd.h"
// prot. functions
void show_tree(); // menu >> insert(+)/remove(-)/exit(1)/show_menu()
void show_text(int n); // entradas
void show_msg(); //
// structs
struct tm* DH;
/*
19 de março de 2020
Departamento de T.I., UniProjeção (Taguatinga, Campus I)
Sistemas de Informação (3º semestre)
Autor: Yan Almeida Garcia - 2019 199 05
Disciplina: Estrutura de Dados - João Evangelista
00) montagem >> árvore binária de busca:
- Inserção.
- Remoção.
- exibição:
- árvore completa - raiz, sub-arv. esquerda e sub-arv. direita;
- em ordem;
- pré-ordem;
- pós-ordem;
- Buscar elemento na árvore - mostragem de tempo de busca
Referência bibliográfica:
[01]. "Árvore Binária com a Linguagem C", disponível em: http://ninjacode.com.br/arvore-binaria-com-a-linguagem-c/
[02]. "Programar em C/Árvores binárias de Busca", disponível em: https://pt.wikibooks.org/wiki/Programar_em_C/%C3%81rvores_bin%C3%A1rias_de_Busca
[03]. "Árvores Binárias de Busca", disponível em: https://www.cin.ufpe.br/~dmrac/aula%20de%20arvore%20binaria%20de%20busca.pdf
*/
// global vars.
int i, j, k;
char op;
int main()
{
system("mode 162"); // size screen >> 162px largura
show_header();
return 0;
}
// functions
// menu >> insert(+)/remove(-)/exit(1)/show_menu()
void show_tree()
{
int a = 2, b = 63, bb = b + 21, c = 7, d = 36, e = 4, k;
int inserts, removes, search, rnd = 0;
clock_t t;
arq = fopen(txt, "a");
system("title Binary Search Tree Viewer - /bin_");
system("COLOR 07");
Node *root = tree_initialize();
do {
system("cls");
textcolor(WHITE);
borders(41, 151, 4, a);
show_text(2);
// exibir >> ordens e max/min
if (root != NULL)
{
textbackground(CYAN);
tree_prints(root, 79, 4, 18);
textbackground(BLACK);
movexy(c, 40, "Pre Order: ");
print_preOrder(root);
movexy(c, 41, "in Order: ");
print_inOrder(root);
movexy(c, 42, "Post Order: ");
print_postOrder(root);
// elemento max. e min. da árvore
movexy(146, 40, "Max: ");
print_max(root);
movexy(146, 42, "Min: ");
print_min(root);
}
else
{
textcolor(WHITE);
textbackground(GREEN);
movexy(73, 4, " TREE IS EMPTY. ");
movexy(72, 5, " INSERT SOMETHING ");
}
gotoxy(160, 45);
op = toupper(getch());
switch (op)
{
// insere elemento aleatorio
case 'M':
if (root == NULL)
{
srand(time(NULL));
for (i = 0; i < 18; i++)
{
rnd = ((double) rand() / ((double) RAND_MAX + 1) * 99) - 30;
root = tree_inserts(root, created_item(rnd));
}
textbackground(BLACK);
}
else
{
textcolor(WHITE);
textbackground(BLUE);
movexy(71, d, " TREE IS NOT VOID ");
movexy(67, d + 1, " R TO RESET AND TRY AGAIN ");
getch();
textbackground(BLACK);
}
textbackground(BLACK);
break;
// insere elemento
case '+':
system("title Binary Search Tree Viewer - Inserts ");
textbackground(GREEN);
movexy(b, a, " Inserts < + > ");
tree_prints(root, 79, 4, 13);
show_text(0); // green color
movexy(c - 1, e, " Insert something: ");
scanf(" %d", &inserts);
fflush(stdin);
root = tree_inserts(root, created_item(inserts));
break;
// remove elemento
case '-':
system("title Binary Search Tree Viewer - Removes ");
if (root != NULL)
{
textbackground(RED);
movexy(bb, a, " Removes < - > ");
tree_prints(root, 79, 4, 13);
show_text(1); // green color
movexy(c - 1, e, " Remove something: ");
scanf(" %d", &removes);
fflush(stdin);
root = tree_removes(root, removes);
}
else
{
show_msg();
}
break;
// limpa a arvore
case 'R':
tree_free(root);
show_tree();
break;
// backup da árvore >> exit(2)
case 'B':
system("title Binary Search Tree Viewer - Backup ");
textbackground(BLACK);
if (root != NULL)
{
time_t segundos;
time(&segundos);
DH = localtime(&segundos);
if (arq != NULL)
{
textcolor(WHITE);
textbackground(BLUE);
movexy(65, d, " BACKUP PERFORMED SUCCESSFULLY! ");
movexy(69, d + 1, " THE PROGRAM WILL ENDED ");
fprintf(arq, "%.2d/%.2d/%.4d ", DH->tm_mday, DH->tm_mon + 1, DH->tm_year + 1900);
fprintf(arq, "- %.2d:%.2d:%.2d", DH->tm_hour, DH->tm_min, DH->tm_sec);
fprintf(arq, "\nPre order: ");
fwrite_preOrder(root);
fprintf(arq, "\nIn order: ");
fwrite_inOrder(root);
fprintf(arq, "\nPost order: ");
fwrite_postOrder(root);
fprintf(arq, "\n\n");
fclose(arq);
tree_free(root);
textbackground(BLACK);
getch();
system("cls");
exit(2);
}
else
{
textcolor(WHITE);
textbackground(BLUE);
movexy(70, d, " DIRECTORY NOT FOUND! ");
movexy(67, d + 1, " MAKE A NEW FOLDER: 'files' ");
textbackground(BLACK);
getch();
}
}
else
{
show_msg();
}
break;
// procura elemento
case 'S':
system("title Binary Search Tree Viewer - Search ");
if (root != NULL)
{
textbackground(BLACK);
movexy(c - 1, e, " Search something: ");
scanf(" %d", &search);
fflush(stdin);
t = clock(); //armazena tempo
Node* tmp = tree_searchs(root, search);
if (tmp != NULL)
{
movexy(c, e + 2, "Found element");
}
else
{
movexy(c, e + 2, "Not found element");
}
t = clock() - t;
gotoxy(c, e + 3);
printf("Runtime: %lf", ((double)t)/((CLOCKS_PER_SEC/1000)));
getch();
}
else
{
show_msg();
}
break;
default:
textbackground(BLACK);
break;
}
} while (op != 27);
tree_free(root);
system("cls");
system("title Binary Search Tree Viewer - Yan Almeida Garcia (2019 199 05)");
system("color 0f");
exit(1);
}
// entrada de textos
void show_text(int n)
{
int a = 2, b = 63, bb = b + 21, d = 44;
if (n == 0)
{
textbackground(BLACK);
movexy(bb, a, " Removes < - > ");
movexy(139, a, " Exit < esc > ");
movexy(8, d, " Random Inserts < m > ");
movexy(b + 2, d, " Reset < r > ");
movexy(bb, d, " Search < s > ");
movexy(139, d, " Backup < b > ");
}
else if (n == 1)
{
textbackground(BLACK);
movexy(b, a, " Inserts < + > ");
movexy(139, a, " Exit < esc > ");
movexy(8, d, " Random Inserts < m > ");
movexy(b + 2, d, " Reset < r > ");
movexy(bb, d, " Search < s > ");
movexy(139, d, " Backup < b > ");
}
else if (n == 2)
{
movexy(b, a, " Inserts < + > ");
movexy(bb, a, " Removes < - > ");
movexy(139, a, " Exit < esc > ");
movexy(8, d, " Random Inserts < m > ");
movexy(b + 2, d, " Reset < r > ");
movexy(bb, d, " Search < s > ");
movexy(139, d, " Backup < b > ");
}
}
//
void show_msg()
{
int d = 36;
system("title Binary Search Tree Viewer - Add Something ");
textcolor(WHITE);
textbackground(BLUE);
movexy(73, d - 1, " Inserts < + > ");
movexy(78, d + 0, " or ");
movexy(70, d + 1, " Random Inserts < m > ");
getch();
textbackground(BLACK);
}