Professional Documents
Culture Documents
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
ARBOLES
1. INTRODUCCIÓN
2. DEFINICIÓN
3. REPRESENTACIÓN
Raíz
Raíz
Nivel 0
asM
Nivel 1
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
Nivel 2
Nivel 3
Arbol
Nodo Hoja= Nodo hoja es todo aquel nodo que no tenga descendientes,
en el ejemplo anterior los nodos hoja son:
TIPOS DE ARBOLES
Arboles Binarios
Son arboles que nos muestran orden, es decir para un ejemplo: Para
todo nodo A del árbol:
N áreas de enlace
Dato(s)
Objeto(s) .
1 2 3 N
. . .
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
b c
d e f
g h
i
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
bc
def
gh
Pre Orden (RID).- Recorre al árbol mostrado primero la Raíz del árbol,
luego toda la Izquierda de la raíz y al final toda la Derecha de la raíz.
ARBOLES HOMOGENEOS
SIMETRICOS
ArbolBinario
NodoA Nodo raíz
Si
4. DIAGRAMA DE CLASES Esvacia();
sd r_preorden();
Dato a) Pilas Múltiples r_Inorden();
r_Postorden()
Nodo() r_Niveles();
getdato() get_raiz();
setDato(Dato NroElem()
y) Abstract crea();
getsi(); Abstract carga(int x)
setsi(NodoA Abstract busqueda (int x)
x);
getSd();
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
ABBusqueda
ABNormall
Carga(iny x)
crea () Busqueda (int x)
Esvacia(); Esvacia();
r_preorden(); r_preorden();
r_Inorden(); r_Inorden();
r_Postorden() r_Postorden()
r_Niveles(); r_Niveles();
get_raiz(); get_raiz();
NroElem() NroElem()
5. APLICACIONES
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
6. IMPLEMENTACIÓN
NODO
public class NodoA {
NodoA izq;
int dato;
NodoA der;
public NodoA ()
{
izq = null;
der = null;
}
}
ARBOL
ArbolBinario ()
{
R = null;
}
if (R == null)
return true;
else
return false;
}
NodoA x = R;
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
p.adicion (null);
while (p.nroElementos () != 0)
{
while (x != null)
{
p.adicion (x);
if (x.der != null)
{
p.adicion (x.der);
p.adicion (null);
}
x = x.izq;
}
x = p.eliminacion ();
while (x != null)
{
System.out.print (x.dato + " ");
x = p.eliminacion ();
}
if (p.nroElementos () != 0)
{
x = p.eliminacion ();
}
}
NodoA x;
nivel.adicion (R);
int i = 0;
while (!nivel.esVacia ())
{
System.out.print ("Nivel : " + i);
while (!nivel.esVacia ())
{
x = nivel.eliminacion ();
System.out.print (" " + x.dato);
if (x.izq != null)
desc.adicion (x.izq);
if (x.der != null)
desc.adicion (x.der);
}
nivel.vaciar (desc);
System.out.println (" ");
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
i = i + 1;
}
}
int nroelem ()
{
NodoA x;
PilaN nivel = new PilaN ();
PilaN desc = new PilaN();
int ne = 0;
if (Esvacia())
return ne;
else
{
nivel.adicion (R);
while (!nivel.esVacia ())
{
while (!nivel.esVacia ())
{
x = nivel.eliminacion ();
ne++;
if (x.izq != null)
desc.adicion (x.izq);
if (x.der != null)
desc.adicion (x.der);
}
System.out.println ();
while (!desc.esVacia ())
super ();
}
{
super ();
}
if (y.izq == null)
System.out.print ("No se encuentra");
y = y.izq;
}
else
{
if (x > y.dato)
{
if (y.der == null)
System.out.print ("No se encuentra");
y = y.der;
}
else
{
System.out.print ("El dato se encuentra en el
arbol");
y = null;
}
}
}
}
}
7. EJERCICIOS RESUELTOS
nivel.adicion (R);
while (!nivel.esVacia ())
{
System.out.println ("\n Nivel :" + niv);
while (!nivel.esVacia ())
{
x = nivel.eliminacion ();
if (x.izq == null && x.der == null)
System.out.print (x.dato + " ");
if (x.izq != null)
desc.adicion (x.izq);
if (x.der != null)
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
desc.adicion (x.der);
}
System.out.println ();
while (!desc.esVacia ())
nivel.adicion (desc.eliminacion ());
niv++;
}}
{
ABNormal a=new ABNormal();
System.out.println("CREAR ARBOL");
a.crea();
System.out.println("MOSTRAR ARBOL");
a.r_Niveles();
System.out.println("HOJAS DE UN ARBOL");
a.hojas();
}}
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
5
0
2 7
7 0
1 3 6 7
0 0 0 5
2 6
1
8 5
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
CLASS RECTANGULO
CLASS ARBOL
ArbolBinario ()
{
R = null;
}
NodoA x;
nivel.adicion (R);
int i = 0;
while (!nivel.esVacia ())
{
System.out.print ("Nivel : " + i);
while (!nivel.esVacia ())
{
x = nivel.eliminacion ();
x.dato.mostrar();
System.out.print(" ");
if (x.izq != null)
desc.adicion (x.izq);
if (x.der != null)
desc.adicion (x.der);
}
nivel.vaciar (desc);
System.out.println (" ");
i = i + 1;
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
}
}
}
CLASS NODOA
public class NodoA {
NodoA izq;
rectang dato=new rectang();
NodoA der;
public NodoA ()
{
izq = null;
der = null;
}
}
CLASS ABNORMAL
NodoA x;
int niv = 0;
PilaN nivel = new PilaN ();
PilaN desc = new PilaN ();
nivel.adicion (R);
while (!nivel.esVacia ())
{
System.out.println ("\n Nivel :" + niv);
while (!nivel.esVacia ())
{
x = nivel.eliminacion ();
if (x.izq == null && x.der == null)
System.out.print (x.dato + " ");
if (x.izq != null)
desc.adicion (x.izq);
if (x.der != null)
desc.adicion (x.der);
}
System.out.println ();
while (!desc.esVacia ())
nivel.adicion (desc.eliminacion ());
niv++;
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
}
}
boolean eshoja(NodoA x)
{
if(x.der==null && x.izq==null)
return (true);
else
return(false);
}
CLASS MANEJO
class manejo
{
public static void contar(ABNormal a)
{
int c=0;
NodoA x;
PilaN nivel= new PilaN();
PilaN desc=new PilaN();
nivel.adicion(a.R);
while(!nivel.esVacia())
{
while(!nivel.esVacia())
{
x=nivel.eliminacion();
if(x.dato.verif())
c=c+1;
if(x.izq!=null)
desc.adicion(x.izq);
if(x.der!=null)
desc.adicion(x.der);
}
nivel.vaciar(desc);
}
System.out.print(c);
}
int c=0;
aux.adicion (null);
x = a.R;
while (x != null)
{
if(x.der!=null && x.izq!=null)
if(a.eshoja(x.der)&& a.eshoja(x.izq))
c=c+1;
if (x.der != null)
aux.adicion (x.der);
if (x.izq != null)
x = x.izq;
else
x = aux.eliminacion ();
}
return(c);
}
a.r_Niveles();
System.out.println("LOS CUADRADOS DENTRO DEL ARBOL SON:");
contar(a);
System.out.println(" ");
System.out.println("EL ARBOL TIENE "+ cont(a)+ " Nodos con 2
nodos hojas");
}
}
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
8. EJERCICIOS PROPUESTOS
5 5
1 1 1 1
2 4 2 4
1 1 2 5 1 1 2 5
6 9 2 1 6 1 2 1
1 2 2 1
6 6
1 0 0 9
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
NodoA
Si
sd ArbolBinario
nombre Nodo raíz
deuda
ABBusque
Esvacia(); da
NodoA( …
)
….
…
UNIVERSIDAD MAYOR DE SAN ANDRES
CARRERA INFORMATICA
LAB 131
PILAS Y COLAS MULTIPLES
(GUIA NO. 7)
Paralelos: “A”y”B” Gestion 1/2010
Responsables: Lic. Victoria Hurtado Cerruto Aux. Doc.: Univ. Jonathan W. Magne Q.
9. BIBLIOGRAFIA