Bináris keresőfa Látványterv Java

szavazat
1

Hi Én jelenleg ezzel a tesztelési fázisban a projektem (algoritmus vizualizációs). Kapok egy probléma a törlési eljárás én 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;}

A kódot az BST osztály maga sokkal hosszabb. Minden jól működik, kivéve, hogy amikor megpróbál törölni egy csomópont nem gyerek, kapok egy nullpointer kivétel, amikor használja például a 9. és 10. input (próbálja del 10) vagy az 5. és a 12. (próbálja del 12), de soha nem ha a felhasználó például a 4. és 8. (próbálja del 8) vagy 9, 6 és 5 azt hiszem, a probléma az 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;}

Tényleg kell segíteni this.I küldhet az egész osztály ha kell .. Köszönöm!

A kérdést 24/03/2011 17:42
a forrás felhasználó
Más nyelveken...                            


1 válasz

szavazat
0

Csak néhány megjegyzés:

  1. Ha át null ellenőrizni, azt kap egy NPE ott.
  2. if( check(current) == 0) stb -> érdemes megnézni egyszer, majd végrehajtja a if (vagy akár egy kapcsoló)

Példa 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: // Igazából elfelejteni ezt a szerkesztés, most láttam ez nem történhet meg, de ez még mindig nem jó stílus

Arra is van a dolgok, mint ez a kód:

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

Ha current.leftsemmis és current.rightnull, currentnull lesz utána.

Válaszolt 24/03/2011 18:04
a forrás felhasználó

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