
arboles binarios con pilas.
Publicado por nacho (1 intervención) el 03/02/2010 19:49:37
Buenas, estoy en la facultad empezando a aprender el lenguaje java y tengo como un pequeño proyecto para hacer lo siguiente.
Me piden que haga un arbol binario :
You need to implement the iterative versions of the print routines. The iterative version
mimics the recursive routines by using a stack. The only exception is the level-order
traversal, that you need to use a queue
Es decir que haga las funciones usando una pila.
La verdad esque tengo algo del programa y me funciona, pero no se si lo he hecho como me piden.
aqui les dejo parte del programa por si alguien me pudiese ayudar y explicarme si lo estoy haciendo bien o no,
gracias
class Node {
int dato;
Node Cleft, Cright;
//Constructores
Node (int el){
dato = el;
Node Cleft, Cright = null;
}
//Insercion de un elemento
public void Insert (int el){
if(el < dato){
if (Cleft == null)
Cleft = new Node(el);
else
Cleft.Insert(el);
}
else{
if (el > dato){
if (Cright == null)
Cright = new Node (el);
else
Cright.Insert(el);
}
}
}
}
class Tree{
Cola Cola = new Cola();
Node Dad;
Node Root;
//Constructor
public Tree(){
Root = null;
}
//Insercion de un elemento en el Tree
public void InsertaNodo(int el){
if(Root == null)
Root = new Node (el);
else
Root.Insert (el);
}
}
Me piden que haga un arbol binario :
You need to implement the iterative versions of the print routines. The iterative version
mimics the recursive routines by using a stack. The only exception is the level-order
traversal, that you need to use a queue
Es decir que haga las funciones usando una pila.
La verdad esque tengo algo del programa y me funciona, pero no se si lo he hecho como me piden.
aqui les dejo parte del programa por si alguien me pudiese ayudar y explicarme si lo estoy haciendo bien o no,
gracias
class Node {
int dato;
Node Cleft, Cright;
//Constructores
Node (int el){
dato = el;
Node Cleft, Cright = null;
}
//Insercion de un elemento
public void Insert (int el){
if(el < dato){
if (Cleft == null)
Cleft = new Node(el);
else
Cleft.Insert(el);
}
else{
if (el > dato){
if (Cright == null)
Cright = new Node (el);
else
Cright.Insert(el);
}
}
}
}
class Tree{
Cola Cola = new Cola();
Node Dad;
Node Root;
//Constructor
public Tree(){
Root = null;
}
//Insercion de un elemento en el Tree
public void InsertaNodo(int el){
if(Root == null)
Root = new Node (el);
else
Root.Insert (el);
}
}
Valora esta pregunta


0