sábado, 19 de febrero de 2011

Laboratorio08 - Arboles


Ola este es el Laboratorio 08 del curso de estructuras de datos que trata sobre Arboles .. espero les ayude.. Saludos...


/* Arboles */

#include<iostream>
#include<conio.h>
#include<iomanip>

using namespace std;

typedef struct nodo /// Estructura Nodo
{  int dato;
   struct nodo *izq;
   struct nodo *der;
}TNodo;


typedef struct Arbol /// Estructura Arbol
{  struct nodo *Raiz;
}TArbol;


/*  CREAR NODO  */
TNodo *crearNodo(int x)
{  TNodo *nodo=(TNodo*)malloc(sizeof(TNodo));
   nodo->dato=x;
   nodo->izq=NULL;
   nodo->der=NULL;
   return nodo;
}

/*  CREAR ARBOL  */
TArbol *crearArbol()
{  TArbol *Arbol=(TArbol*)malloc(sizeof(TArbol));
   Arbol->Raiz=NULL;
   return Arbol;
}


/*  FUNCION INSERTAR  */
void insertar(TArbol *Arbol, int x)
{  TNodo *Nodo=crearNodo(x);
   char lado;
   TNodo *p=NULL;
   TNodo *a=NULL;
   if(Arbol->Raiz==NULL)
      Arbol->Raiz=Nodo;
   else
   {  p = Arbol->Raiz;
      bool band = true;
      while ( p != NULL)
      {   a = p;
          if( x > a->dato )
          {   p = a -> der;
              lado = 'D';
              continue;
          }
          if ( x < a->dato )
          {   p = a -> izq;
              lado = 'I';
              continue;
          }  
          band = false;
          break;
      }
      if( band == true )
      {   if( lado == 'D')
          a->der = Nodo;
          if( lado == 'I')
             a->izq = Nodo;
      }
      else
          cout<<"\n\t\t\t\t\t El Elemento Ya Existe!!! \n";
   }
}


/*  FUNCION PRE ORDEN  */
void PreOrden(TNodo *p)
{
   if(p!=NULL)
   {
      cout<<setw(5)<<p->dato;
 PreOrden(p->izq);
 PreOrden(p->der);
   }
}


/*  FUNCION IN ORDEN  */
void InOrden(TNodo *p)
{  if(p!=NULL)
   {   InOrden(p->izq);
  cout<<setw(5)<<p->dato;
  InOrden(p->der);
   }
}


/*  FUNCION POST ORDEN  */
void PostOrden(TNodo *p)
{

 if(p!=NULL)
   {
 
PostOrden(p->izq);
PostOrden(p->der);
cout<<setw(5)<<p->dato;
     
   }
}


/*  BUSCAR  */
bool buscar(TNodo *p , int num)
{  if (p!=NULL)
   {
       if(p->dato==num)
       {   cout<<"\n"<<setw(10)
           <<"\t\t     Dato Encontrado = "<<p->dato<<endl;
           return(1);
       }
       else
       {   if(num < p->dato)
               buscar(p->izq,num);
           else
               buscar(p->der,num);
       }
   }
   else
       return(0);
}


/*  FUNCION PRESENTACION  */
void presentacion()
{  cout<<"\n\n\n\n\n\n";
   cout<<endl<<"\t      ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»  ";
   cout<<endl<<"\t      º     |<-[ UNIVERSIDAD NACIONAL DE TRUJILLO ]->|     º  ";
   cout<<endl<<"\t      º          CURSO ::: ESTRUCTURA DE DATOS             º  ";
   cout<<endl<<"\t      º             Laboratio 08 - ARBOLES                 º  ";
   cout<<endl<<"\t      º       Hecho por InfoBik's ---> JP. Rodriguez       º  ";
   cout<<endl<<"\t      ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  \n"<<endl;
   cout<<endl<<"\t\t >> Presione cualqier tecla para continuar... ";
   getch();
   system("CLS");
}


/*  FUNCION MENU  */
void menu()
{
   cout<<"\n\n";  
   cout<<endl<<"\t\t                  MENU PRINCIPAL ";
   cout<<endl<<"\t\t                ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ";
 
   cout<<endl<<"\t\t DISENIE E IMPLEMENTE LOS SIGUIENTES ALGORITMOS: ";
   cout<<endl<<"\t\tÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ \n\n";

   cout<<"\t\t\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»  "<<endl;
   cout<<"\t\t\tº   1. Insertar Dato en el Arbol  º  "<<endl;
   cout<<"\t\t\tº   2. Recorrido en PreOrden      º  "<<endl;
   cout<<"\t\t\tº   3. Recorrido en InOrden       º  "<<endl;
   cout<<"\t\t\tº   4. Recorrido en PostOrden     º  "<<endl;
   cout<<"\t\t\tº   5. Buscar Dato en el Arbol    º  "<<endl;
   cout<<"\t\t\tºÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͺ  "<<endl;
   cout<<"\t\t\tº   6.  Salir                     º  "<<endl;
   cout<<"\t\t\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ  "<<endl<<endl;
}


/*  MENU PRINCIPAL  */
int main()
{  system("color 1E");
   presentacion();
   TArbol *A = crearArbol();
   TNodo *p = NULL;
   while(1)
   {   menu();
       int opcion;
       do
       {  cout<<"\t   >> Elija una opcion: ";
          cin>>opcion;
       }
       while(opcion < -10000 || opcion > 10000);
       
          switch (opcion)
          {
              case 1: int N;
 int Nro;
 cout<<endl<<"\t\t Cantidad de Datos : ";
 cin>>N;
                      cout<<"\n";
 for( int i = 0 ; i< N ; i++ )
         {  
                         cout<<"\t\t\t\tPosicion["<<i+1<<"] : ";
cin>>Nro;
insertar(A,Nro);
         }
         cout<<"\n\t\t      Los Datos han sido almacenados...\n\n";
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
                      break;

              case 2: cout<<"\n\t\t Recorrido en PreOrden :"<<endl<<endl<<"\t";      
                      p = A->Raiz;
                      cout<<"\t\t";
 PreOrden(p);
 cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
 break;

case 3:  cout<<"\n\t\t Recorrido en InOrden :"<<endl<<endl<<"\t";
                      p = A->Raiz;
                      cout<<"\t\t";
 InOrden(p);
 cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
 break;

case 4:  cout<<"\n\t\t Recorrido en PostOrden "<<endl<<endl<<"\t";
                      p = A->Raiz;
                      cout<<"\t\t";
 PostOrden(p);
 cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
 break;

case 5:  int num;
                      bool valor;
                      cout<<"\n\t\t  Ingrese Numero A Buscar : ";
 cin>>num;
 p = A->Raiz;
     valor = buscar(p,num);
                      if(valor == 0)
                          cout<<"\n\t\t     No se Encontro el Dato!!!";
                      cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
     break;
                     
case 6:  system("cls");
         cout<<"\n\n\n\n\n\n\n\n\n\n\n";
         cout<<"\t\t      Visita www.infobiks.blogspot.com ";
         getch();
                      exit(1);    
 break;

default: cout<<endl;
                      cout<<"\t\t     La Opcion No es Valida !!!!!!!!!";
                      cout<<endl<<endl;
                      cout<<"\t   Presione una tecla para continuar... ";
                      getch();
                      system("cls");
                      break;

        }

    }

 system("PAUSE");

}

No hay comentarios.:

Publicar un comentario