Advertisement
mig_eu

Arvore Binaria Aula

May 20th, 2021
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct no {
  6.     struct no * esq;
  7.     int idade;
  8.     struct no * dir;
  9. } t_no;
  10.  
  11. typedef t_no* t_arvore;
  12.  
  13. void exibirPreOrdem(t_arvore tree){
  14.    
  15.     if (tree!=NULL){
  16.         printf("%d ", tree->idade);
  17.         exibirPreOrdem(tree->esq);
  18.         exibirPreOrdem(tree->dir);
  19.     }
  20. }
  21.  
  22. void exibirInOrdem(t_arvore tree){
  23.    
  24.     if (tree!=NULL){
  25.         exibirInOrdem(tree->esq);
  26.         printf("%d ", tree->idade);
  27.         exibirInOrdem(tree->dir);
  28.     }
  29. }
  30.  
  31. void exibirPosOrdem(t_arvore tree){
  32.    
  33.     if (tree!=NULL){
  34.         exibirPosOrdem(tree->esq);
  35.         exibirPosOrdem(tree->dir);
  36.         printf("%d ", tree->idade);
  37.     }
  38. }
  39.  
  40. int isVazia(t_arvore tree){
  41.     return(tree == NULL);
  42. }
  43.  
  44. t_no * criar (){
  45.    
  46.     t_no * novo;
  47.    
  48.     novo = (t_no*) malloc(sizeof(t_no));
  49.  
  50.     if (novo == NULL)
  51.         return NULL;
  52.    
  53.     novo->esq = novo->dir = NULL;
  54.     novo->idade = -1;
  55.  
  56.     return (novo);
  57. }
  58.  
  59. int insereRaiz(t_arvore* tree, int idd){
  60.    
  61.     t_no* novo;
  62.    
  63.     if (*tree != NULL)
  64.         return 0; // erro: ja existe raiz
  65.    
  66.     novo = criar();
  67.    
  68.     if (novo == NULL)
  69.         return 0; // erro: memoria insuficiente
  70.    
  71.     novo->idade = idd;
  72.    
  73.     *tree = novo;
  74.    
  75.     return 1;
  76. }
  77.  
  78. t_no *busca(t_arvore tree, int idd){
  79.    
  80.     t_no* achou;
  81.    
  82.     if(tree == NULL){
  83.         return NULL;
  84.     }
  85.    
  86.     if(tree->idade == idd){
  87.         return tree;
  88.     }
  89.  
  90.     achou = busca(tree->esq, idd);
  91.  
  92.     if(achou == NULL){
  93.         achou = busca(tree->dir, idd);
  94.     }
  95.  
  96.     return achou;
  97. }
  98.  
  99. // Inserir um filho aa direita de um dado noh
  100. int insereDireita(t_arvore tree, int pai, int filho)
  101. {
  102.     t_no *f, *p, *novo;
  103.    
  104.     // verifica se o elemento ja nao existe
  105.     f = busca(tree,filho);
  106.    
  107.     if (f != NULL){
  108.         return 0; // erro: dado ja existente
  109.     }
  110.    
  111.     // busca o pai e verifica se ja nao possui filho direito
  112.     p = busca(tree,pai);
  113.    
  114.     if(p == NULL){
  115.         return 0; // erro: pai nao encontrado
  116.     }
  117.    
  118.     if(p->dir != NULL){
  119.         return 0; // erro: ja existe filho direito
  120.     }
  121.    
  122.     novo = criar();
  123.    
  124.     if(novo == NULL){
  125.         return 0; // erro: memoria insuficiente
  126.     }
  127.  
  128.     novo->idade = filho;
  129.    
  130.     p->dir = novo;
  131.    
  132.     return 1;
  133. }
  134.  
  135. // Inserir um filho a esquerda de um dado noh
  136. int insereEsquerda(t_arvore tree, int pai, int filho)
  137. {
  138.     t_no *f, *p, *novo;
  139.    
  140.     // verifica se o elemento ja nao existe
  141.     f = busca(tree,filho);
  142.    
  143.     if (f != NULL){
  144.         return 0; // erro: dado ja existente
  145.     }
  146.    
  147.     // busca o pai e verifica se ja nao possui filho direito
  148.     p = busca(tree,pai);
  149.    
  150.     if(p == NULL){
  151.         return 0; // erro: pai nao encontrado
  152.     }
  153.    
  154.     if(p->esq != NULL){
  155.         return 0; // erro: ja existe filho direito
  156.     }
  157.    
  158.     novo = criar();
  159.    
  160.     if(novo == NULL){
  161.         return 0; // erro: memoria insuficiente
  162.     }
  163.  
  164.     novo->idade = filho;
  165.    
  166.     p->esq = novo;
  167.    
  168.     return 1;
  169. }
  170.  
  171. int main(void){
  172.    
  173.     t_arvore mArv = NULL;
  174.     t_arvore aux;
  175.    
  176.     insereRaiz(&mArv, 21);
  177.    
  178.     if(isVazia(mArv)){
  179.         printf("Arvore vazia\n");
  180.     }
  181.     else{
  182.         printf("Arvore nao vazia\n");
  183.     }
  184.    
  185.     insereDireita(mArv, 21, 35);
  186.    
  187.     exibirPreOrdem(mArv);
  188.     printf("\n");
  189.     exibirInOrdem(mArv);
  190.     printf("\n");
  191.     exibirPosOrdem(mArv);
  192.     printf("\n");
  193.    
  194.     insereEsquerda(mArv, 21, 12);
  195.    
  196.     exibirPreOrdem(mArv);
  197.     printf("\n");
  198.     exibirInOrdem(mArv);
  199.     printf("\n");
  200.     exibirPosOrdem(mArv);
  201.     printf("\n");
  202.    
  203.     insereEsquerda(mArv, 12, 10);
  204.     insereEsquerda(mArv, 35, 40);
  205.    
  206.     exibirPreOrdem(mArv);
  207.     printf("\n");
  208.     exibirInOrdem(mArv);
  209.     printf("\n");
  210.     exibirPosOrdem(mArv);
  211.     printf("\n");
  212.    
  213.     aux = busca(mArv, 35);
  214.    
  215.     if(aux != NULL){
  216.         printf("Valor %d encontrado\n", aux->idade);
  217.     }
  218.     else{
  219.         printf("Valor %d nao encontrado\n", 35);
  220.     }
  221.    
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement