One slogan raises the following:
"Develop a method that allows you to print on screen, so ascending, the code of each node next to the level where it is said node. "
It gives me to understand that I have to do an inorder tour but showing in an aggregate way the level of each node returned.
The predefined methods that give me to solve this now are the following:
TRAVEL INORDEN
public void imprimirEntreorden(NodoArbol p) {
if (p != null) {
imprimirEntreorden(p.getIzquierdo());
//Visito el nodo
System.out.println(p.getInfo());
//Visito el nodo
imprimirEntreorden(p.getDerecho());
}
}
GET NODE HEIGHT
public int obtenerAltura(NodoArbol nodo) {
int alturaParcial, alturaD = 0, alturaI = 0;
NodoArbol p = nodo;
if (p.getIzquierdo() != null) {
alturaI = obtenerAltura(p.getIzquierdo());
}
if (p.getDerecho() != null) {
alturaD = obtenerAltura(p.getDerecho());
}
if (alturaI > alturaD) {
alturaParcial = alturaI + 1;
} else {
alturaParcial = alturaD + 1;
}
return alturaParcial;
}
public int obtenerAltura() {
if (this.raiz == null) {
return 0;
} else {
return obtenerAltura(this.raiz);
}
}
THIS NEXT METHOD WAS MY MODIFICATION TO THE INORDEN METHOD TO RESOLVE THE SETTING
public void ImprimirAscNivel(NodoArbol p) {
int nivelNodo = 0;
if (p != null) {
nivelNodo = obtenerAltura(p);
ImprimirAscNivel(p.getIzquierdo());
//Visito el nodo
System.out.println("El valor del nodo es: "+p.getInfo()+" y el nivel es "+nivelNodo);
//Visito el nodo
ImprimirAscNivel(p.getDerecho());
}
}
The result is the following ...
The value of the node is: 1 and the level is 1
The value of the node is: 3 and the level is 2
The value of the node is: 7 and the level is 1
The value of the node is: 10 and the level is 3
The value of the node is: 11 and the level is 1
The value of the node is: 21 and the level is 2
The value of the node is: 23 and the level is 1
The value of the node is: 32 and the level is 4
The value of the node is: 33 and the level is 1
The value of the node is: 34 and the level is 2
The value of the node is: 36 and the level is 1
The value of the node is: 50 and the level is 3
The value of the node is: 51 and the level is 2
The value of the node is: 62 and the level is 1
The leaves mark me as level 1, and the root as the highest level, in this case 4. Should not it be the other way around? And if that is the case, how do I resolve it? Thank you very much!