#include
#include
#include
void InicializarABB(pArbol &A)
{
A=NULL;
}
//MOVIMIENTO EN ABB
void MostrarDato(int Dato)
{
printf("
%d",Dato);
}
void InOrden(pArbol &A)
{
if(A!=NULL)
{
InOrden(A->izq);
MostrarDato(A->Dato);
InOrden(A->der);
}
}
void PreOrden(pArbol &A)
{
if(A!=NULL)
{
MostrarDato(A->Dato);
PreOrden(A->izq);
PreOrden(A->der);
}
}
void PosOrden(pArbol &A)
{
if(A!=NULL)
{
PosOrden(A->izq);
PosOrden(A->der);
MostrarDato(A->Dato);
}
}
//INFORMACION DE ABB
int ArbolVacio(pArbol &A)
{
if(A==NULL)
return 1;
else
return 0;
}
int EsHoja(pArbol &A)
{
if((A->der==NULL)&&(A->izq==NULL))
return 1;
else
return 0;
}
void NumerosDeNodos(pArbol &A, int &x)
{
if(A!=NULL)
{
x++;
NumerosDeNodos(A->izq,x);
NumerosDeNodos(A->der,x);
}
}
int CantidadDeNodos(pArbol &A)
{
int Cantidad=0;
if(A!=NULL)
NumerosDeNodos(A,Cantidad);
return Cantidad;
}
int AlturaDeUnNodo(pArbol &A, int x)
{
int Altura=0;
Nodo Aux=A;
while(Aux!=NULL)
{
if(x==Aux->Dato)
return Altura;
else
{
Altura++;
if(x>Aux->Dato)
Aux=Aux->der;
else
Aux=Aux->izq;
}
}
if(Altura==0)
return 1;
return Altura;
}
void VaciarABB(pArbol &A)
{
if(A!=NULL)
{
VaciarABB(A->izq);
VaciarABB(A->der);
delete(A);
}
A=NULL;
}
int AlturaArbol(pArbol &A)
{
int AltDer=0,AltIzq=0;
if(A==NULL)
return -1;
else
{
AltIzq=AlturaArbol(A->izq);
AltDer=AlturaArbol(A->der);
if(AltIzq>AltDer)
AltIzq=AltIzq+1;
else
AltIzq=AltDer+1;
}
return AltIzq;
}
//OPRECIONES EN ABB
int BuscarMenorEnABB(pArbol &A)
{
Nodo Aux=A;
if(Aux!=NULL)
{
while(Aux->izq!=NULL)
Aux=Aux->izq;
}
return (Aux->Dato);
}
int BuscarMayorEnABB(pArbol &A)
{
Nodo Aux=A;
if(Aux!=NULL)
{
while(Aux->der!=NULL)
Aux=Aux->der;
}
return (Aux->Dato);
}
int BuscarElementoEnABB(pArbol &A, int x) // 1: Encontado, 0: No Existe
{
Nodo Aux=A;
if(Aux!=NULL)
{
if(Aux->Dato==x)
return 1;
else
{
if(Aux->Dato>x)
BuscarElementoEnABB(Aux->izq,x);
if(Aux->Dato
BuscarElementoEnABB(Aux->der,x);
}
}
return 0;
}
int InsertarNodoEnArbol(pArbol &A, int x)
{
int bandera=0;
pArbol Aux,Padre;
Aux=A;
Padre=NULL;
while((Aux!=NULL)&&(Aux->Dato!=x))
{
Padre=Aux;
if(Aux->Dato>x)
Aux=Aux->izq;
else
if(Aux->Dato
Aux=Aux->der;
}
if(Aux==NULL)
{
Aux=new Arbol;
Aux->Dato=x;
Aux->der=NULL;
Aux->izq=NULL;
if(Padre!=NULL)
{
if(Padre->Dato>x)
Padre->izq=Aux;
else
Padre->der=Aux;
bandera=1;
}
else
{
A=Aux;
bandera=1;
}
}
return bandera;
}
int EliminarNodoEnABB(pArbol &A, int x)
{
pArbol Aux;
if(A!=NULL)
{
Aux=A;
if(Aux->Dato==x)
{
if(EsHoja(Aux))//Es hoja
{
delete(Aux);
A=NULL;
return(1);
}
else
{
if(A->izq!=NULL)
{
Aux=Aux->izq;
while(Aux->der!=NULL)
Aux=Aux->der;
A->Dato=Aux->Dato;
Aux->Dato=x;
EliminarNodoEnABB(A->izq,x);
}
else
{
Aux=Aux->der;
while(Aux->izq!=NULL)
Aux=Aux->izq;
A->Dato=Aux->Dato;
Aux->Dato=x;
EliminarNodoEnABB(A->der,x);
}
}
}
else
if(A->Dato>x)
EliminarNodoEnABB(A->izq,x);
else
EliminarNodoEnABB(A->der,x);
}
else
return 0;
}
int PodaABB(pArbol &A, int x)
{
pArbol Aux;
int Bandera;
if(A!=NULL)
{
Aux=A;
if(Aux->Dato==x)
{
Aux->der=NULL;
Aux->izq=NULL;
Bandera=1;
}
else
{
if(Aux->Dato>x)
PodaABB(Aux->izq,x);
else
{
if(Aux->Dato<=x)
PodaABB(Aux->der,x);
}
}
}
else
Bandera=0;
return Bandera;
}
No hay comentarios:
Publicar un comentario