Advertisement
eduardovp97

ex11.c

Nov 14th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "tree.h"
  4. #include <string.h>
  5. #define N 20
  6. int niveles[N];
  7. void imprimirMayorNivel(TTree *tree);
  8. void recorrer(TNode *node,int n, int max);
  9. void calcularNiveles(TNode *node, int n);
  10. int main(){
  11.  
  12.     TTree arbol;
  13.     crearArbol(&arbol);
  14.     insertar(&arbol,15);
  15.     insertar(&arbol,6);
  16.     insertar(&arbol,3);
  17.     insertar(&arbol,1);
  18.     insertar(&arbol,4);
  19.     insertar(&arbol,9);
  20.     insertar(&arbol,7);
  21.     insertar(&arbol,12);
  22.     insertar(&arbol,20);
  23.     insertar(&arbol,18);
  24.     insertar(&arbol,24);
  25.     insertar(&arbol,17);  
  26.  
  27.     memset(niveles,0,sizeof(niveles));
  28.  
  29.     imprimirMayorNivel(&arbol);
  30.     return 0;
  31. }
  32.  
  33. void imprimirMayorNivel(TTree *tree){
  34.     int i=1,maximo = 0,maximoNivel = 0;
  35.  
  36.     calcularNiveles(tree->root,1);
  37.     /*int j;
  38.     for (int j = 0; j < N; ++j)
  39.     {
  40.         printf("%d\n",niveles[j]);
  41.     }*/
  42.     while(niveles[i] != 0){
  43.         if(niveles[i]>maximo){
  44.             maximo = niveles[i];
  45.             maximoNivel = i;
  46.         }
  47.         i++;
  48.     }
  49.     //printf("aca si%d\n",maximoNivel);
  50.  
  51.     recorrer(tree->root,1,maximoNivel);
  52.  
  53. }
  54.  
  55. void recorrer(TNode *node,int n, int max){
  56.     if(node==NULL)
  57.         return;
  58.     if(n == max){
  59.         printf("%d ",node->info);
  60.         return ;
  61.     }
  62.     //printf("maximo%d\n",max);
  63.     //printf("n%d\n",n);
  64.     recorrer(node->izq,n+1,max);
  65.     recorrer(node->der,n+1,max);
  66. }
  67.  
  68. void calcularNiveles(TNode *node, int n){
  69.     if(node == NULL)
  70.         return;
  71.  
  72.     niveles[n]++;
  73.     calcularNiveles(node->izq,n+1);
  74.     calcularNiveles(node->der,n+1);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement