Skip to content

Instantly share code, notes, and snippets.

@luizcarraro
Created October 2, 2017 11:37
Show Gist options
  • Save luizcarraro/599ac62ef3efb5673f232100e290446e to your computer and use it in GitHub Desktop.
Save luizcarraro/599ac62ef3efb5673f232100e290446e to your computer and use it in GitHub Desktop.
Arvore Binária do Brunão (Com suporte a Linux 64bits)
#include <stdio.h>
#include <stdlib.h>
typedef struct nodo{
int valor;
struct nodo *esquerda, *direita;
}Nodo;
Nodo *arvore;
void inicializa(Nodo **t);
void insere(Nodo **t, int n);
void P_digitacao();
void P_menuPrincipal();
void P_ImprimirEmOrdem(Nodo *t);
void P_ImprimirPreOrdem(Nodo *t);
void P_ImprimirPosOrdem(Nodo *t);
void inicializa(Nodo **t){
*t = NULL;
}
void insere(Nodo **t, int n){
if(*t == NULL){
*t = malloc(sizeof(Nodo));
(**t).direita = NULL;
(**t).esquerda = NULL;
(**t).valor = n;
}else{
if (n > (**t).valor){
insere(&(**t).direita, n);
}else{
insere(&(**t).esquerda, n);
}
}
}
void P_digitacao(){
int valor;
do{
printf("Digite um valor Inteiro.. ");
scanf("%d", &valor);
if(valor != 0){
insere(&arvore, valor);
}else{
break;
}
}while(1);
P_menuPrincipal();
}
void P_ImprimirEmOrdem(Nodo *t){
if(t != NULL){
P_ImprimirEmOrdem((*t).esquerda);
printf("%d\n", (*t).valor );
P_ImprimirEmOrdem((*t).direita );
}
}
void P_ImprimirPreOrdem(Nodo *t){
if(t != NULL){
printf("%d\n", (*t).valor );
P_ImprimirPreOrdem((*t).esquerda);
P_ImprimirPreOrdem((*t).direita );
}
}
void P_ImprimirPosOrdem(Nodo *t){
if(t != NULL){
P_ImprimirPosOrdem((*t).esquerda);
P_ImprimirPosOrdem((*t).direita );
printf("%d\n", (*t).valor );
}
}
void P_menuPrincipal(){
system("clear");
int n;
printf("\n------Escolha uma Opcao------\n");
printf("[1] - Inserir item\n" );
printf("[2] - Listar em Ordem\n" );
printf("[3] - Listar em Pre-Ordem\n" );
printf("[4] - Listar em Pos-Ordem\n" );
printf("[5] - Sair\n" );
printf("-----------------------------\n");
scanf("%d", &n);
switch(n){
case 1:
P_digitacao();
break;
case 2:
system("clear");
printf("IMPRESSAO EM ORDEM \n");
P_ImprimirEmOrdem(arvore);
getchar(); getchar();
P_menuPrincipal();
break;
case 3:
system("clear");
printf("IMPRESSAO EM PRE-ORDEM \n");
P_ImprimirPreOrdem(arvore);
getchar(); getchar();
P_menuPrincipal();
break;
case 4:
system("clear");
printf("IMPRESSAO EM POS-ORDEM \n");
P_ImprimirPosOrdem(arvore);
getchar(); getchar();
P_menuPrincipal();
break;
case 5:
break;
}
}
void main(){
inicializa(&arvore);
P_menuPrincipal();
}
@emanoelqueiroz
Copy link

Arvore binária tope 🕺 🕺 🕺 🕺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment