Advertisement
KDOXG

Arvore_de_Pesquisa_AVL

Dec 8th, 2018
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.41 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. int FB (struct No *pRaiz);
  28.  
  29. int Altura(struct No *pRaiz);
  30.  
  31. void RotacaoSimplesEsquerda(struct No **ppRaiz);
  32.  
  33. void RotacaoSimplesDireita(struct No **ppRaiz);
  34.  
  35. int BalancaEsquerda(struct No **ppRaiz);
  36.  
  37. int BalancaDireita(struct No **ppRaiz);
  38.  
  39. int Balanceamento(struct No **ppRaiz);
  40.  
  41. int Inserir(struct No **ppRaiz, Registro x);
  42.  
  43. int EhArvoreAVL(struct No *pRaiz);
  44.  
  45. //-----------------------Main
  46.  
  47. int main()
  48. {
  49.     struct No *arvore, *resgate=malloc(sizeof(struct No));
  50.     Inicializa(arvore);
  51.     int confere;
  52.     Registro dado;
  53.  
  54.     Leia: printf("Digite: ");
  55.     scanf("%li", &dado.Chave);
  56.     Insere(dado,&arvore);
  57.     printf("Feito!\n");
  58.     printf("Inserir outro número? ");
  59.     scanf("%d", &confere);
  60.     if (confere == 1)
  61.         goto Leia;
  62.  
  63.     Pesquise: printf("Digite: ");
  64.     scanf("%li", &resgate->Reg.Chave);
  65.     Pesquisa(&resgate->Reg,arvore);
  66.     printf("Feito!\n");
  67.     printf("Pesquisar por outro? ");
  68.     scanf("%d", &confere);
  69.     if (confere == 1)
  70.         goto Pesquise;
  71.  
  72.     Remova: printf("Digite: ");
  73.     scanf("%li", &resgate->Reg.Chave);
  74.     Retira(resgate->Reg,&arvore);
  75.     printf("Feito!\n");
  76.     printf("Remover outro? ");
  77.     scanf("%d", &confere);
  78.     if (confere == 1)
  79.         goto Remova;
  80.  
  81.     return 0;
  82. }
  83.  
  84. //-----------------------Métodos
  85.  
  86. void Pesquisa(Registro *x, struct No *p)
  87. {
  88.     if (p == NULL)
  89.     {
  90.         printf("Erro : Registro nao esta presente na arvore\n");
  91.         return;
  92.     }
  93.     if (x->Chave < p->Reg.Chave)
  94.     {
  95.         Pesquisa(x, p->pEsq);
  96.         return;
  97.     }
  98.     if (x->Chave > p->Reg.Chave)
  99.         Pesquisa(x, p->pDir);
  100.     else
  101.         *x = p->Reg;
  102. }
  103.  
  104. void Inicializa(struct No *Dicionario)
  105. {
  106.     Dicionario = NULL;
  107. }
  108.  
  109. void Insere(Registro x, struct No **p)
  110. {
  111.     if ((*p) == NULL)
  112.     {
  113.         (*p) = (struct No*)malloc(sizeof(struct No));
  114.         (*p)->Reg = x;
  115.         (*p)->pEsq = NULL;
  116.         (*p)->pDir = NULL;
  117.         return;
  118.     }
  119.     if (x.Chave < (*p)->Reg.Chave)
  120.     {
  121.         Insere(x, &(*p)->pEsq);
  122.         return;
  123.     }
  124.     if (x.Chave > (*p)->Reg.Chave)
  125.         Insere(x, &(*p)->pDir);
  126.     else
  127.         printf("Erro : Registro ja existe na arvore\n");
  128. }
  129.  
  130.  
  131. void Retira(Registro x, struct No **p)
  132. {
  133.     if ((*p) == NULL)
  134.     {
  135.         printf("Erro : Registro nao esta na arvore\n");
  136.         return;
  137.     }
  138.     if (x.Chave < (*p)->Reg.Chave)
  139.     {
  140.         Retira(x, &(*p)->pEsq);
  141.         return;
  142.     }
  143.     if (x.Chave > (*p)->Reg.Chave)
  144.     {
  145.         Retira(x, &(*p)->pDir);
  146.         return;
  147.     }
  148.     if ((*p)->pDir == NULL)
  149.     {
  150.         struct No *Aux;
  151.         Aux = (*p);
  152.         (*p) = (*p)->pEsq;
  153.         free(Aux);
  154.         return;
  155.     }
  156.     if ((*p)->pEsq != NULL)
  157.     {
  158.         Antecessor((*p), &(*p)->pEsq);
  159.         return;
  160.     }
  161.     // ((*p)->pEsq == NULL)
  162.     struct No *Aux;
  163.     Aux = (*p);
  164.     (*p) = (*p)->pDir;
  165.     free(Aux);
  166. }
  167.  
  168. void Antecessor(struct No *q, struct No **r)
  169. {
  170.     struct No *aux;
  171.     if ((*r)->pDir != NULL)
  172.     {
  173.         Antecessor(q, &(*r)->pDir);
  174.         return;
  175.     }
  176.     q->Reg = (*r)->Reg;
  177.     aux = (*r);
  178.     (*r) = (*r)->pEsq;
  179.     free(aux);
  180. }
  181.  
  182. int FB (struct No *pRaiz)
  183. {
  184.     if (pRaiz == NULL)
  185.         return 0;
  186.  
  187.     return Altura(pRaiz->pEsq) - Altura(pRaiz->pDir);
  188. }
  189.  
  190. int Altura(struct No *pRaiz)
  191. {
  192.     int iEsq,iDir;
  193.     if (pRaiz == NULL)
  194.         return 0;
  195.     iEsq = Altura(pRaiz->pEsq);
  196.     iDir = Altura(pRaiz->pDir);
  197.     if ( iEsq > iDir )
  198.         return iEsq + 1;
  199.     else
  200.         return iDir + 1;
  201. }
  202.  
  203. void RotacaoSimplesEsquerda(struct No **ppRaiz)
  204. {
  205.     struct No *pAux;
  206.     pAux = (*ppRaiz)->pDir;
  207.     (*ppRaiz)->pDir = pAux->pEsq;
  208.     pAux->pEsq = (*ppRaiz);
  209.     (*ppRaiz) = pAux;
  210. }
  211.  
  212. void RotacaoSimplesDireita(struct No **ppRaiz)
  213. {
  214.     struct No *pAux;
  215.     pAux = (*ppRaiz)->pEsq;
  216.     (*ppRaiz)->pEsq = pAux->pDir;
  217.     pAux->pDir = (*ppRaiz);
  218.     (*ppRaiz) = pAux;
  219. }
  220.  
  221. int BalancaEsquerda(struct No **ppRaiz)
  222. {
  223.     int fbe = FB ((*ppRaiz)->pEsq);
  224.     if (fbe > 0)
  225.     {
  226.         RotacaoSimplesDireita(ppRaiz);
  227.         return 1;
  228.     }
  229.     else if (fbe < 0)
  230.     { //Rotação Dupla Direita
  231.         RotacaoSimplesEsquerda(&((*ppRaiz)->pEsq));
  232.         RotacaoSimplesDireita(ppRaiz); // &(*ppRaiz)
  233.         return 1;
  234.     }
  235.     return 0;
  236. }
  237.  
  238. int BalancaDireita(struct No **ppRaiz)
  239. {
  240.     int fbd = FB((*ppRaiz)->pDir);
  241.     if (fbd < 0)
  242.     {
  243.         RotacaoSimplesEsquerda(ppRaiz);
  244.         return 1;
  245.     }
  246.     else if (fbd > 0 )
  247.     { // Rotação Dupla Esquerda
  248.         RotacaoSimplesDireita(&((*ppRaiz)->pDir));
  249.         RotacaoSimplesEsquerda(ppRaiz); // &(*ppRaiz)
  250.         return 1;
  251.     }
  252.     return 0;
  253. }
  254.  
  255. int Balanceamento(struct No **ppRaiz)
  256. {
  257.     int fb = FB(*ppRaiz);
  258.     if ( fb > 1)
  259.         return BalancaEsquerda(ppRaiz);
  260.     else if (fb < -1 )
  261.         return BalancaDireita(ppRaiz);
  262.     else
  263.         return 0;
  264. }
  265.  
  266. int Inserir(struct No **ppRaiz, Registro x)
  267. {
  268.     if (*ppRaiz == NULL)
  269.     {
  270.         *ppRaiz = (struct No*)malloc(sizeof(struct No));
  271.         (*ppRaiz)->Reg = x;
  272.         (*ppRaiz)->pEsq = NULL;
  273.         (*ppRaiz)->pDir = NULL;
  274.         return 1;
  275.     }
  276.     if ((*ppRaiz)->Reg.Chave > x.Chave)
  277.     {
  278.         if (Inserir(&(*ppRaiz)->pEsq,x))
  279.         {
  280.             if (Balanceamento(ppRaiz))
  281.                 return 0;
  282.             else
  283.                 return 1;
  284.         }
  285.     }
  286.     if ((*ppRaiz)->Reg.Chave < x.Chave)
  287.     {
  288.         if (Inserir(&(*ppRaiz)->pDir,x))
  289.         {
  290.             if (Balanceamento(ppRaiz))
  291.                 return 0;
  292.             else
  293.                 return 1;
  294.         }
  295.         else
  296.             return 0;
  297.     }
  298.     else
  299.         return 0; // valor ja presente
  300. }
  301.  
  302. int EhArvoreAVL(struct No *pRaiz)
  303. {
  304.     int fb;
  305.     if (pRaiz == NULL)
  306.         return 1;
  307.     if (!EhArvoreAVL(pRaiz->pEsq))
  308.         return 0;
  309.     if (!EhArvoreAVL(pRaiz->pDir))
  310.         return 0;
  311.     fb = FB (pRaiz);
  312.     if ((fb > 1) || (fb < -1))
  313.         return 0;
  314.     else
  315.         return 1;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement