Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int posicao(Node node, int x) {
- if (node == null) {
- return -1;
- }
- else if (x < node.getValor()) {
- int pos = posicao(node.getEsq(), x);
- if (pos == -1) return pos;
- return pos;
- }
- else if (x == node.getValor()) {
- return (node.getEsq() != null ? node.getEsq().descendentes + 2 : 1);
- }
- else if (x > node.getValor()) {
- int pos = posicao(node.getDir(), x);
- if (pos == -1) return pos;
- return (node.getEsq() != null ? node.getEsq().descendentes + 1: 0) + 1 + pos;
- }
- return -1;
- }
- public void printPosicao(int x) {
- int result = posicao(raiz, x);
- if (result != -1) {
- System.out.println("\nPosição do elemento de valor " + x +": " + result);
- } else {
- System.out.println("Elemento não existe.\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement