Created
November 20, 2017 17:22
-
-
Save Rafael11j/0c1393c959f392b9440c8c195f6806cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define true 1 | |
#define false 0 | |
#include <windows.h> | |
#ifdef WIN32 | |
void gotoxy(int coluna, int linha){ | |
COORD point; | |
point.X = coluna; | |
point.Y = linha; | |
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point); | |
} | |
#endif | |
typedef unsigned char bool; | |
typedef struct elemento{ | |
int num; | |
}t_elemento; | |
typedef struct no{ | |
struct no *esquerda; | |
t_elemento dado; | |
struct no *direita; | |
}t_no; | |
typedef t_no* t_arvore; | |
t_no *criaNo(void){ | |
t_no *novo = NULL; | |
novo = (t_no*) malloc(sizeof(t_no)); | |
if (novo != NULL) { | |
novo->esquerda = novo->direita = NULL; | |
novo->dado.num = 0; | |
} | |
return novo; | |
} | |
bool isVazia(t_no *no){ | |
bool resultado = true; | |
if (no == NULL) | |
resultado = false; | |
return resultado; | |
} | |
int compara(t_elemento item1, t_elemento item2){ | |
int resultado = 0; | |
if (item1.num > item2.num) | |
resultado = 1; | |
else if (item1.num < item2.num) | |
resultado = -1; | |
return resultado; | |
} | |
t_no *busca(t_arvore raiz, t_elemento dado){ | |
t_no *no = NULL; | |
if (raiz != NULL) { | |
if(compara(raiz->dado, dado) == 0) | |
no = raiz; | |
else{ | |
no = busca(raiz->esquerda, dado); | |
if(no == NULL) | |
no = busca(raiz->direita, dado); | |
} | |
} | |
return no; | |
} | |
t_no *buscaSetPai(t_arvore raiz, t_elemento dado, t_no **pai){ | |
t_no *no = NULL; | |
if(raiz == NULL) | |
*pai = NULL; | |
else | |
{ | |
if(compara(raiz->dado, dado) == 0) | |
no = raiz; | |
else | |
{ | |
*pai = raiz; | |
if(compara(raiz->dado, dado) > 0) | |
no = buscaSetPai(raiz->esquerda, dado, pai); | |
else | |
no = buscaSetPai(raiz->direita, dado, pai); | |
} | |
} | |
return no; | |
} | |
bool inserir (t_arvore *raiz, t_elemento item){ | |
bool resultado = false; | |
if(*raiz == NULL) | |
{ | |
*raiz = criaNo(); | |
if(*raiz != NULL) | |
{ | |
(*raiz)->dado = item; | |
resultado = true; | |
} | |
} | |
else | |
{ | |
if(compara((*raiz)->dado, item) > 0) | |
resultado = inserir(&((*raiz)->esquerda), item); | |
else if(compara((*raiz)->dado, item) < 0) | |
resultado = inserir(&((*raiz)->direita), item); | |
} | |
return resultado; | |
} | |
bool remover (t_arvore *raiz, t_elemento item){ | |
bool resultado = false; | |
t_no *no, *pai, *sub, *paiSuc, *suc; | |
no = pai = sub = paiSuc = suc = NULL; | |
no = buscaSetPai(*raiz, item, &pai); | |
if(no != NULL) | |
{ | |
if(no->esquerda == NULL) | |
sub = no->direita; | |
else | |
{ | |
if(no->direita == NULL) | |
sub = no->esquerda; | |
else | |
{ | |
paiSuc = no; | |
sub = no->direita; | |
suc = sub->esquerda; | |
while(suc != NULL) | |
{ | |
paiSuc = sub; | |
sub = suc; | |
suc = sub->esquerda; | |
} | |
if(paiSuc != no) | |
{ | |
paiSuc->esquerda = sub->direita; | |
sub->direita = no->direita; | |
} | |
sub->esquerda = no->esquerda; | |
} | |
} | |
if(pai == NULL) | |
*raiz = sub; | |
else | |
{ | |
if(no == pai->esquerda) | |
pai->esquerda = sub; | |
else | |
pai->direita = sub; | |
free(no); | |
resultado = true; | |
} | |
} | |
return resultado; | |
} | |
void esvaziar (t_arvore *raiz){ | |
if(*raiz != NULL) | |
{ | |
esvaziar(&(*raiz)->esquerda); | |
esvaziar(&(*raiz)->direita); | |
free(*raiz); | |
*raiz = NULL; | |
} | |
} | |
void exibeGraficamente(t_arvore raiz, int col, int lin, int desloc){ | |
if(raiz == NULL) | |
return; | |
gotoxy(col,lin); | |
printf("%d", raiz->dado.num); | |
if(raiz->esquerda != NULL) | |
exibeGraficamente(raiz->esquerda,col-desloc,lin+2,desloc/2+1); | |
if(raiz->direita != NULL) | |
exibeGraficamente(raiz->direita,col+desloc,lin+2,desloc/2+1); | |
} | |
void exibirPreOrdem (t_arvore raiz){ | |
if(raiz != NULL) | |
{ | |
printf("%d ", raiz->dado.num); | |
exibirPreOrdem(raiz->esquerda); | |
exibirPreOrdem(raiz->direita); | |
} | |
} | |
void exibirInOrdem (t_arvore raiz){ | |
if(raiz != NULL) | |
{ | |
exibirInOrdem(raiz->esquerda); | |
printf("%d ", raiz->dado.num); | |
exibirInOrdem(raiz->direita); | |
} | |
} | |
void exibirPosOrdem (t_arvore raiz){ | |
if(raiz != NULL) | |
{ | |
exibirPosOrdem(raiz->esquerda); | |
exibirPosOrdem(raiz->direita); | |
printf("%d ", raiz->dado.num); | |
} | |
} | |
int menu (void){ | |
int op = 0; | |
system("cls"); | |
fflush(stdin); | |
printf("ALUNO: RAFAEL ANTONIO VASCONCELOS JAPYASSU\n"); | |
printf("DISCIPLINA: ESTRUTURA DE DADOS I\n"); | |
printf("PROFESSOR: WALACE BONFIM\n\n\n"); | |
printf("\t\tEDITOR DE ARVORE\n\n"); | |
printf("1 - INSERIR\n"); | |
printf("2 - REMOVER\n"); | |
printf("3 - PESQUISAR\n"); | |
printf("4 - ESVAZIAR\n"); | |
printf("5 - EXIBIR\n"); | |
printf("6 - EXIBIR GRAFICAMENTE\n"); | |
printf("0 - SAIR\n\n"); | |
printf("DIGITE SUA OPCAO: "); | |
scanf("%d", &op); | |
fflush(stdin); | |
if((op < 0) || (op > 6)) | |
op = menu(); | |
return op; | |
} | |
int main() { | |
int op = 0; | |
t_elemento dado, dado1, dado2, dado3, dado4, dado5, dado6, dado7; | |
t_arvore raiz = NULL; | |
FILE *fp; | |
fp = fopen("arquivo.txt","r"); | |
fscanf(fp, "%d %d %d %d %d %d %d", &dado1.num, &dado2.num, &dado3.num, &dado4.num ,&dado5.num, &dado6.num, &dado7.num); | |
fclose(fp); | |
inserir(&raiz, dado1); | |
inserir(&raiz, dado2); | |
inserir(&raiz, dado3); | |
inserir(&raiz, dado4); | |
inserir(&raiz, dado5); | |
inserir(&raiz, dado6); | |
inserir(&raiz, dado7); | |
do | |
{ | |
op = menu(); | |
switch(op) | |
{ | |
case 1: | |
{ | |
printf("\n"); | |
printf("Digite o numero que deseja inserir: "); | |
scanf("%d", &dado.num); | |
if(inserir(&raiz, dado)) | |
printf("\nO numero %d foi inserido na arvore!\n\n", dado); | |
system("pause"); | |
break; | |
} | |
case 2: | |
{ | |
printf("\n"); | |
printf("Digite o numero para ser removido da arvore: "); | |
scanf("%d", &dado.num); | |
remover(&raiz, dado); | |
system("pause"); | |
break; | |
} | |
break; | |
case 3: | |
{ | |
printf("\n"); | |
printf("Digite o numero para verificar se ele esta na arvore: "); | |
scanf("%d", &dado.num); | |
if(busca(raiz, dado) != NULL) | |
printf("O numero %d esta na arvore!\n\n", dado.num); | |
else | |
printf("O numero %d nao esta na arvore!\n\n", dado.num); | |
system("pause"); | |
break; | |
} | |
case 4: | |
{ | |
esvaziar(&raiz); | |
printf("A arvore foi esvaziada!\n\n"); | |
system("pause"); | |
break; | |
} | |
case 5: | |
{ | |
printf("\nPre ordem: "); | |
exibirPreOrdem(raiz); | |
printf("\n\nIn ordem: "); | |
exibirInOrdem(raiz); | |
printf("\n\nPos ordem: "); | |
exibirPosOrdem(raiz); | |
printf("\n\n\n"); | |
system("pause"); | |
break; | |
} | |
case 6: | |
{ | |
system("cls"); | |
exibeGraficamente(raiz, 30, 1, 10); | |
printf("\n\n\n\n\n\n\n"); | |
system("pause"); | |
break; | |
} | |
case 0: | |
{ | |
break; | |
} | |
} | |
}while(op != 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment