-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercício 02.c
141 lines (117 loc) · 3.03 KB
/
Exercício 02.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
//Escreva uma função que retorne a soma de todos os valores armazenados nos nós da árvore uma Árvore Binária de Busca.
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#include "stdio.h"
#include "stdlib.h"
typedef struct estrutura {
int info;
struct estrutura *esq;
struct estrutura *dir;
} NO;
typedef struct {
NO* raiz;
} ARVORE;
void inicializar(ARVORE *arv) {
arv->raiz = NULL;
}
int buscar(NO* noAtual, int infoProc) {
if (noAtual == NULL) {
return FALSE;
}
if (noAtual->info == infoProc) {
return TRUE;
}
if (noAtual->info < infoProc) {
return buscar(noAtual->dir, infoProc);
} else {
return buscar(noAtual->esq, infoProc);
}
}
int buscarNaArvore(ARVORE arv, int infoProc) {
return buscar(arv.raiz, infoProc);
}
void inserir(NO** noAtual, int novaInfo) {
if (*noAtual == NULL) {
NO* novoNo = (NO*) malloc(sizeof(NO));
novoNo->esq = NULL;
novoNo->dir = NULL;
novoNo->info = novaInfo;
*noAtual = novoNo;
} else if ((*noAtual)->info < novaInfo) {
return inserir(&((*noAtual)->dir), novaInfo);
} else {
return inserir(&((*noAtual)->esq), novaInfo);
}
}
void inserirNaArvore(ARVORE *arv, int novaInfo) {
inserir(&arv->raiz, novaInfo);
}
void preOrdem(NO* noAtual) {
if (noAtual != NULL) {
printf("%d ", noAtual->info);
preOrdem(noAtual->esq);
preOrdem(noAtual->dir);
}
}
void imprimir(ARVORE arv) {
preOrdem(arv.raiz);
}
void substituir(NO** noAtual, NO** noSub) {
if ((*noSub)->dir != NULL) {
substituir(&(*noAtual), &((*noSub)->dir));
} else {
(*noAtual)->info = (*noSub)->info;
(*noSub) = (*noSub)->esq;
}
}
int remover(NO** noAtual, int infoRemo) {
if ((*noAtual) == NULL) { //N?o existe
return FALSE;
} else if (infoRemo > (*noAtual)->info) { //Caso recursivo
return remover(&((*noAtual)->dir), infoRemo);
} else if (infoRemo < (*noAtual)->info) { //Caso recursivo
return remover(&((*noAtual)->esq), infoRemo);
} else { //Encontrei o caboco!
if ((*noAtual)->dir == NULL) { //CASO 1 e 2
(*noAtual) = (*noAtual)->esq;
} else if ((*noAtual)->esq == NULL) { //CASO 2
(*noAtual) = (*noAtual)->dir;
} else { //CASO 3
substituir(&(*noAtual), &((*noAtual)->esq));
}
return TRUE;
}
}
void removerNaArvore(ARVORE *arv, int infoRemo) {
int deuCerto = remover(&arv->raiz, infoRemo);
if (!deuCerto) {
printf("Elemento nao encontrado!\n");
}
}
int totalInfo(NO* no){
if (no != NULL){
return no->info + totalInfo(no->esq) + totalInfo(no->dir);
}
}
int calculaTotalInfo(ARVORE arv){
return totalInfo(arv.raiz);
}
void main() {
ARVORE arv;
inicializar(&arv);
printf("\n");
inserirNaArvore(&arv, 15);
inserirNaArvore(&arv, 20);
inserirNaArvore(&arv, 8);
inserirNaArvore(&arv, 10);
inserirNaArvore(&arv, 3);
inserirNaArvore(&arv, 1);
inserirNaArvore(&arv, 30);
inserirNaArvore(&arv, 100);
imprimir(arv);
printf("\n");
printf("\nA soma da informacoes dos nos eh : %d ", calculaTotalInfo(arv));
}