Hi I am currently doing the testing phase of my project(Algorithm Visualization Tool). I am getting a problem with the delete method of my 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.repai开发者_StackOverflownt();
}
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;}
The code for the BST class itself is much longer. Everything is working fine except that when I try to delete a node with no child, I get a nullpointer exception when I use for example 9 and 10 as input(try to del 10) or 5 and 12(try to del 12) but never if I user for example 4 and 8(try to del 8) or 9, 6 and 5. I think the problem is with 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 really need help with this.I can post the whole class if need be.. Thank You!
Just a few notes:
- If you pass null to check, you'd get an NPE there.
if( check(current) == 0)
etc. -> you should check once and then execute the if (or even a switch)
Example for 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: //Actually, forget this edit, just saw this should not happen, but it's still not a good style
You also have things like this in your code:
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
...
}
If current.left
is null and current.right
is null, current
will be null afterwards.
精彩评论