• Miércoles 15 de Mayo de 2024, 06:20

Autor Tema:  Promedio De Los Nodos Terminales  (Leído 1031 veces)

AnioN

  • Miembro MUY activo
  • ***
  • Mensajes: 339
    • Ver Perfil
Promedio De Los Nodos Terminales
« en: Viernes 22 de Septiembre de 2006, 14:22 »
0
Hola, necesito crear una funcion para calcular el promedio de los nodos hojas. Cree la funcion promedio para hacerlo, la cual no esta bien hecha. El resto del codigo funciona bien. Alguien sabe cual es el error?

Código: Text
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. struct dato {
  6.   int entero;
  7. };
  8.  
  9. struct nodo {
  10.   struct dato d;
  11.   struct nodo *izq, *der;
  12. };
  13.  
  14. int arbol_vacio(struct nodo *q);
  15. struct nodo* arbol_lleno();
  16. struct nodo* insertar(struct nodo *q, struct dato x);
  17. void listar_preorden(struct nodo *q);
  18. float promedio(struct nodo *q, int*, int*);
  19.  
  20. void main() {
  21.   struct nodo *raiz = NULL;
  22.   struct dato x;
  23.   int c = 0, s = 0;
  24.  
  25.   printf("Ingrese un entero:");
  26.   scanf("%d", &x.entero);
  27.  
  28.   while(x.entero != 0) {
  29.  
  30.     raiz = insertar(raiz, x);
  31.  
  32.     printf("Ingrese un entero:");
  33.     scanf("%d", &x.entero);
  34.   }
  35.  
  36.   listar_preorden(raiz);
  37.  
  38.   printf("\nEl promedio de los nodos hojas es:%.2f.", promedio(raiz,&c,&s));
  39.  
  40.   printf("\nPresione una tecla para salir");
  41.   getch();
  42. }
  43.  
  44. int arbol_vacio(struct nodo *q) {
  45.  
  46.   if(!q) {
  47.     return 1;
  48.   } else {
  49.     return 0;
  50.   }
  51. }
  52.  
  53.  
  54. struct nodo* arbol_lleno() {
  55.   struct nodo *aux;
  56.  
  57.   aux = (struct nodo*)malloc(sizeof(struct nodo)*1);
  58.  
  59.   return aux;
  60. }
  61.  
  62.  
  63. void listar_preorden(struct nodo *q) {
  64.  
  65.   if(q) {
  66.     printf("\n%d", q->d.entero);
  67.     listar_preorden(q->izq);
  68.     listar_preorden(q->der);
  69.   }
  70. }
  71.  
  72.  
  73. struct nodo* insertar(struct nodo *q, struct dato x) {
  74.   struct nodo *nuevo, *ant = q, *act = q;
  75.  
  76.   if((nuevo=arbol_lleno())==NULL) {
  77.     printf("El arbol esta lleno");
  78.   } else {
  79.     nuevo->d = x;
  80.     nuevo->izq = nuevo->der = NULL;
  81.  
  82.     if(arbol_vacio(q)) {
  83.       q = nuevo;
  84.     } else {
  85.  
  86.       while(act!=NULL) {
  87.         ant = act;
  88.  
  89.         if(nuevo->d.entero > act->d.entero) {
  90.           act = act->der;
  91.         } else {
  92.           act = act->izq;
  93.         }
  94.       }
  95.  
  96.       if(nuevo->d.entero > ant->d.entero) {
  97.         ant->der = nuevo;
  98.       } else {
  99.  
  100.         if(nuevo->d.entero < ant->d.entero) {
  101.           ant->izq = nuevo;
  102.         } else {
  103.           printf("Clave duplicada");
  104.         }
  105.       }
  106.     }
  107.   }
  108.   return q;
  109. }
  110.  
  111.  
  112. float promedio(struct nodo *q, int *c, int *s) {
  113.  
  114.   if(q) {
  115.       if(q->izq==NULL && q->der==NULL) {
  116.       (*c)++;
  117.       (*s) += q->d.entero;
  118.     } else {
  119.       promedio(q->izq,&c,&s);
  120.       promedio(q->der,&c,&s);
  121.     }
  122.   }
  123.   return (float)(*s/(*c));
  124. }
  125.