-1
I have a question, how to make a function that returns the Knot with the highest value of a tree B that stores integers. I’m trying to adapt the ride code in order to try to return, but only returns null. In the Node class I created the following function
Node class
public Node maiorValor() {
int i;
Node aux=null;
for (i = 0; i < this.n; i++) {
if (this.filhos[i] != null) {
this.filhos[i].percorrerEmOrdem();
}
aux = this.filhos[i]; // recebe o valor
}
if (this.filhos[i] != null) {
this.filhos[i].percorrerEmOrdem();
}
return aux;// retorna aux
}
}
`
In Btree class I created the following function that will be called by the Main class that has a Btree instance
public Node maiorValor() {
int i;
Node aux;
if (this.isEmpty() == false) {
aux= root.maiorValor();
return aux;
} else {
return null;
}
}
}
Can someone help me?
If it is a B tree, then it maintains the order of the elements. In this case, just take the leaf to the right (you don’t need to go through all the nodes). Something like
maiorValor() { if (this.filhos[this.n] == null) return this.filhos[this.n]; else return this.filhos[this.n].maiorValor(); }
– hkotsubo