Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1.     public int posicao(Node node, int x) {
  2.         if (node == null) {
  3.             return -1;
  4.         }
  5.         else if (x < node.getValor()) {
  6.             return posicao(node.getEsq(), x);
  7.         }
  8.         else if (x == node.getValor()) {
  9.             return (node.getEsq() != null ? node.getEsq().descendentes + 2 : 1);
  10.         }
  11.         else if (x > node.getValor()) {
  12.             return (node.getEsq() != null ? node.getEsq().descendentes + 1: 0) + 1 + posicao(node.getDir(), x);
  13.         }
  14.  
  15.         return -1;
  16.     }
  17.  
  18.     public void printPosicao(int x) {
  19.             int result = posicao(raiz, x);
  20.             if (result != -1) {
  21.                     System.out.println("\nPosição do elemento de valor " + x +":  " + result);
  22.             } else {
  23.                     System.out.println("Elemento não existe.\n");
  24.             }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement