Created
March 4, 2020 01:39
-
-
Save ivanantunes/138ca988a27fc37fb29255bdd62b6f56 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> | |
| #include <string.h> | |
| typedef struct stNo { | |
| char info; | |
| struct stNo *esquerda; | |
| struct stNo *direitra; | |
| } tNo; | |
| void print_arvore(tNo *no, int espaco) { | |
| int i; | |
| if (!no) { | |
| return; | |
| } | |
| print_arvore(no->direitra, espaco + 1); | |
| for (i = 0; i < espaco; ++i) { | |
| printf(" "); | |
| } | |
| printf("%c\n", no->info); | |
| print_arvore(no->esquerda, espaco + 1); | |
| } | |
| tNo *subArvore(tNo *raiz, tNo *no, char info) { | |
| if (!no) { | |
| no = (tNo *) malloc(sizeof(tNo)); | |
| if (!no) { | |
| printf("Sem Memoria\n"); | |
| exit(0); | |
| } | |
| no->esquerda = NULL; | |
| no->direitra = NULL; | |
| no->info = info; | |
| if (!raiz) { | |
| return no; | |
| } | |
| if (info < raiz->info) { | |
| raiz->esquerda = no; | |
| } else { | |
| raiz->direitra = no; | |
| } | |
| /*************************/ | |
| /*Não e Preciso Retornar!*/ | |
| /* return no; */ | |
| /*************************/ | |
| return no; | |
| } | |
| if (info < no->info) { | |
| subArvore(no, no->esquerda, info); | |
| } else { | |
| subArvore(no, no->direitra, info); | |
| } | |
| } | |
| int main(int argc, char** argv) { | |
| char letra[2]; | |
| tNo *raiz = NULL; | |
| do { | |
| printf("Insira um caractere ou vazio para sair: "); | |
| gets(letra); | |
| if (strlen(letra) > 1) { | |
| printf("Apenas um!\n"); | |
| continue; | |
| } | |
| if (*letra) { | |
| if (!raiz) { | |
| raiz = subArvore(raiz, raiz, *letra); | |
| } else { | |
| subArvore(raiz, raiz, *letra); | |
| } | |
| } | |
| } while(*letra); | |
| print_arvore(raiz, 0); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment