//Marcela Fernández Salas
// 17.857.575-9
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct nodoAVL {//Estructura del arbol
int licor ,flag;
int altura ;
struct nodoAVL *izq,*der;
}AVL;
void rotacion_simple_izq(AVL**raiz){//Rotacion simple izquierda
AVL *aux;
AVL *aux2;
aux= (*raiz)->izq->der;
(*raiz)->izq->der = *raiz;
(*raiz)->izq->altura = 0;
aux2 = (*raiz)->izq;
(*raiz)->izq = aux;
(*raiz)->altura = 0;
*raiz = aux2;
}
void rotacion_simple_der(AVL**raiz){//Rotacion simple derecha
AVL* aux=NULL, *aux2=NULL;
aux= (*raiz)->der->izq;
(*raiz)->der->izq = *raiz;
(*raiz)->der->altura = 0;
aux2 = (*raiz)->der;
(*raiz)->der = aux;
(*raiz)->altura = 0;
*raiz = aux2;
}
void rotacion_doble_izq(AVL**raiz){//Rotacion doble izquierda
rotacion_simple_der(&((*raiz)->izq));
rotacion_simple_izq(&*raiz);
}
void rotacion_doble_der(AVL**raiz){//Rotacion doble derecha
rotacion_simple_izq(&((*raiz)->der));
rotacion_simple_der(&*raiz);
}
AVL* nuevonodo(int dato){//Crear nuevo nodo para agregar a los arboles
AVL *nodo;
nodo=(AVL*)malloc(sizeof(AVL));
(nodo)->licor=dato;
(nodo)->altura=0;
(nodo)->flag=1;
(nodo)->izq=NULL;
(nodo)->der=NULL;
return(nodo);
}
int agregar_arbol(int dato,AVL **raiz){//Para insertar los nodos a los arboles.
AVL *hoja,*nodo;
int flag, cont;
hoja=nuevonodo(dato);
if(!(*raiz)){*raiz=hoja;return cont=0;}
else{
if (dato < (*raiz)->licor){
agregar_arbol(dato,&((*raiz)->izq));
if ((*raiz)->flag==1){
switch ((*raiz)->altura){
case -1:{
(*raiz)->altura = 0;
flag = 0;
break;
}
case 0:{
(*raiz)->altura = 1;
flag = 1;
break;
}
case 1:{
if (((*raiz)->izq)->altura == 1){
rotacion_simple_izq(&(*raiz));
cont++; //Luego de cada ingreso a alguna rotacion, el contador aumenta, para saber cuantas rotaciones realizó el arbol para estar balanceado.
}else{
rotacion_doble_izq(&(*raiz));
cont++;
}
(*raiz)->flag = 0;
break;
}
}
}
}else{
agregar_arbol(dato,&((*raiz)->der));
if ((*raiz)->flag==1){
switch ((*raiz)->altura){
case -1:{
if (((*raiz)->der)->altura == 1){
rotacion_doble_der(&(*raiz));
cont++;
}else{
rotacion_simple_der(&(*raiz));
cont++;
}
(*raiz)->flag = 0;
break;
}
case 0:{
(*raiz)->altura = -1;
(*raiz)->flag = 1;
break;
}
case 1:{
(*raiz)->altura = 0;
(*raiz)->flag = 0;
break;
}
}
}
}
}
return(cont);
}
void mostrar_datos(AVL *raiz){//Mostrar los datos de los arboles
if(raiz){
printf("%d ",(raiz)->licor);
mostrar_datos((raiz)->izq);
mostrar_datos((raiz)->der);
}
}
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
FILE *puntero_bodega;
char bodega_[]="bodega_1.txt";
int dato,i,arb;
cont1=0;cont2=0;cont3=0;
for (i=0; i<9; i++){
puntero_bodega=fopen(bodega_, "r+");
if(puntero_bodega){
arb=0;
dato=0;
while(!feof(puntero_bodega)){
fscanf(puntero_bodega, "%d:%d:", &arb,&dato);
switch(arb){
case 1:
cont1=agregar_arbol(dato,&(*arbol_1));
break;
case 2:
cont2=agregar_arbol(dato,&(*arbol_2));
break;
case 3:
cont3=agregar_arbol(dato,&(*arbol_3));
break;
default:
break;
}
}
}else{
printf("Archivo %d, no tiene contenido");
}
fclose(puntero_bodega);
bodega_[7]++;
if(bodega_[7]==':')strcpy(bodega_,"bodega_10.txt");
}
}
AVL *buscar_dato(AVL **raiz, int dato){ // Buscar el numero que está en X arbol para luego ser eliminado.
AVL*aux=NULL;
aux=*raiz;
while(*raiz && (*raiz)->licor!=dato){
if(dato>(*raiz)->licor){
*raiz=(*raiz)->der;
}else *raiz=(*raiz)->izq;
}
return(*raiz);
}
void traspasar_producto(){ // Traspasar numeros de un arbol a otro.
}
void menu(){//Menú
int flag=0, op, dato,num,cont1=0,cont2=0,cont3=0;
AVL *arbol_1=NULL,*arbol_2=NULL,*arbol_3=NULL;
abrir_archivos(&arbol_1, &arbol_2, &arbol_3,&cont1,&cont2,&cont3);
while(flag != 1){
system("cls");
printf("\t\t\t********************");
printf("\n\t\t\t* *");
printf("\n\t\t\t* BIENVENIDOS *");
printf("\n\t\t\t* *");
printf("\n\t\t\t********************");
printf("\n\n\n\n\n\n");
printf("|||||||||| ~ M E N U ~ ||||||||||"); //MENU DE LAS OPCIONES
printf("\n");
printf("\n Ingrese operacion a realizar: ") ;
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 ");
printf("\nOpcion => ");
scanf("%d", &op );
switch(op){
case 1://listo
mostrar_datos(arbol_1);
printf("\n");
mostrar_datos(arbol_2);
printf("\n");
mostrar_datos(arbol_3);
printf("\n");
system("pause");
break;
case 2://listo
printf("Rotaciones del primer arbol: %d\n",cont1);
printf("Rotaciones del segundo arbol: %d\n",cont2);
printf("Rotaciones del tercer arbol: %d\n",cont3);
system("pause");
break;
case 3:
printf("Ingrese el dato a eliminar => \n");
scanf("%d", &dato);
printf("Ingrese en la bodega => \n");
scanf("%d", &num);
switch(num){
case 1:
buscar_dato(&arbol_1, dato);
retirar_producto(&arbol_1,dato);
break;
case 2:
buscar_dato(&arbol_2, dato);
retirar_producto(&arbol_2, dato);
break;
case 3:
buscar_dato(&arbol_3, dato);
retirar_producto(&arbol_3, dato);
break;
}
case 4:
traspasar_producto();
break;
case 5:
system("cls");
printf("\n");
printf("| ~ Hasta Luego ~ |");
printf("\n");
exit (0);
break;
}
}
}
int retirar_producto(AVL **raiz,int dato){ // Funcion para eliminar un dato de un arbol.
AVL *nodo=NULL,*aux=NULL, *aux_2=NULL;
nodo=buscar_dato(raiz, dato);
if(nodo->izq && nodo->der){
aux=nodo->der;
aux_2=nodo;
while(aux->izq){
aux=aux_2;
aux=aux->izq;
}
nodo->licor=aux->licor;
nodo=aux_2;
}
if(!nodo->der && !nodo->izq){
if(*raiz==nodo)*raiz=NULL;
else{
if(aux->der==nodo)aux->der=NULL;
else aux->izq=NULL;
}
}
if((nodo->izq && nodo->der==NULL)||(nodo->der && nodo->izq==NULL)){
if(nodo->izq && nodo->der==NULL){
if(*raiz==nodo)*raiz=nodo->izq;
else{
if(aux->izq==nodo)aux->izq=nodo->izq;
else aux->der=nodo->izq;
}
}
else{
if(nodo->der && nodo->izq==NULL){
if(*raiz==nodo)*raiz=nodo->der;
else{
if(aux->izq==nodo)aux->izq=nodo->der;
else aux->der=nodo->der;
}
}
}
}
nodo->izq=NULL;
nodo->der=NULL;
free(nodo);
}
main(){//Main
menu();
}