You are on page 1of 14

Estruturas de Dados

Exerccios rvores Binrias e Genricas

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias


4.2. Implemente uma funo que retorne a quantidade de folhas de uma rvore binria. Essa funo deve obedecer ao prottipo:
int folhas (Arv* a);

4.4. Implemente uma funo que compare se duas rvores binrias so iguais. Essa funo deve obedecer ao prottipo:
Arv* igual (Arv* a, Arv* b);

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias


/* Lista II Exerccio 4.2 */ int folhas (Arv* a) { if (arv_vazia(a->esq) && arv_vazia(a->dir)) return 1; else if (!arv_vazia(a->esq) && arv_vazia(a->dir)) return folhas(a->esq); else if (arv_vazia(a->esq) && !arv_vazia(a->dir)) return folhas(a>dir); return folhas(a->esq) + folhas(a->dir); }

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias


/* Lista II Exerccio 4.4 */ int igual (Arv* a, Arv* b) { return arv_vazia(a) && arv_vazia(b) || (!arv_vazia(a) && !arv_vazia(b) && a->info == b->info && igual(a->esq, b->esq) && igual(a->dir, b->dir)); }

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias


int ocorrencias_x (Arv* a, int x) { if (a==NULL) return 0; if (a->info == x) return 1 + ocorrencias_x(a->dir, x); if (a->info < x) return ocorrencias_x(a->dir, x); return ocorrencias_x(a->esq, x); }

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias


int imprime_folhas (Arv* a) { if (a==NULL) return 0; if ((a->esq == NULL) && (a->dir == NULL)) /* imprime s as folhas */ {printf(%d,a->info); return 1;} imprime_folhas(a->dir); /* em ordem no-crescente */ imprime_folhas(a->esq); }

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias

6/6/2006

(c) Dept. Informtica - PUC-Rio

Exerccios rvores Binrias


void imprime(Arv* a) { /* supe expresso no vazia */ if ((a->esq == NULL) && (a->dir == NULL)) printf(%g,a->valor); else { imprime(a->esq); imprime(a->dir); printf(%c,a->op); } }

6/6/2006

(c) Dept. Informtica - PUC-Rio

10

Exerccios rvores Binrias


float avalia(Arv* a) { /* supe expresso no vazia */ if ((a->esq == NULL) && (a->dir == NULL)) return a->valor; else { if(a->op == +) return avalia(a->esq) + avalia(a->dir); if(a->op == -) return avalia(a->esq) - avalia(a->dir); if(a->op == *) return avalia(a->esq) * avalia(a->dir); if(a->op == /) return avalia(a->esq) / avalia(a->dir); } }

6/6/2006

(c) Dept. Informtica - PUC-Rio

11

rvores Genricas
5.2. Implemente uma funo que retorne a quantidade de folhas de uma rvore com nmero varivel de filhos. Essa funo deve obedecer ao prottipo:
int folhas (ArvVar* a);

5.4. Implemente uma funo que compare se duas rvores so iguais. Essa funo deve obedecer ao prottipo:
ArvVar* igual (ArvVar* a, ArvVar* b);

6/6/2006

(c) Dept. Informtica - PUC-Rio

12

rvores Genricas
/* Lista II Exerccio 5.2 */ int folhas (ArvVar* a) { ArvVar* p; int n = 0; if (a->prim == NULL) return 1; for (p=a->prim; p!=NULL; p=p->prox) {n = n + folhas(p);}; return n; }

6/6/2006

(c) Dept. Informtica - PUC-Rio

13

rvores Genricas
/* Lista II Exerccio 5.4 */ int igual (ArvVar* a, ArvVar* b) { ArvVar* p; ArvVar* q; int n = 0; if (a == NULL && b == NULL) return 1; for (p=a->prim, q=b->prim; p!=NULL && q!=NULL; p=p->prox, q=q->prox) {if (!igual(p,q)) return 0;}; return 1; }

6/6/2006

(c) Dept. Informtica - PUC-Rio

14

You might also like