Advertisement
KDOXG

Busca em Largura - Arvore Binaria em C

May 29th, 2019
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. int FilaTemElementos(arvore **fila, int tam)
  2. {
  3.     int i, flag=0;
  4.     for (i=0; i<tam; i++)
  5.     {
  6.         if (fila[i] == NULL)
  7.             flag++;
  8.     }
  9.     if (flag == tam-1)
  10.         return 0;
  11.     else
  12.         return 1;
  13. }
  14.  
  15. arvore* GetNodo(arvore *nodo, short flag)
  16. {
  17.     if (!!nodo)
  18.         return NULL;
  19.     if (flag)
  20.         return filaNodos[j]->pDir;
  21.     else
  22.         return filaNodos[j]->pEsq;
  23. }
  24.  
  25. arvore** arvoreSimetrica(arvore *raiz, int quantidadeElementos)
  26. {
  27.     int altura=2, filaTamanho=2, aux, i=0, j=0, k=1;
  28.     arvore **vetor, **filaNodos, **filaNodos_aux;
  29.     filaNodos = malloc(filaTamanho*sizeof(arvore*));
  30.     filaNodos[0] = raiz->pEsq;
  31.     filaNodos[1] = raiz->pDir;
  32.     vetor = malloc(quantidadeElementos*sizeof(arvore*));
  33.     vetor[0] = raiz;
  34.     for (i=0; i<filaTamanho; i++)
  35.     {
  36.         if (filaNodos[i] != NULL)
  37.         {
  38.             vetor[k] = filaNodos_aux[i];
  39.             k++;
  40.         }
  41.     }
  42.     i=0;
  43.  
  44.     while(FilaTemElementos(filaNodos,filaTamanho))
  45.     {
  46.         filaTamanho *= 2;
  47.         filaNodos_aux = malloc(filaTamanho*sizeof(arvore*));
  48.         aux = altura;
  49.         while (aux > 0)
  50.         {
  51.             filaNodos_aux[i] = GetNodo(filaNodos[j],0);
  52.             i++;
  53.             filaNodos_aux[i] = GetNodo(filaNodos[j],1);
  54.             i++;
  55.             j++;
  56.             aux--;
  57.         }
  58.         for (i=0; i<filaTamanho; i++)
  59.         {
  60.             if (filaNodos_aux[i] != NULL)
  61.             {
  62.                 vetor[k] = filaNodos_aux[i];
  63.                 k++;
  64.             }
  65.         }
  66.         free(filaNodos);
  67.         filaNodos = filaNodos_aux;
  68.         altura *= 2;
  69.         i=0;
  70.         j=0;
  71.     }
  72.  
  73.     free(filaNodos);
  74.     return vetor;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement