https://drive.google.com/file/d/0BwltBPRc8Fj4dzdDU1BsbE0tQjg/view?usp=sharing
CÓDIGO:
#include <iostream>
#include <cstdlib>
using namespace std;
void menu();
struct nodo{
int nro;
struct nodo *izq, *der;
};
typedef struct nodo *ABB;
ABB crearNodo(int x)
{
ABB nuevoNodo = new(struct nodo);
nuevoNodo->nro = x;
nuevoNodo->izq = NULL;
nuevoNodo->der = NULL;
return nuevoNodo;
}
void insertar(ABB &arbol, int x)
{
if(arbol==NULL)
{
arbol = crearNodo(x);
}
else if(x < arbol->nro)
insertar(arbol->izq, x);
else if(x > arbol->nro)
insertar(arbol->der, x);
}
void preOrden(ABB arbol)
{
if(arbol!=NULL)
{
cout << arbol->nro <<" ";
preOrden(arbol->izq);
preOrden(arbol->der);
}
}
void enOrden(ABB arbol)
{
if(arbol!=NULL)
{
enOrden(arbol->izq);
cout << arbol->nro << " ";
enOrden(arbol->der);
}
}
void postOrden(ABB arbol)
{
if(arbol!=NULL)
{
postOrden(arbol->izq);
postOrden(arbol->der);
cout << arbol->nro << " ";
}
}
void verArbol(ABB arbol, int n)
{
if(arbol==NULL)
return;
verArbol(arbol->der, n+1);
for(int i=0; i<n; i++)
cout<<" ";
cout<< arbol->nro <<endl;
verArbol(arbol->izq, n+1);
}
void menu(){
system("color F5");
cout<<"\n\311\311\315\315\315\315\315\315\315\315\315\315\315\315\311\315\315\315\315\315\
315\315\315\315\315\315\315\273"<<endl;
cout<<"** ARBOL BINARIO **"<<endl;
cout<<"***Alejandro Mondragon Perez***"<<endl;
cout<<" E L I G E TU O P C I O N"<<endl;
cout<<"1.- CREAR ESTRCTURA DEL ARBOL"<<endl;
cout<<"2.- SALIR"<<endl;
cout<<"\n\311\311\315\315\315\315\315\315\315\315\315\315\315\315\311\315\315\315\315\315\
315\315\315\315\315\315\315\273"<<endl;
}
int main()
{
ABB arbol = NULL;
int n;
int x;
cout << "\n\t\t ..[ ARBOL BINARIO DE BUSQUEDA ].. \n\n";
cout << "\n\t\t ..[ ALEJANDRO MONDRAGON PEREZ ].. \n\n";
int opc;
menu();
cout<<"\n=)";
cin>>opc;
while (opc != 2){
switch(opc){
case 1:
cout << " Por favor introduse el Numero de nodos que tendra el arbol: ";
cin >> n;
cout << endl;
for(int i=0; i<n; i++)
{
cout << " Numero del nodo " << i+1 << ": ";
cin >> x;
insertar( arbol, x);
}
cout << "\n MUESTRA DEL ARBOL \n\n";
verArbol( arbol, 0);
cout << "\n Recorridos del ARBOL BINARIO";
cout << "\n\n En orden : "; enOrden(arbol);
cout << "\n\n Pre Orden : "; preOrden(arbol);
cout << "\n\n Post Orden : "; postOrden(arbol);
cout << endl << endl;
system("pause");
break;
case 2:
return 0;
break;
case 3:
default:
cout<<"Opcion no valida"<<endl;
break;
}
menu();
cout<<"\n=)";
cin>>opc;
}
}
IMPRESIONES DE PANTALLA:


No hay comentarios:
Publicar un comentario