-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc.c
233 lines (198 loc) · 5.82 KB
/
func.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
#include <stdio.h>
#include <stdlib.h>
#include "arvore.h"
// Gera as constantes true e false
#define true 1
#define false 0
void mensagensAviso(int cod, int valor) {
printf("\nAVISO #%d: ", cod); // Exibe aviso, cod e depois a mensagem
switch (cod) {
case 1:
printf("O valor %d ja foi adicionado!", valor);
break;
case 2:
printf("O No %d nao foi localizado!", valor);
break;
case 3:
printf("O No %d nao possui descendentes! (folha)", valor);
break;
case 4:
printf("O No %d nao possui ancestrais! (raiz)", valor);
break;
default:
printf("Mensagem nao encontrada!");
break;
}
printf("\n");
}
void mensagensErro(int cod, char *nome) {
printf("\nERRO #%d: ", cod); // Exibe o erro e o cod e depois a mensagem
switch (cod) {
case 1:
printf("O arquivo \"%s\" nao foi aberto corretamente", nome);
break;
default:
printf("Mensagem nao encontrada!");
break;
}
printf("\n");
}
void iniciarArvore(Arvore **tree) { // Inicializa a arvore com valor "vazio"
*tree = NULL;
}
//Não esta preenchendo a arvore corretamente
void inserirNo(Arvore **tree, int valor) {
Arvore *aux = *tree;
if(valor == NULL) return;
if(estaVazia(*tree)) {
Arvore *novo;
novo = (Arvore*) malloc(sizeof(Arvore)); // Aloca um espaco na memoria para o novo No
novo->pai = NULL;
novo->dir = NULL;
novo->esq = NULL;
novo->dado = valor;
*tree = novo;
} else {
// Garante que nao haveram valores repetidos
// E que uma ordenacao de menor pra o maior
if(aux->dado == valor) {
mensagensAviso(1, valor);
return;
} else if(aux->dado > valor)
inserirNo(&aux->esq, valor);
else
inserirNo(&aux->dir,valor);
// Adiciona um pai aos Nos
if (aux->esq != NULL)
if(aux->esq->pai == NULL)
aux->esq->pai = aux;
if (aux->dir != NULL)
if(aux->dir->pai == NULL)
aux->dir->pai = aux;
}
}
void lerArquivo(Arvore **tree) {
int valor, ctd;
char *nome = "dados.txt"; // Salva o nome do arquivo
FILE *arq;
arq = fopen(nome, "r"); // Abre o arquivo para leitura
if(arq == NULL) {
mensagensErro(1, nome); // Printa mensagem de erro
exit(1);
}
for(ctd = 0; !feof(arq); ctd++) { // Le todo o arquivo
fscanf(arq, "%d", &valor); // Le valor por valor
inserirNo(tree, valor); // Insere no No
}
fclose(arq); // Encerra a leitura e fecha o arquivo
}
//verifica se o no esta vazio
int estaVazia(Arvore *tree) {
if(tree == NULL)
return true;
else
return false;
}
// Verifica se o no tem filhos
int ehNoFolha(Arvore *tree) {
if(tree->dir == NULL && tree->esq == NULL)
return true;
else
return false;
}
// Verifica se o no é raiz
int ehNoRaiz(Arvore *tree) {
if(tree->pai == NULL)
return true;
else
return false;
}
// Busca um no na arvore e o retorna
Arvore* buscar(Arvore *tree,int valor) {
while(tree != NULL) {
if (tree->dado == valor)
return tree; // Retorna o No caso ele seja encontrado
else if (tree->dado > valor)
tree = tree->esq;
else
tree = tree->dir;
}
return tree; // Retorna NULL caso o No nao exista
}
// Printa somente os nos folha
void printarNosFolha(Arvore *tree) {
if (!estaVazia(tree)) {
// Printa so o que for folha
if (ehNoFolha(tree))
printf("%d ", tree->dado);
printarNosFolha(tree->esq);
printarNosFolha(tree->dir);
}
}
void printarNosRamo(Arvore *tree) {
if (!estaVazia(tree)) {
// Printa tudo que nao for no folha ou raiz
if (!ehNoFolha(tree) && !ehNoRaiz(tree))
printf("%d ", tree->dado);
printarNosRamo(tree->esq);
printarNosRamo(tree->dir);
}
}
int alturaEProfundidade(Arvore *tree) {
int esq, dir;
if (estaVazia(tree))
return -1; // Garante que o primeiro No valido será zero
esq = alturaEProfundidade(tree->esq); // Conta a profundidade a esquerda
dir = alturaEProfundidade(tree->dir); // Conta a profundidade a direita
if (esq > dir) // Retorna so a maior profundidade encontrada
return esq+1;
else
return dir+1;
}
int grauDoNo(Arvore *tree) {
int filhos = 0;
if (tree->dir != NULL) // Se houver filho a direita soma +1
filhos++;
if(tree->esq != NULL) // Se houver filho a esquerda soma +1
filhos++;
return filhos; // Retorna o grau do No
}
int profundidadeDoNo(Arvore *tree) {
int profundidade = 0;
while(!ehNoRaiz(tree)) { // Para ao encontrar a raiz
tree = tree->pai; // Sobe ate o no raiz
profundidade++; // Conta os pulos
}
return profundidade;
}
void printarDescendentes(Arvore *tree, int valor) {
emOrdem(tree->esq); // Exibe primeiro os Nos de menor valor
emOrdem(tree->dir); // Depois os de maior
}
void printarAncestrais(Arvore *tree, int valor) {
while(!ehNoRaiz(tree)) { // Para ao encontrar a raiz
tree = tree->pai; // Sobe ate o no raiz
printf("%d ", tree->dado); // Exibe todos os nos encontrados
}
}
void preOrdem(Arvore *tree) { //Gui
if(tree != NULL){
printf("%d ", tree->dado); // Processa e pula
preOrdem(tree->esq);
preOrdem(tree->dir);
}
}
void posOrdem(Arvore *tree) { //Gui
if(tree != NULL){
posOrdem(tree->esq);
posOrdem(tree->dir);
printf("%d ", tree->dado); // Pula e processa
}
}
void emOrdem(Arvore *tree) {
if (!estaVazia(tree)) {
emOrdem(tree->esq);
printf("%d ", tree->dado); // Pula, processa e continua
emOrdem(tree->dir);
}
}