Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "tree.h"
- #include <string.h>
- #define N 20
- int niveles[N];
- void imprimirMayorNivel(TTree *tree);
- void recorrer(TNode *node,int n, int max);
- void calcularNiveles(TNode *node, int n);
- int main(){
- TTree arbol;
- crearArbol(&arbol);
- insertar(&arbol,15);
- insertar(&arbol,6);
- insertar(&arbol,3);
- insertar(&arbol,1);
- insertar(&arbol,4);
- insertar(&arbol,9);
- insertar(&arbol,7);
- insertar(&arbol,12);
- insertar(&arbol,20);
- insertar(&arbol,18);
- insertar(&arbol,24);
- insertar(&arbol,17);
- memset(niveles,0,sizeof(niveles));
- imprimirMayorNivel(&arbol);
- return 0;
- }
- void imprimirMayorNivel(TTree *tree){
- int i=1,maximo = 0,maximoNivel = 0;
- calcularNiveles(tree->root,1);
- /*int j;
- for (int j = 0; j < N; ++j)
- {
- printf("%d\n",niveles[j]);
- }*/
- while(niveles[i] != 0){
- if(niveles[i]>maximo){
- maximo = niveles[i];
- maximoNivel = i;
- }
- i++;
- }
- //printf("aca si%d\n",maximoNivel);
- recorrer(tree->root,1,maximoNivel);
- }
- void recorrer(TNode *node,int n, int max){
- if(node==NULL)
- return;
- if(n == max){
- printf("%d ",node->info);
- return ;
- }
- //printf("maximo%d\n",max);
- //printf("n%d\n",n);
- recorrer(node->izq,n+1,max);
- recorrer(node->der,n+1,max);
- }
- void calcularNiveles(TNode *node, int n){
- if(node == NULL)
- return;
- niveles[n]++;
- calcularNiveles(node->izq,n+1);
- calcularNiveles(node->der,n+1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement