This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lista_arvores_60.c
89 lines (81 loc) · 1.54 KB
/
lista_arvores_60.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
#include <stdio.h>
#include<stdlib.h>
typedef struct node{
int i;
struct node *dir,*esq;
}node;
void add(int insere, node **p);
void imprime_pre_ordem(node **p);
void limpa(node **p);
node* novo_nodo(int insere);
int qtd_divisores_soma(node **p);
int main(int argc, char const *argv[])
{
node **p;
p=(node**)malloc(sizeof(node*));
*p = NULL;
add(4,p);
add(2,p);
add(1,p);
add(3,p);
add(6,p);
add(5,p);
add(7,p);
add(8,p);
add(0,p);
printf("Árvore: \n");
imprime_pre_ordem(p);
printf("\n%d nodos são divisores da soma de seus filhos.\n",qtd_divisores_soma(p));
limpa(p);
free(p);
return 0;
}
void add(int insere, node **p){
if(*p==NULL){
*p=novo_nodo(insere);
return;
}
else if(insere < (*p)->i){
add(insere,&(*p)->esq);
}
else if(insere > (*p)->i){
add(insere,&(*p)->dir);
}
}
void imprime_pre_ordem(node **p){
if(*p!=NULL){
printf("%d ",(*p)->i);
imprime_pre_ordem(&(*p)->esq);
imprime_pre_ordem(&(*p)->dir);
}
}
void limpa(node **p){
if(*p!=NULL){
limpa(&(*p)->esq);
limpa(&(*p)->dir);
free(*p);
}
}
node* novo_nodo(int insere){
node *n = (node*) malloc(sizeof(node));
if(n==NULL){
printf("ERRO\n");
exit(1);
}
n->esq=NULL;
n->dir=NULL;
n->i = insere;
return n;
}
int qtd_divisores_soma(node **p){
int i=0;
if(*p==NULL){
return 0;
}
else if(((*p)->esq->i + (*p)->dir->i)%(*p)->i==0){
printf("\n%d é divisor de %d + %d.",(*p)->i, (*p)->esq->i,(*p)->dir->i );
return 1;
}
i = qtd_divisores_soma(&(*p)->esq) + qtd_divisores_soma(&(*p)->dir);
return i;
}