La visualización binaria Búsqueda Árbol En Java

votos
1

Hola estoy haciendo actualmente la fase de prueba de mi proyecto (herramienta de visualización Algorithm). Me estoy haciendo un problema con el método de borrado de mi BST.

 public boolean delete(String key) {
boolean deleted = true;
boolean finished=false;
BNode current = root;
BNode prev = null;
while (!finished) {
  if (key.compareTo(current.key) > 0) {
    prev = current;
    current = current.right;
    this.repaint();
  }
  else if (key.compareTo(current.key) < 0) {
    prev = current;
    current = current.left;
    this.repaint();
  }
  else if (key.compareTo(current.key) == 0) {
      finished=true;
      this.repaint();
  }

}

if (check(current) == 0) {
    if(current==root)
    {
        root=null;
        xPos=400;
        yPos=60;
        this.repaint();
    }
    else
    {
        if (current.key.compareTo(prev.key) > 0) {
            prev.right = null;
            this.repaint();
        }
        else if(current.key.compareTo(prev.key) < 0) {
            prev.left = null;
            this.repaint();
        }
    }

}
else if (check(current) == 1) {
    if(current==root)
    {
        prev=current;
        if (current.left != null) {
            current=current.left;
            prev.key=current.key;
            prev.left = current.left;
            this.repaint();
        }
        else {
            current=current.right;
            prev.key=current.key;
            prev.right = current.right;
            this.repaint();
        }
    }
    else
    {

    if (current.key.compareTo(prev.key) > 0) {
    if (current.left != null) {
      prev.right = current.left;
      this.repaint();
    }
    else {
      prev.right = current.right;
      this.repaint();
    }
  }
  else if(current.key.compareTo(prev.key) < 0) {
    if (current.left != null) {
      prev.left = current.left;
      this.repaint();
    }
    else {
      prev.left = current.right;
      this.repaint();
    }
  }
    }
}
else if (check(current) == 2) {
  BNode temp = inord(current);
  if(current==root)
  {
      root.key=temp.key;
      this.repaint();
  }
  else
  {

      if (current.key.compareTo(prev.key) > 0) {
      prev.right.key = temp.key;
      this.repaint();
    }
    else {
      prev.left.key = temp.key;
      this.repaint(0);
    }
    }
}

return deleted;}

El código para la propia clase BST es mucho más largo. Todo funciona bien, excepto que cuando intento para eliminar un nodo con ningún niño, recibo una excepción NullPointer cuando se utiliza, por ejemplo, 9 y 10 como entrada (intentar del 10) o el 5 y el 12 (tratar del 12), pero nunca si el usuario, por ejemplo, 4 y 8 (prueba a del 8) ó 9, 6 y 5. Creo que el problema es con compareTo.

int check(BNode a) {
int ret;
if ( (a.left != null) && (a.right != null)) {
  ret = 2;
}
else if ( (a.left == null) && (a.right == null)) {
  ret = 0;
}
else {
  ret = 1;
}
return ret;}

Realmente necesito ayuda con this.I pueden publicar toda la clase si es necesario .. Gracias!

Publicado el 24/03/2011 a las 17:42
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
0

Sólo unas pocas notas:

  1. Si pasa nulo para comprobar, se obtendría una NPE allí.
  2. if( check(current) == 0) etc -> usted debe comprobar una vez y luego ejecutar el caso (o incluso un interruptor)

Ejemplo para 2 .:

 int result = check(current);
 switch(result) {
  case 0:
    //do whatever is appropriate
    break;
  case 1:
    //do whatever is appropriate
    break;
  case 2:
    //do whatever is appropriate
    break;
  default:
    //should never happen, either leave it or throw an exception if it ever happens
}

Editar: // En realidad, olvidar esta edición, acaba de ver esto no debería ocurrir, pero todavía no es un buen estilo

También tiene cosas como esta en el código:

if (current.left != null) {
    current=current.left;
    prev.key=current.key;
    prev.left = current.left;
    this.repaint();
}
else {
    current=current.right; //this might be null
 ...
}

Si current.leftes nulo y current.rightes nula, currentserá nulo después.

Respondida el 24/03/2011 a las 18:04
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more