-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_bin_search_static.c
126 lines (116 loc) · 2.87 KB
/
tree_bin_search_static.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXTREE 30
int cont, i, x, cont, op;
int tree[MAXTREE];
int insercao(int *tree, int i, int x){
if(i>=MAXTREE)
return -1;
if(tree[i] == -1){
tree[i] = x;
return (i);
}
if(x<tree[i]){
return (insercao(tree, i*2, x));
}
return (insercao(tree, ((i*2)+1), x));
}
void percuso_pre(int tree, int i){
if(i<MAXTREE){
if(tree[i] != -1){
printf("%d \n", tree[i]);
percuso_pre(tree, i*2);
percuso_pre(tree, (i*2)+1);
}
}
}
void percuso_inter(int tree, int i){
if(i<MAXTREE){
if(tree[i] != -1){
percuso_inter(tree, i*2);
printf("%d \n", tree[i]);
percuso_inter(tree, (i*2)+1);
}
}
}
void percuso_pos(int tree, int i){
if(i<MAXTREE){
if(tree[i] != -1){
percuso_pos(tree, i*2);
percuso_pos(tree, (i*2)+1);
printf("%d \n", tree[i]);
}
}
}
int busca(int tree, int i, int x){
if(i<MAXTREE){
if(tree[i] == (-1)){
return (-1);
}
else if(tree[i] == x){
return i;
}
else if(x<tree[i]){
return (busca(tree, i*2, x));
}
return (busca(tree, (i*2)+1, x));
}
return (-1);
}
void esvazia(int *tree){
for(cont=0; cont<MAXTREE; cont++){
tree[cont] = (-1);
}
}
int main(){
i=0;
esvazia(&tree);
printf("********MENU********\n");
printf("\n1- Insercao\n");
printf("2- Percurso\n");
printf("3- Busca\n");
printf("4- Esvazia\n");
printf("5- Sair\n");
while(op != 5){
switch(op){
case 1:
printf("Digite um valor a ser inserido: ");
scanf("%d ", &x);
i++;
insercao(&tree, 1, x);
break;
case 2:
printf("\n 1- Prefixado");
printf("\n 2- Interfixado");
printf("\n 3- Posfixado");
printf("\n 4- Sair\n");
while(op != 4){
switch(op){
case 1:
i++;
percuso_pre(tree, i);
break;
case 2:
i++;
percuso_inter(tree, i);
break;
case 3:
i++;
percuso_pos(tree, i);
break;
}
}
break;
case 3:
printf("Digite um valor a ser Buscado: ");
scanf("%d ", &x);
busca(tree, i, x);
break;
case 4:
esvazia(tree);
break;
}
}
getch();
}