4

Click here to load reader

Árvore Binária em C

Embed Size (px)

DESCRIPTION

Exemplo de Árvore Binária com alocação dinâmica de memória em linguagem C.

Citation preview

Page 1: Árvore Binária em C

#include <stdio.h>#include <stdlib.h>//#include <conio.h>

struct arvore{ struct arvore *esq; char dado; struct arvore *dir;} *raiz;

struct arvore* cria_no(struct arvore *atual,char dado,char lado){ struct arvore *no;

no = (struct arvore*) malloc(sizeof(struct arvore)); (*no).esq = NULL;

(*no).dado = dado; (*no).dir = NULL; if (!raiz) raiz = no; else if (lado == 'e')

(*atual).esq = no; else

(*atual).dir = no; return(no);}

void insere_arv(struct arvore *no_filho,char lado,char dado){ char carac; struct arvore *sub_arv; if(lado == 'r') printf("\nRAIZ = "); else if(lado == 'e') printf("\nESQ DE %c = ",dado); else printf("\nDIR DE %c = ",dado); carac = getche(); if(carac != 13)//se digitar <enter> passa p/ o próximo { sub_arv = cria_no(no_filho,carac,lado); insere_arv(sub_arv,'e',carac); insere_arv(sub_arv,'d',carac); }}

Page 2: Árvore Binária em C

void mostra(struct arvore *pont_arv){ char esquerda, direita; struct arvore *no_atual; if(pont_arv) { no_atual = (*pont_arv).esq; if(no_atual) esquerda = (*no_atual).dado; else esquerda = ' '; no_atual = (*pont_arv).dir; if(no_atual) direita = (*no_atual).dado; else direita = ' '; printf("\n [ %c ] <--- [ %c ] ---> [ %c ]",esquerda,(*pont_arv).dado,direita); mostra((*pont_arv).esq); mostra((*pont_arv).dir); }}

void mostra2(struct arvore *pont_arv, int cont,char lado){ char raiz_2; int i; struct arvore *no_atual; if(pont_arv) { raiz_2 = (*pont_arv).dado; if(cont) { if(cont>1) { printf("|"); for(i=0;i<cont;i++) printf(" "); } printf("|"); for(i=0;i<cont;i++) printf("_"); } printf("%c[%c]\n",lado,raiz_2); no_atual = (*pont_arv).esq; if(no_atual)//SIM-esq { cont++; mostra2(no_atual,cont,'e');

Page 3: Árvore Binária em C

} else//NÃO-esq { cont++; //Se você quiser mostrar traços no local das posições vazias, desmarque o bloco abaixo /*printf("|"); for(i=0;i<cont;i++) printf("-"); printf("\n");*/ } no_atual = (*pont_arv).dir; if(no_atual)//SIM-dir mostra2(no_atual,cont,'d'); else//NÃO-dir { //Se você quiser mostrar traços no local das posições vazias, desmarque o bloco abaixo /*printf("|"); for(i=0;i<cont;i++) printf("-"); printf("\n");*/ } }}

void limpa_heap(struct arvore *pont_arv){ struct arvore *no_atual; if(pont_arv) { no_atual = (*pont_arv).esq; if(no_atual)//SIM-esq limpa_heap(no_atual); no_atual = (*pont_arv).dir; if(no_atual)//SIM-dir limpa_heap(no_atual); free(pont_arv); printf("."); sleep(500); }}

int main(){ raiz = NULL; printf("\n******* ARVORE BINARIA ORIENTADA *******\n"); printf("\n (pressione <enter> p/ elemento vazio)\n"); insere_arv(raiz,'r',' '); if(raiz)

Page 4: Árvore Binária em C

{ //printf("\nRaiz = %c",(*raiz).dado); printf("\n\n******* Exibindo o relacionamento entre os elementos *******\n"); mostra(raiz); printf("\n\n******* Exibindo em formato semelhante a 'Arvore de Diretorios' *******\n"); mostra2(raiz,0,'r'); } else printf("\nArvore vazia"); printf("\n"); system("PAUSE"); printf("\nLIMPANDO A MEMORIA HEAP, AGUARDE"); limpa_heap(raiz); return 0;}