Binární vyhledávací strom Vizualizace V Javě

hlasů
1

Ahoj, já jsem v současné době dělá testovací fázi svého projektu (Algorithm Visualization Tool). Jsem stále problém s odstraněny způsobem mého 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;}

Kód pro samotný BST třídě je mnohem delší. Vše je v pořádku kromě toho, že když se snažím odstranit uzel s žádným dítětem, jsem dostal výjimku NullPointer když jsem použít například 9 a 10 jako vstup (zkuste del 10) nebo 5 a 12 (zkuste del 12), ale nikdy pokud se uživatel například 4 a 8 (pokusit del 8) nebo 9, 6 a 5. myslím, že problém je s 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;}

JÁ ve skutečnosti potřebovat pomoci s this.I může zveřejnit celou třídu v případě potřeby možné .. Děkuji!

Položena 24/03/2011 v 17:42
zdroj uživatelem
V jiných jazycích...                            


1 odpovědí

hlasů
0

Jen pár poznámek:

  1. Pokud předáte null zkontrolovat, že dostanete NPE tam.
  2. if( check(current) == 0) atd. -> byste měli zkontrolovat jednou a pak provést if (nebo dokonce přepínač)

Příklad 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: // Vlastně, zapomeňte na tuto úpravu, prostě viděl toto by se nemělo stát, ale je to stále není dobrý styl

Máte také věci, jako je to v kódu:

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

Pokud current.leftje null a current.rightnull, currentbude null později.

Odpovězeno 24/03/2011 v 18:04
zdroj uživatelem

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