#include <stdlib.h>
#include <stdio.h>
typedef int tree_info;
typedef struct no_arvbin {
struct no_arvbin *esq;
tree_info dado;
struct no_arvbin *dir;
} arvbin;
void preordem(arvbin *t);
/**Ex 1**/
void preordemprintafolha(arvbin *t);
/**Ex 2**/
void encontraMaior(arvbin *t, int *maior);
arvbin *criaRaiz();
void destroiArvBin(arvbin *t);
int main() {
arvbin *a = criaRaiz();
arvbin *b = criaRaiz();
arvbin *c = criaRaiz();
arvbin *d = criaRaiz();
a->dado = 6;
b->dado = 3;
c->dado = 2;
d->dado = 4;
a->esq = b;
a->dir = c;
b->esq = d;
int maior = -1;
encontraMaior(a, &maior);
destroiArvBin(a);
return 0;
}
arvbin *criaRaiz() {
arvbin
*t
= (arvbin
*) malloc(sizeof(arvbin
*)); t->esq = NULL;
t->dir = NULL;
return t;
}
void destroiArvBin(arvbin *t) {
if (t == NULL) return;
destroiArvBin(t->esq);
destroiArvBin(t->dir);
}
void preordem(arvbin *t) {
if (t != NULL) {
preordem(t->esq);
preordem(t->dir);
}
}
void preordemprintafolha(arvbin *t) {
if (t != NULL) {
if (t->dir == NULL && t->esq == NULL) {
}
preordemprintafolha(t->esq);
preordemprintafolha(t->dir);
}
}
void encontraMaior(arvbin *t, int *maior) {
/*system("pause\n");
printf("%d ", *maior);
printf(" %p\n", maior);
system("pause");*/
if (t != NULL) {
if (*maior < t->dado) {
*maior = t->dado;
//printf("%p %p", maior, &t->dado);
}
encontraMaior(t->esq, maior);
encontraMaior(t->dir, maior);
}
}
I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCnR5cGVkZWYgaW50IHRyZWVfaW5mbzsKdHlwZWRlZiBzdHJ1Y3Qgbm9fYXJ2YmluIHsKICAgIHN0cnVjdCBub19hcnZiaW4gKmVzcTsKICAgIHRyZWVfaW5mbyBkYWRvOwogICAgc3RydWN0IG5vX2FydmJpbiAqZGlyOwp9IGFydmJpbjsKCnZvaWQgcHJlb3JkZW0oYXJ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0gTlVMTCkgewogICAgICAgIHByaW50ZigiJWRcbiIsIHQtPmRhZG8pOwogICAgICAgIHByZW9yZGVtKHQtPmVzcSk7CiAgICAgICAgcHJlb3JkZW0odC0+ZGlyKTsKICAgIH0KfQoKdm9pZCBwcmVvcmRlbXByaW50YWZvbGhhKGFydmJpbiAqdCkgewogICAgaWYgKHQgIT0gTlVMTCkgewogICAgICAgIGlmICh0LT5kaXIgPT0gTlVMTCAmJiB0LT5lc3EgPT0gTlVMTCkgewogICAgICAgICAgICBwcmludGYoIiVkXG4iLCB0LT5kYWRvKTsKICAgICAgICB9CiAgICAgICAgcHJlb3JkZW1wcmludGFmb2xoYSh0LT5lc3EpOwogICAgICAgIHByZW9yZGVtcHJpbnRhZm9saGEodC0+ZGlyKTsKICAgIH0KfQoKdm9pZCBlbmNvbnRyYU1haW9yKGFydmJpbiAqdCwgaW50ICptYWlvcikgewogICAgLypzeXN0ZW0oInBhdXNlXG4iKTsKICAgIHByaW50ZigiJWQgIiwgKm1haW9yKTsKICAgIHByaW50ZigiICVwXG4iLCBtYWlvcik7CiAgICBzeXN0ZW0oInBhdXNlIik7Ki8KICAgIGlmICh0ICE9IE5VTEwpIHsKICAgICAgICBpZiAoKm1haW9yIDwgdC0+ZGFkbykgewogICAgICAgICAgICAqbWFpb3IgPSB0LT5kYWRvOwogICAgICAgICAgICAvL3ByaW50ZigiJXAgJXAiLCBtYWlvciwgJnQtPmRhZG8pOwogICAgICAgIH0KCiAgICAgICAgZW5jb250cmFNYWlvcih0LT5lc3EsIG1haW9yKTsKICAgICAgICBlbmNvbnRyYU1haW9yKHQtPmRpciwgbWFpb3IpOwogICAgfQp9