Está en la página 1de 7

#include<iostream.

h>
#include<conio.h>
struct nodo
{
int valor;
struct nodo* izq;
struct nodo* der;
};

typedef struct nodo* ABB;

void inserta(ABB&,int);
void preorden(ABB);
void postorden(ABB);
void enorden(ABB);
void verArbol(ABB,int);
bool Buscar(ABB,int);
int BuscarMayor(ABB);
int BuscarMenor(ABB);
void Podar(ABB&,int);
void hojas(ABB);
int cuenta(ABB);
void res(ABB);

void main(void)
{
ABB arbol=NULL;
int n,x,opcion;
do
{
//----------MEN� PRINCIPAL-----------------------
cout<<" +--------------------------+"<<endl;
cout<<" | MENU PRINCIPAL |"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" | 1 - Ingresar Elementos |"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" +--------RECORRIDOS--------+"<<endl;
cout<<" | 2 - Preorden |"<<endl;
cout<<" | 3 - Postorden |"<<endl;
cout<<" | 4 - En Orden |"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" | 5 - Visualizar Arbol |"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" +--------BUSQUEDAS---------+"<<endl;
cout<<" | 6 - Buscar Elemento |"<<endl;
cout<<" | 7 - Buscar Mayor |"<<endl;
cout<<" | 8 - Buscar Menor |"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" +-----------CORTES---------+"<<endl;
cout<<" | 9 - Podar |"<<endl;
cout<<" | 10- Talar(Elim. Arbol) |"<<endl;
cout<<" | 11- hojas |"<<endl;
cout<<" | 12- CUENTA DEL ARBOL IZQ-DER|"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" | 0 - SALIR |"<<endl;
cout<<" +--------------------------+"<<endl;
cout<<" |Ingrese su Opcion: ";cin>>opcion;
cout<<" +--------------------------+"<<endl<<endl;

switch(opcion)
{
case 1:
{
cout<<"Ingrese cantidad de elemtos del arbol: ";
cin>>n;
for(int i=0;i<n; i++)
{
cout<<"Ingrese nodo numero "<<i<<": ";
cin>>x;
inserta(arbol,x);
}
getch();
break;
}
case 2:
{
if(arbol!=NULL)
{
cout<<endl<<"Preorden : "; preorden(arbol);
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 3:
{
if(arbol!=NULL)
{
cout<<endl<<"Postorden : "; postorden(arbol);
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 4:
{
if(arbol!=NULL)
{
cout<<endl<<"En Orden : "; enorden(arbol);
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 5:
{
cout<<" VISUALIZACION DEL ARBOL "<<endl<<endl;
if(arbol!=NULL)
{
verArbol(arbol,0);
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 6:
{
int elem;
if(arbol!=NULL)
{
cout<<" -->Ingrese Elemento a buscar: ";
cin>>elem;

if (Buscar(arbol, elem))
cout<<" --> "<<elem<<" Encontrado ......";
else
cout<<" --> "<<elem<<" NO Encontrado ......";
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 7:
{
if(arbol!=NULL)
{
cout<<" -->El Mayor Elemento es: "<<BuscarMayor(arbol);
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 8:
{
if(arbol!=NULL)
{
cout<<" -->El Mayor Elemento es: "<<BuscarMenor(arbol);
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 9:
{
int corte;
if(arbol!=NULL)
{
cout<<" -->Ingrese Elemento de Corte: ";
cin>>corte;
if (Buscar(arbol, corte))
{
if (arbol->valor == corte)
arbol = NULL;
else
Podar(arbol, corte);
cout<<" El Arbol fue podado con exito......";
}
else
cout<<" El Elemento noesta en el arbol.....";
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
}
case 10:
{
if(arbol!=NULL)
{
arbol=NULL;
cout<<" El Arbol fue Talado con exito......";
}
else
{
cout<<" -->El arbol no tiene Elementos";
}
getch();
break;
case 11:
{cout<<"las hojas son :"<<endl;
hojas(arbol);

}getch;
break;
case 12:
{ res(arbol);

}getch;
break;

}
}

clrscr();

}while(opcion!=0);
}
void inserta(ABB& arbol,int x)
{
if(arbol==NULL)
{
arbol=new(struct nodo);
arbol->valor=x;
arbol->izq=NULL;
arbol->der=NULL;
}
else
{
if(x<arbol->valor) inserta(arbol->izq,x);
else
if(x>arbol->valor) inserta(arbol->der,x);
}
}//SI ES IGUAL NO SE INSERTA ---

void preorden(ABB arbol)


{
if(arbol!=NULL)
{
cout<<arbol->valor<<" ";
preorden(arbol->izq);
preorden(arbol->der);
}
}

void enorden(ABB arbol)


{
if(arbol!=NULL)
{
enorden(arbol->izq);
cout<<arbol->valor<<" ";
enorden(arbol->der);
}
}

void postorden(ABB arbol)


{
if(arbol!=NULL)
{
postorden(arbol->izq);
postorden(arbol->der);
cout<<arbol->valor<<" ";
}
}

void verArbol(ABB arbol,int nro)


{
int i;
if(arbol==NULL)return;
verArbol(arbol->der,nro+1);
for(i=0;i<nro;i++)
cout<<" ";
cout<<arbol->valor<<endl;
verArbol(arbol->izq,nro+1);
}

bool Buscar(ABB arbol, int buscado)


{
ABB A1;
A1=arbol;
while (A1 != NULL)
{
if (buscado < A1->valor )
A1 = A1->izq;
else
if (buscado > A1->valor)
A1 = A1->der;
else
return true;
}
return false;
}

int BuscarMenor(ABB arbol)


{
int menor;
while(arbol->izq!=NULL)
arbol=arbol->izq;
menor=arbol->valor;
return menor;
}

int BuscarMayor(ABB arbol)


{
int mayor;
while(arbol->der!=NULL)
arbol=arbol->der;
mayor=arbol->valor;
return mayor;
}

void Podar(ABB& A1, int corte)


{
while (A1 != NULL)
{
if (corte < A1->valor)
if(A1->izq->valor == corte)
{
A1->izq=NULL;
break;
}
else
A1 = A1->izq;
else
if (A1->der->valor == corte)
{
A1->der=NULL;
break;
}
else
A1 = A1->der;
}
}

void hojas(ABB arbol)


{ABB A;
A=arbol;
if(A==NULL)
return ;
else
{if(A->izq==NULL && A->der==NULL)
cout<<A->valor<<" ";
else
hojas(A->izq);
hojas(A->der);
}
}

int cuenta(ABB arbol)


{int cont;
ABB A;
A=arbol;
if(A!=NULL)
{cont++;
cuenta(A->der);
cuenta(A->izq);
}
return cont;
}

void res(ABB arbol)


{int k,i,cont=0;
ABB a,b;
a=arbol->der;
b=arbol->izq;
cont=0;
k=(cuenta(a));
cout<<"A LA DERECHA HAY"<<k<<"ELEMENTOS"<<endl;
cont=0;
i=cuenta(b);
cout<<"A LA IQUIERDA HAY"<<i<<"ELEMENTOS"<<endl;
}

También podría gustarte