Advertisement
KDOXG

Arvore_de_Pesquisa

Nov 26th, 2018
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef long TipoChave;
  5.  
  6. typedef struct Regist {
  7.     TipoChave Chave;
  8. } Registro;
  9.  
  10. struct No {
  11.     Registro Reg;
  12.     struct No *pEsq, *pDir;
  13. };
  14.  
  15. //-----------------------Funções
  16.  
  17. void Pesquisa(Registro *x, struct No *p);
  18.  
  19. void Inicializa(struct No *Dicionario);
  20.  
  21. void Insere(Registro x, struct No **p)
  22.  
  23. void Antecessor(struct No *q, struct No **r);
  24.  
  25. void Retira(Registro x, struct No **p);
  26.  
  27. //-----------------------Main
  28.  
  29. int main()
  30. {
  31.     struct No *arvore, *resgate;
  32.     Inicializa(arvore);
  33.     int confere;
  34.     Registro dado;
  35.  
  36.     Leia: printf("Digite: ");
  37.     scanf("%li", &dado.Chave);
  38.     Insere(dado,arvore);
  39.     printf("Feito!\n");
  40.     printf("Inserir outro número? ");
  41.     scanf("%d", confere);
  42.     if (confere == 1)
  43.         goto Leia;
  44.  
  45.     Pesquise: printf("Digite: ");
  46.     scanf("%li", &resgate->Reg.Chave);
  47.     Pesquisa(resgate,arvore);
  48.     printf("Feito!\n");
  49.     printf("Pesquisar por outro? ");
  50.     scanf("%d", confere);
  51.     if (confere == 1)
  52.         goto Pesquise;
  53.  
  54.     Remova: printf("Digite: ");
  55.     scanf("%li", &resgate->Reg.Chave);
  56.     Retira(resgate->Reg,arvore);
  57.     printf("Feito!\n");
  58.     printf("Remover outro? ");
  59.     scanf("%d", confere);
  60.     if (confere == 1)
  61.         goto Remova;
  62.  
  63.     return 0;
  64. }
  65.  
  66. //-----------------------Métodos
  67.  
  68. void Pesquisa(Registro *x, struct No *p)
  69. {
  70.     if (p == NULL)
  71.     {
  72.         printf("Erro : Registro nao esta presente na arvore\n");
  73.         return;
  74.     }
  75.     if (x->Chave < p->Reg.Chave)
  76.     {
  77.         Pesquisa(x, p->pEsq);
  78.         return;
  79.     }
  80.     if (x->Chave > p->Reg.Chave)
  81.         Pesquisa(x, p->pDir);
  82.     else
  83.         *x = p->Reg;
  84. }
  85.  
  86. void Inicializa(struct No *Dicionario)
  87. {
  88.     Dicionario = NULL;
  89. }
  90.  
  91. void Insere(Registro x, struct No **p)
  92. {
  93.     if ((*p) == NULL)
  94.     {
  95.         (*p) = (struct No*)malloc(sizeof(struct No));
  96.         (*p)->Reg = x;
  97.         (*p)->pEsq = NULL;
  98.         (*p)->pDir = NULL;
  99.         return;
  100.     }
  101.     if (x.Chave < (*p)->Reg.Chave)
  102.     {
  103.         Insere(x, &(*p)->pEsq);
  104.         return;
  105.     }
  106.     if (x.Chave > (*p)->Reg.Chave)
  107.         Insere(x, &(*p)->pDir);
  108.     else
  109.         printf("Erro : Registro ja existe na arvore\n");
  110. }
  111.  
  112.  
  113. void Retira(Registro x, struct No **p)
  114. {
  115.     if ((*p) == NULL)
  116.     {
  117.         printf("Erro : Registro nao esta na arvore\n");
  118.         return;
  119.     }
  120.     if (x.Chave < (*p)->Reg.Chave)
  121.     {
  122.         Retira(x, &(*p)->pEsq);
  123.         return;
  124.     }
  125.     if (x.Chave > (*p)->Reg.Chave)
  126.     {
  127.         Retira(x, &(*p)->pDir);
  128.         return;
  129.     }
  130.     if ((*p)->pDir == NULL)
  131.     {
  132.         struct No *Aux;
  133.         Aux = (*p);
  134.         (*p) = (*p)->pEsq;
  135.         free(Aux);
  136.         return;
  137.     }
  138.     if ((*p)->pEsq != NULL)
  139.     {
  140.         Antecessor((*p), (*p)->pEsq);
  141.         return;
  142.     }
  143.     // ((*p)->pEsq == NULL)
  144.     struct No *Aux;
  145.     Aux = (*p);
  146.     (*p) = (*p)->pDir;
  147.     free(Aux);
  148. }
  149.  
  150. void Antecessor(struct No *q, struct No **r)
  151. {
  152.     struct No *aux;
  153.     if (r->pDir != NULL)
  154.     {
  155.         Antecessor(q, &(*r)->pDir);
  156.         return;
  157.     }
  158.     q->Reg = (*r)->Reg;
  159.     aux = (*r);
  160.     (*r) = (*r)->pEsq;
  161.     free(aux);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement