how to add to a tree correctly

1

I have tried to work with this function but the only thing it does is fill the root I try to make a binary tree and when I input the values the only thing it does is the root does not add children neither to the left nor to the right what happens is that for example I enter 2 then 3 then 1 the father would have to be 2 left son 1 right son 3 but the only thing that this does is get only the father if I put 2 the father is 2 if I put 3 the father is 3 does not make the tree only the root is added

Here is my method below the class where you are

    public void addNodo(Nodo nodo, Nodo raiz) {
    Principal p = new Principal();

    if (raiz==null) {

        this.setRaiz(nodo);
        System.out.println("Agrego raiz");
    } else if (nodo.getValor() <= raiz.getValor()) {
        if (raiz.getHojaIzquierda() == null) {
            raiz.setHojaIzquierda(nodo);
        } else {
            addNodo(nodo, raiz.getHojaIzquierda());
        }
        System.out.println("Agrego izquierda");

   } else{
        if (raiz.getHojaDerecha() == null) {
             raiz.setHojaDerecha(nodo);
        } else {
             addNodo(nodo, raiz.getHojaDerecha());
        }
        System.out.println("Agrego derecha");
   }
}

I have my node class

public class Nodo {
/* Declaraciones de variables */
private int valor;

private Nodo padre;
private Nodo hojaIzquierda;
private Nodo hojaDerecha;

/* Constructor */
public Nodo(int valor) {
    this.valor = valor;
}

/* Setters y Getters */
public void setValor(int valor) {
    this.valor = valor;
}

public int getValor() {
    return valor;
}

public Nodo getPadre() {
    return padre;
}

public void setPadre(Nodo padre) {
    this.padre = padre;
}

public Nodo getHojaIzquierda() {
    return hojaIzquierda;
}

public void setHojaIzquierda(Nodo hojaIzquierda) {
    this.hojaIzquierda = hojaIzquierda;
}

public Nodo getHojaDerecha() {
    return hojaDerecha;
}

public void setHojaDerecha(Nodo hojaDerecha) {
    this.hojaDerecha = hojaDerecha;
}

@Override
public String toString() {
    return ""+valor;
}

}

This is my tree class

public class Arbol {

/* Atributos */
public Arbol() {
}

private Nodo raiz;

/* Contructories */
public Arbol(int valor) {
    this.raiz = new Nodo(valor);
}

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

/* Setters y Getters */
public Nodo getRaiz() {
    return raiz;
}

public void setRaiz(Nodo raiz) {
    this.raiz = raiz;
}

// Here is my method with problems     public void addNode (Node node, root node) {         Principal p = new Principal ();

    if (raiz==null) {
        this.setRaiz(nodo);
        System.out.println("Agrego raiz");
    } else if (nodo.getValor() <= raiz.getValor()) {

        if (raiz.getHojaIzquierda() == null) {
            raiz.setHojaIzquierda(nodo);
        } else {
            addNodo(nodo, raiz.getHojaIzquierda());
        }
        System.out.println("Agrego izquierda");

    } else{
        if (raiz.getHojaDerecha() == null) {
            raiz.setHojaDerecha(nodo);
        } else {
            addNodo(nodo, raiz.getHojaDerecha());
        }
        System.out.println("Agreago derecha");
    }
}

public void addNodo(Nodo nodo) {
    this.addNodo(nodo, this.raiz);
}
    
asked by fr gar 27.03.2018 в 19:14
source

2 answers

0

What I advise is that you rethink the methods of your tree addNodo(Nodo nodo, Nodo raiz) and public void addNodo(Nodo nodo) .

I work them in the following way:

    public void addNodo(Nodo nodo) {
        //Primero esta validación ya que existe la posibilidad de que la 
        //raiz este siempre con un valor asignado
        if (raiz != null) {
            addNodo(nodo, raiz);
        }else {
            raiz = nodo;
        }
    }

    private void addNodo(Nodo nodo, Nodo padre) {
        if (nodo.getValor() > padre.getValor()) {
            if (padre.getHojaDerecha() != null) {
                //Se aplica recursividad para ir recorriendo el árbol por la 
                //rama derecha
                addNodo(nodo, padre.getHojaDerecha());
            }else {
                //Se asigna el nodo en la posición vacia encontrada a la 
                //derecha
                padre.setHojaDerecha(nodo);
            }
        }else {
            if (padre.getHojaIzquierda() != null) {
                //Se aplica recursividad para ir recorriendo el árbol por la 
                //rama izquierda
                addNodo(nodo, padre.getHojaIzquierda());
            }else {
                //Se asigna el nodo en la posición vacia encontrada a la 
                //izquierda
                padre.setHojaIzquierda(nodo);
            }
        }
    }

In this link you will find more information about binary trees, link and in the following link information regarding recursion link

    
answered by 25.06.2018 в 07:30
0

With these methods you can solve your problem by adding.

public void add(Node node) {
    if (root != null) {
        add(root, node);
    }else {
        root = node;
    }
}

private void add(Node base, Node node) {
    if (node.getInformation() < base.getInformation()) {
        if (base.getLeft() != null) {
            add(base.getLeft(), node);
        }else {
            base.setLeft(node);
        }
    }else {
        if (base.getRight() != null) {
            add(base.getRight(), node);
        }else {
            base.setRight(node);
        }
    }
}
    
answered by 25.06.2018 в 16:40