Binarne drzewo poszukiwań Wizualizacja W Javie

głosy
1

Cześć Jestem obecnie robi fazę testowania mojego projektu (algorytm Wizualizacja Tool). Dostaję problem z metodą usuwania mojego 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;}

Kod dla samej klasie BST jest znacznie dłuższa. Wszystko działa poprawnie z wyjątkiem, że gdy próbuję usunąć węzeł bez dziecka, pojawia się wyjątek NullPointer kiedy używać na przykład 9 i 10 jako wejście (spróbuj del 10) lub 5 i 12 (spróbuj del 12), ale nigdy jeśli użytkownik na przykład 4 i 8 (spróbuj del 8) lub 9, 6 i 5. Myślę, że problem jest z 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;}

I naprawdę potrzebują pomocy z this.I możesz pisać całą klasę, jeśli trzeba .. Dziękuję!

Utwórz 24/03/2011 o 17:42
źródło użytkownik
W innych językach...                            


1 odpowiedzi

głosy
0

Zaledwie kilka uwag:

  1. Jeśli przekazać null w celu sprawdzenia, można uzyskać NPE tam.
  2. if( check(current) == 0) itd. -> należy sprawdzić raz, a następnie wykonać if (lub nawet wyłącznika)

Przykład 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
}

Edit: // rzeczywistości, zapomnieć o tej edycji, po prostu zobaczyłem to nie powinno się zdarzyć, ale wciąż nie jest to dobry styl

Trzeba również takie rzeczy jak to w kodzie:

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
 ...
}

Jeśli current.leftjest nieważna i current.rightjest null, currentbędzie null później.

Odpowiedział 24/03/2011 o 18:04
źródło użytkownik

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