gonzalob

Untitled

Dec 2nd, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. typedef struct
  7. {
  8.     int idAlumno;
  9.     char alumno[20];
  10.     struct nodoArbol1 *izq;
  11.     struct nodoArbol1 *der;
  12.     struct nodoArbol2 *abdab;
  13. } nodoArbol1;
  14.  
  15. typedef struct
  16. {
  17.     char asignatura[20];
  18.     int nota;
  19.     char estadoCursada[15]; ///activo - inactivo
  20. } stMateriaYNota;
  21.  
  22. typedef struct
  23. {
  24.     stMateriaYNota MyN;
  25.     struct nodoArbol2 *izq;
  26.     struct nodoArbol2 *der;
  27. } nodoArbol2;
  28.  
  29. nodoArbol1 *inicArbol1();
  30. nodoArbol2 *inicArbol2();
  31. nodoArbol1 *crearNodoArbol1(int idAlumno, char alumno[]);
  32. nodoArbol2 *crearNodoArbol2(stMateriaYNota MyN);
  33. stMateriaYNota auxCarga(char asignatura[], int nota, char estadoCursada[]);
  34. nodoArbol1 *agregarAlArbol1(nodoArbol1 *lito, nodoArbol1 *nn);
  35. nodoArbol2 *agregarAlArbol2(nodoArbol2 *lito2, nodoArbol2 *nn);
  36. nodoArbol1 *altaEnAbdAb(int idAlumno, char alumno[],stMateriaYNota MyN, nodoArbol1 *lito);
  37. void recorrerYMostrarAbl(nodoArbol1 *lito);
  38. void mostrarArbol1(nodoArbol1 *lito);
  39. void mostrarArbol2(nodoArbol2 *lito2);
  40.  
  41. int main()
  42. {
  43.     nodoArbol1 *lito = inicArbol1();
  44.  
  45.     stMateriaYNota aux;
  46.  
  47.     aux = auxCarga("Matematica", 8, "Activo");
  48.     lito = altaEnAbdAb(3, "Jose", aux, lito);
  49. /*
  50.     aux = auxCarga("Contabilidad", 2, "inactivo");
  51.     lito = altaEnAbdAb(3, "Jose", aux, lito);
  52.  
  53.  
  54.     aux = auxCarga("Programacion", 9, "activo");
  55.     lito = altaEnAbdAb(1, "Ale", aux, lito);
  56.  
  57.  
  58.     aux = auxCarga("Metodologia", 4, "inactivo");
  59.     lito = altaEnAbdAb(2, "Miguel", aux, lito);
  60.  
  61.  
  62.     aux = auxCarga("Programacion", 9, "activo");
  63.     lito = altaEnAbdAb(2, "Miguel", aux, lito);
  64. */
  65.     recorrerYMostrarAbl(lito);
  66.  
  67.     return 0;
  68. }
  69.  
  70. nodoArbol1 *inicArbol1()
  71. {
  72.     return NULL;
  73. }
  74.  
  75. nodoArbol2 *inicArbol2()
  76. {
  77.     return NULL;
  78. }
  79.  
  80. nodoArbol1 *crearNodoArbol1(int idAlumno, char alumno[])
  81. {
  82.     nodoArbol1 *aux = (nodoArbol1*) malloc(sizeof(nodoArbol1));
  83.     aux->idAlumno = idAlumno;
  84.     strcpy(aux->alumno, alumno);
  85.     aux->izq = NULL;
  86.     aux->der = NULL;
  87.     aux->abdab = inicArbol2();
  88.  
  89.     return aux;
  90. }
  91.  
  92. nodoArbol2 *crearNodoArbol2(stMateriaYNota MyN)
  93. {
  94.     nodoArbol2 *aux = (nodoArbol2*) malloc (sizeof(nodoArbol2));
  95.     aux->MyN = MyN;
  96.     aux->izq = NULL;
  97.     aux->der = NULL;
  98.  
  99.     return aux;
  100. }
  101.  
  102. stMateriaYNota auxCarga(char asignatura[], int nota, char estadoCursada[])
  103. {
  104.     stMateriaYNota aux;
  105.  
  106.     strcpy(aux.asignatura, asignatura);
  107.     aux.nota = nota;
  108.     strcpy(aux.estadoCursada, estadoCursada);
  109.  
  110.     return aux;
  111. }
  112.  
  113. nodoArbol1 *agregarAlArbol1(nodoArbol1 *lito, nodoArbol1 *nn)
  114. {
  115.     if(lito == NULL)
  116.     {
  117.         lito = nn;
  118.     }
  119.     else if (nn->idAlumno < lito->idAlumno)
  120.     {
  121.         lito->izq = agregarAlArbol1(lito->izq, nn);
  122.     }
  123.     else
  124.     {
  125.         lito->der = agregarAlArbol1(lito->der, nn);
  126.     }
  127.     return lito;
  128. }
  129.  
  130. nodoArbol2 *agregarAlArbol2(nodoArbol2 *lito2, nodoArbol2 *nn)
  131. {
  132.     if(lito2 == NULL)
  133.     {
  134.         lito2 = nn;
  135.     }
  136.     else if(nn->MyN.nota < lito2->MyN.nota)
  137.     {
  138.         lito2->izq = agregarAlArbol2(lito2->izq, nn);
  139.     }
  140.     else
  141.     {
  142.         lito2->der = agregarAlArbol2(lito2->der, nn);
  143.     }
  144.     return lito2;
  145. }
  146.  
  147. nodoArbol1 *altaEnAbdAb(int idAlumno, char alumno[],stMateriaYNota MyN, nodoArbol1 *lito)
  148. {
  149.         nodoArbol2 *nn2 =crearNodoArbol2(MyN);
  150.  
  151.     if(lito == NULL) ///si el arbol es null ingreso el alumno, y al mismo le agrego sus respectivos datos
  152.     {
  153.         nodoArbol1 *nn = crearNodoArbol1(idAlumno, alumno);
  154.         lito = agregarAlArbol1(lito, nn);
  155.         nodoArbol2 *lito2 = lito->abdab;
  156.         lito->abdab = agregarAlArbol2(lito2, nn2);
  157.     }
  158.     else
  159.     {
  160.         if(lito->idAlumno == idAlumno) /// si el arbol no es NULL recorro recursivamente, y busco si ya se encuentra el id, y agrego los datos
  161.         {
  162.             nodoArbol2 *lito2 = lito->abdab;
  163.             lito->abdab = agregarAlArbol2(lito2, nn2);
  164.         }
  165.         else if (lito->idAlumno < idAlumno)
  166.         {
  167.             lito->izq = altaEnAbdAb(idAlumno, alumno, MyN, lito->izq);
  168.         }
  169.         else if((lito->idAlumno > idAlumno))
  170.         {
  171.             lito->der = altaEnAbdAb(idAlumno, alumno, MyN, lito->der);
  172.         }
  173.         else ///el arbol no es NULLO, y no se encuentra el dato, entonces lo creo y agrego
  174.         {
  175.             nodoArbol1 *nn = crearNodoArbol1(idAlumno, alumno);
  176.             lito = agregarAlArbol1(lito, nn);
  177.         }
  178.     }
  179.     return lito;
  180. }
  181.  
  182. void recorrerYMostrarAbl(nodoArbol1 *lito)
  183. {
  184.     if(lito != NULL)
  185.     {
  186.         mostrarArbol1(lito);
  187.         //aca habia un error de tipo
  188.         nodoArbol2 *lito2 = lito->abdab;
  189.         if(lito2 != NULL)
  190.         {
  191.             mostrarArbol2(lito2); //modularizo completamente como mostrar otro arbol
  192.         }
  193.         recorrerYMostrarAbl(lito->izq);
  194.         recorrerYMostrarAbl(lito->der);
  195.     }
  196. }
  197.  
  198.  
  199. void mostrarArbol1(nodoArbol1 *lito)
  200. {
  201.     printf("\nidAlumno......: [%d]", lito->idAlumno);
  202.     printf("\nAlumno........: [%s]", lito->alumno);
  203.     printf("\n     --");
  204. }
  205.  
  206. void mostrarArbol2(nodoArbol2 *lito2)
  207. {
  208.     if (lito2!=NULL)
  209.     {
  210.         //muestro los datos de ese nodo
  211.         printf("\nAsignatura.....:[%s]", lito2->MyN.asignatura);
  212.         printf("\nNota...........:[%d]", lito2->MyN.nota);
  213.         printf("\nEstadoCurs.....:[%s]", lito2->MyN.estadoCursada);
  214.         printf("\n-------");
  215.         //llamado recursivc para moverme dentro del arbol
  216.         mostrarArbol2(lito2->der);
  217.         mostrarArbol2(lito2->izq);
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment