How to create a function to display the largest numbers of a B tree in Java?

Asked

Viewed 52 times

-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?

  • 1

    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(); }

1 answer

-1

Hello, I don’t know if I understand what you need to do, but if you need to get the most value from an integer array just use the stream api.

   int[] nros = {3,6,7,20,4,5,98,65,7,9,43,57,86,46,8,34,67};
   int maior = Arrays.stream(nros).max().getAsInt();
   System.out.println(maior);

[]'s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.