miércoles, 24 de junio de 2015

Arbol binario

Un árbol binario es un árbol de orden 2, es decir tiene solo dos hijos. Se puede crear una estructura de árbol binario en Java y es lo que se va a explicar a continuación. El árbol binario tiene dos hijos (izquierda, y derecha), a su vez un árbol puede ser el conjunto de varios arboles por lo que se puede unir dos arboles, formando un árbol nuevo. Se va a tener tres clases, Arbol, Nodo y el Main


En la clase Nodo se empieza se declara 3 atributos de la clase: dato, izquierda y derecha.

  private Object dato;
    private Object value;
    private String key;
    private Nodo left;
    private Nodo right;
    
    public Nodo(String key,Object value){
        this.key = key;
        this.value = value;
        this.left = this.right = null;
    }

A continuación en la clase Arbol se declara un atributo raíz
  
  private Nodo raiz;

Los Nodos tiene los siguientes métodos:

    public void setIzq(String key, Object value) {
        this.setLeft(new Nodo(key, value));
    }

    public void setDer(String key, Object value) {
        this.setRight(new Nodo(key, value));
    }
    
El árbol puede de igual forma ingresar a la derecha o a la izquierda de la raíz:

    public void setIzq(String key, Object value) {
        this.raiz.setLeft(new Nodo(key, value));
    }

    public void setDer(String key, Object value) {
        this.raiz.setRight(new Nodo(key, value));
    }
Para imprimir el árbol de forma prefija, infija y postfija:
    private void prefijo(Nodo a) {
        if (a != null) {
            System.out.println(a.getValue());
            prefijo(a.getLeft());
            prefijo(a.getRight());
        }
    }

    public void prefijo() {
        prefijo(this.raiz);
    }
private void infijo(Nodo a) {
        if (a != null) {
            prefijo(a.getLeft());
            System.out.println(a.getValue());
            prefijo(a.getRight());
        }
    }

    public void infijo() {
        infijo(this.raiz);
    }

    private void postfijo(Nodo a) {
        prefijo(a.getLeft());
        prefijo(a.getRight());
        System.out.println(a.getValue());
    }

    public void postfijo() {
        postfijo(this.raiz);
    }
Y también puede unir dos árboles
    public Arbol joinDer(Arbol b) {
        Arbol nuevo = new Arbol("c", "raiz c");
        nuevo.raiz.setRight(b.raiz);
        nuevo.raiz.setLeft(this.raiz);
        return nuevo;
    }

    public Arbol joinIzq(Arbol b) {
        Arbol nuevo = new Arbol("c", "raiz c");
        nuevo.raiz.setRight(this.raiz);
        nuevo.raiz.setLeft(b.raiz);
        return nuevo;
    }
La clase nodo quedaría de la siguiente manera:
/**
 *
 * @author robertoarmas
 */
public class Nodo {
    
    private Object value;
    private String key;
    private Nodo left;
    private Nodo right;
    
    public Nodo(String key,Object value){
        this.key = key;
        this.value = value;
        this.left = this.right = null;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public Nodo getLeft() {
        return left;
    }

    public void setLeft(Nodo left) {
        this.left = left;
    }

    public Nodo getRight() {
        return right;
    }

    public void setRight(Nodo right) {
        this.right = right;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
    
    public void setIzq(String key, Object value) {
        this.setLeft(new Nodo(key, value));
    }

    public void setDer(String key, Object value) {
        this.setRight(new Nodo(key, value));
    }
    
}

La clase Arbol.java
/**
 *
 * @author robertoarmas
 */
public class Arbol {

    private Nodo raiz;

    public Arbol() {
        this.raiz = null;
    }
    
    private Arbol(Nodo nodo){
        this.raiz = nodo;
    }

    public Arbol(String key, Object value) {
        this.raiz = new Nodo(key, value);
    }

    private void prefijo(Nodo a) {
        if (a != null) {
            System.out.println(a.getValue());
            prefijo(a.getLeft());
            prefijo(a.getRight());
        }
    }

    public void prefijo() {
        prefijo(this.raiz);
    }

    private void infijo(Nodo a) {
        if (a != null) {
            prefijo(a.getLeft());
            System.out.println(a.getValue());
            prefijo(a.getRight());
        }
    }

    public void infijo() {
        infijo(this.raiz);
    }

    private void postfijo(Nodo a) {
        prefijo(a.getLeft());
        prefijo(a.getRight());
        System.out.println(a.getValue());
    }

    public void postfijo() {
        postfijo(this.raiz);
    }
    
    public Nodo getIzq(){
        return this.raiz.getLeft();
    }
    
    public Nodo getDer(){
        return this.raiz.getRight();
    }
    
    public Object getValueDer(){
        return this.raiz.getRight().getValue();
    }
    
    public Object getValueIzq(){
    return this.raiz.getLeft().getValue();
    }

    public void setIzq(String key, Object value) {
        this.raiz.setLeft(new Nodo(key, value));
    }

    public void setDer(String key, Object value) {
        this.raiz.setRight(new Nodo(key, value));
    }

    public Arbol joinDer(Arbol b) {
        Arbol nuevo = new Arbol("c", "raiz c");
        nuevo.raiz.setRight(b.raiz);
        nuevo.raiz.setLeft(this.raiz);
        return nuevo;
    }

    public Arbol joinIzq(Arbol b) {
        Arbol nuevo = new Arbol("c", "raiz c");
        nuevo.raiz.setRight(this.raiz);
        nuevo.raiz.setLeft(b.raiz);
        return nuevo;
    }

}

Y la clase Main.java
 Arbol b = new Arbol("a2", "raiz b");
        
        Arbol a = new Arbol("a","raiz a");
        a.setIzq("b", "hola");
        a.setDer("d", "mundo");
        a.getIzq().setIzq("c", "hijo de b");
        System.out.println(" ++++++++++++++++++++++++++++++PREFIJO ++++++++++++++++++++++++++++++");
        a.prefijo(); // Imrpimira
        System.out.println(" ++++++++++++++++++++++++++++++INFJO  ++++++++++++++++++++++++++++++");
        a.infijo();
        System.out.println(" ++++++++++++++++++++++++++++++POSTFIJO  ++++++++++++++++++++++++++++++");
        a.postfijo();
        System.out.println(" ++++++++++++++++++++++++++++++");
        System.out.println(a.getValueIzq()); // hola
        System.out.println(" ++++++++++++++++++++++++++++++");
        Arbol c = a.joinDer(b);
        c.prefijo();
De este modo nos da una salida:
 ++++++++++++++++++++++++++++++PREFIJO ++++++++++++++++++++++++++++++
raiz a
hola
hijo de b
mundo
 ++++++++++++++++++++++++++++++INFJO  ++++++++++++++++++++++++++++++
hola
hijo de b
raiz a
mundo
 ++++++++++++++++++++++++++++++POSTFIJO  ++++++++++++++++++++++++++++++
hola
hijo de b
mundo
raiz a
 ++++++++++++++++++++++++++++++
hola
 ++++++++++++++++++++++++++++++
raiz c
raiz a
hola
hijo de b
mundo
raiz b

No hay comentarios:

Publicar un comentario