Don't like ads? PRO users don't see any ads ;-)
Guest

VANIA

By: aciidow on Jun 2nd, 2012  |  syntax: C  |  size: 12.18 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Marcela Fernández Salas
  2. // 17.857.575-9
  3.  
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7.  
  8. typedef struct nodoAVL {//Estructura del arbol
  9.  
  10.     int licor ,flag;
  11.     int altura ;
  12.     struct nodoAVL *izq,*der;
  13.  
  14. }AVL;
  15.  
  16. void rotacion_simple_izq(AVL**raiz){//Rotacion simple izquierda
  17.         AVL *aux;
  18.         AVL *aux2;
  19.  
  20.             aux= (*raiz)->izq->der;
  21.             (*raiz)->izq->der = *raiz;
  22.             (*raiz)->izq->altura = 0;
  23.             aux2 = (*raiz)->izq;
  24.             (*raiz)->izq = aux;
  25.             (*raiz)->altura = 0;
  26.             *raiz = aux2;
  27.  
  28. }
  29.  
  30. void rotacion_simple_der(AVL**raiz){//Rotacion simple derecha
  31.         AVL* aux=NULL, *aux2=NULL;
  32.  
  33.             aux= (*raiz)->der->izq;
  34.             (*raiz)->der->izq = *raiz;
  35.             (*raiz)->der->altura = 0;
  36.             aux2 = (*raiz)->der;
  37.             (*raiz)->der = aux;
  38.             (*raiz)->altura = 0;
  39.             *raiz = aux2;
  40.  
  41. }
  42.  
  43. void rotacion_doble_izq(AVL**raiz){//Rotacion doble izquierda
  44.         rotacion_simple_der(&((*raiz)->izq));
  45.         rotacion_simple_izq(&*raiz);
  46. }
  47.  
  48. void rotacion_doble_der(AVL**raiz){//Rotacion doble derecha
  49.         rotacion_simple_izq(&((*raiz)->der));
  50.         rotacion_simple_der(&*raiz);
  51. }
  52.  
  53. AVL* nuevonodo(int dato){//Crear nuevo nodo para agregar a los arboles
  54.     AVL *nodo;
  55.             nodo=(AVL*)malloc(sizeof(AVL));
  56.             (nodo)->licor=dato;
  57.             (nodo)->altura=0;
  58.             (nodo)->flag=1;
  59.             (nodo)->izq=NULL;
  60.             (nodo)->der=NULL;
  61.     return(nodo);
  62. }
  63.  
  64. int agregar_arbol(int dato,AVL **raiz){//Para insertar los nodos a los arboles.
  65. AVL *hoja,*nodo;
  66. int flag, cont;
  67.     hoja=nuevonodo(dato);
  68.     if(!(*raiz)){*raiz=hoja;return cont=0;}
  69.         else{
  70.             if (dato < (*raiz)->licor){
  71.                     agregar_arbol(dato,&((*raiz)->izq));
  72.                     if ((*raiz)->flag==1){
  73.                             switch ((*raiz)->altura){
  74.                                     case -1:{
  75.                                             (*raiz)->altura = 0;
  76.                                             flag = 0;
  77.                                             break;
  78.                                     }
  79.                                     case 0:{
  80.                                             (*raiz)->altura = 1;
  81.                                             flag = 1;
  82.                                             break;
  83.                                     }
  84.                                     case 1:{
  85.                                             if (((*raiz)->izq)->altura == 1){
  86.                                                     rotacion_simple_izq(&(*raiz));
  87.                                                     cont++; //Luego de cada ingreso a alguna rotacion, el contador aumenta, para saber cuantas rotaciones realizó el arbol para estar balanceado.
  88.  
  89.                                             }else{
  90.                                                     rotacion_doble_izq(&(*raiz));
  91.                                                     cont++;
  92.                                             }
  93.                                             (*raiz)->flag = 0;
  94.                                             break;
  95.                                     }
  96.                             }
  97.                     }
  98.             }else{
  99.                     agregar_arbol(dato,&((*raiz)->der));
  100.                     if ((*raiz)->flag==1){
  101.                             switch ((*raiz)->altura){
  102.                                     case -1:{
  103.                                             if (((*raiz)->der)->altura == 1){
  104.                                                     rotacion_doble_der(&(*raiz));
  105.                                                     cont++;
  106.                                             }else{
  107.                                                     rotacion_simple_der(&(*raiz));
  108.                                                     cont++;
  109.                                             }
  110.                                             (*raiz)->flag = 0;
  111.                                             break;
  112.                                     }
  113.                                     case 0:{
  114.                                             (*raiz)->altura = -1;
  115.                                             (*raiz)->flag = 1;
  116.                                             break;
  117.                                     }
  118.                                     case 1:{
  119.                                             (*raiz)->altura = 0;
  120.                                             (*raiz)->flag = 0;
  121.                                             break;
  122.                                     }
  123.                             }
  124.                     }
  125.             }
  126.     }
  127.  
  128. return(cont);
  129. }
  130.  
  131. void mostrar_datos(AVL *raiz){//Mostrar los datos de los arboles
  132.     if(raiz){
  133.         printf("%d ",(raiz)->licor);
  134.         mostrar_datos((raiz)->izq);
  135.         mostrar_datos((raiz)->der);
  136.     }
  137. }
  138.  
  139. void abrir_archivos(AVL **arbol_1, AVL **arbol_2, AVL **arbol_3, int *cont1, int *cont2, int *cont3){//Abrir los archivos y extraer los datos de las plantillas
  140. FILE *puntero_bodega;
  141. char bodega_[]="bodega_1.txt";
  142. int dato,i,arb;
  143. cont1=0;cont2=0;cont3=0;
  144.    for (i=0; i<9; i++){
  145.             puntero_bodega=fopen(bodega_, "r+");
  146.                         if(puntero_bodega){
  147.                                 arb=0;
  148.                                 dato=0;
  149.                                         while(!feof(puntero_bodega)){
  150.                                             fscanf(puntero_bodega, "%d:%d:", &arb,&dato);
  151.                                                     switch(arb){
  152.                                                             case 1:
  153.                                                                     cont1=agregar_arbol(dato,&(*arbol_1));
  154.                                                             break;
  155.                                                             case 2:
  156.                                                                     cont2=agregar_arbol(dato,&(*arbol_2));
  157.                                                             break;
  158.                                                             case 3:
  159.                                                                     cont3=agregar_arbol(dato,&(*arbol_3));
  160.                                                             break;
  161.                                                             default:
  162.                                                             break;
  163.                                                     }
  164.                                         }
  165.                         }else{
  166.                                 printf("Archivo %d, no tiene contenido");
  167.  
  168.                         }
  169.     fclose(puntero_bodega);
  170.     bodega_[7]++;
  171.  
  172.             if(bodega_[7]==':')strcpy(bodega_,"bodega_10.txt");
  173.  
  174.  
  175.  
  176.     }
  177.  
  178.  
  179. }
  180.  
  181. AVL *buscar_dato(AVL **raiz, int dato){ // Buscar el numero que está en X arbol para luego ser eliminado.
  182.     AVL*aux=NULL;
  183.     aux=*raiz;
  184.     while(*raiz && (*raiz)->licor!=dato){
  185.             if(dato>(*raiz)->licor){
  186.                     *raiz=(*raiz)->der;
  187.                             }else *raiz=(*raiz)->izq;
  188.             }
  189.  
  190.     return(*raiz);
  191.  
  192.  
  193.     }
  194.  
  195. void traspasar_producto(){ // Traspasar numeros de un arbol a otro.
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. }
  203.  
  204. void menu(){//Menú
  205.     int flag=0, op, dato,num,cont1=0,cont2=0,cont3=0;
  206.     AVL *arbol_1=NULL,*arbol_2=NULL,*arbol_3=NULL;
  207.     abrir_archivos(&arbol_1, &arbol_2, &arbol_3,&cont1,&cont2,&cont3);
  208.  
  209.  
  210.  
  211.  
  212.  
  213. while(flag != 1){
  214.  
  215.  
  216.  
  217.            system("cls");
  218.            printf("\t\t\t********************");
  219.            printf("\n\t\t\t*                  *");
  220.            printf("\n\t\t\t*    BIENVENIDOS   *");
  221.            printf("\n\t\t\t*                  *");
  222.            printf("\n\t\t\t********************");
  223.            printf("\n\n\n\n\n\n");
  224.  
  225.  
  226.  
  227.            printf("|||||||||| ~ M E N U ~ ||||||||||"); //MENU DE LAS OPCIONES
  228.            printf("\n");
  229.            printf("\n Ingrese operacion a realizar: ")  ;
  230.            printf("\n 1. Mostrar el estado actual de las bodegas => \n 2. Mostrar la cantidad de rotaciones realizadas en cada bodega. => \n 3. Retirar productos de alguna bodega especifica. => \n 4. Traspasar producto de una bodega a otra. => \n 5. Salir => \n ");
  231.            printf("\nOpcion => ");
  232.            scanf("%d", &op );
  233.  
  234.             switch(op){
  235.                         case 1://listo
  236.                                 mostrar_datos(arbol_1);
  237.                                 printf("\n");
  238.                                 mostrar_datos(arbol_2);
  239.                                 printf("\n");
  240.                                 mostrar_datos(arbol_3);
  241.                                 printf("\n");
  242.                                 system("pause");
  243.                         break;
  244.                          case 2://listo
  245.                                 printf("Rotaciones del primer arbol: %d\n",cont1);
  246.                                 printf("Rotaciones del segundo arbol: %d\n",cont2);
  247.                                 printf("Rotaciones del tercer arbol: %d\n",cont3);
  248.                                 system("pause");
  249.                             break;
  250.                      case 3:
  251.                             printf("Ingrese el dato a eliminar => \n");
  252.                             scanf("%d", &dato);
  253.                             printf("Ingrese en la bodega => \n");
  254.                             scanf("%d", &num);
  255.                                 switch(num){
  256.                                     case 1:
  257.  
  258.                                             buscar_dato(&arbol_1, dato);
  259.                                             retirar_producto(&arbol_1,dato);
  260.                                     break;
  261.  
  262.                                     case 2:
  263.  
  264.                                             buscar_dato(&arbol_2, dato);
  265.                                             retirar_producto(&arbol_2, dato);
  266.                                     break;
  267.  
  268.                                     case 3:
  269.  
  270.                                             buscar_dato(&arbol_3, dato);
  271.                                             retirar_producto(&arbol_3, dato);
  272.                                     break;
  273.                                 }
  274.                     case 4:
  275.                                 traspasar_producto();
  276.                         break;
  277.                     case 5:
  278.                         system("cls");
  279.                         printf("\n");
  280.                         printf("| ~ Hasta Luego ~ |");
  281.                         printf("\n");
  282.                         exit (0);
  283.                         break;
  284.                     }
  285.  
  286.             }
  287.     }
  288.  
  289. int retirar_producto(AVL **raiz,int dato){ // Funcion para eliminar un dato de un arbol.
  290.     AVL *nodo=NULL,*aux=NULL, *aux_2=NULL;
  291.  
  292.     nodo=buscar_dato(raiz, dato);
  293.  
  294.     if(nodo->izq && nodo->der){
  295.             aux=nodo->der;
  296.             aux_2=nodo;
  297.                 while(aux->izq){
  298.                     aux=aux_2;
  299.                     aux=aux->izq;
  300.                 }
  301.                 nodo->licor=aux->licor;
  302.                 nodo=aux_2;
  303.     }
  304.     if(!nodo->der && !nodo->izq){
  305.             if(*raiz==nodo)*raiz=NULL;
  306.                     else{
  307.                             if(aux->der==nodo)aux->der=NULL;
  308.                                 else aux->izq=NULL;
  309.                     }
  310.     }
  311.     if((nodo->izq && nodo->der==NULL)||(nodo->der && nodo->izq==NULL)){
  312.             if(nodo->izq && nodo->der==NULL){
  313.                     if(*raiz==nodo)*raiz=nodo->izq;
  314.                             else{
  315.                                     if(aux->izq==nodo)aux->izq=nodo->izq;
  316.                                             else aux->der=nodo->izq;
  317.                             }
  318.             }
  319.             else{
  320.                     if(nodo->der && nodo->izq==NULL){
  321.                             if(*raiz==nodo)*raiz=nodo->der;
  322.                                         else{
  323.                                                 if(aux->izq==nodo)aux->izq=nodo->der;
  324.                                                         else aux->der=nodo->der;
  325.                                         }
  326.                     }
  327.             }
  328.     }
  329.     nodo->izq=NULL;
  330.     nodo->der=NULL;
  331.     free(nodo);
  332.  
  333.  
  334. }
  335.  
  336. main(){//Main
  337.     menu();
  338.  
  339. }