【问题标题】:Binary Search Tree Visualisation In JavaJava中的二叉搜索树可视化
【发布时间】:2011-03-24 16:42:31
【问题描述】:

您好,我目前正在进行项目的测试阶段(算法可视化工具)。我的 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;}

BST 类本身的代码要长得多。一切正常,除了当我尝试删除没有子节点的节点时,当我使用例如 9 和 10 作为输入(尝试删除 10)或 5 和 12(尝试删除 12)但从不如果我使用例如 4 和 8(尝试删除 8)或 9、6 和 5。我认为问题出在 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;}

我真的需要这方面的帮助。如果需要,我可以发布整个课程。 谢谢!

【问题讨论】:

  • 你能发布 NPE 的堆栈跟踪吗?
  • 所以不涉及可视化?然后你应该改变你的帖子的标题,因为人们希望被问到一些可视化问题。

标签: java swing visualization binary-search-tree


【解决方案1】:

只是一些注意事项:

  1. 如果您将 null 传递给检查,您将在那里获得 NPE。
  2. if( check(current) == 0) 等 -> 你应该检查一次,然后执行 if(甚至是 switch)

示例 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: //其实忘了这个edit,刚看到这应该不会发生,但还是不是很好的风格

您的代码中也有类似的内容:

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

如果current.left 为空且current.right 为空,则current 之后将为空。

【讨论】:

  • 其实如果我输入 5 和 18 并想删除 18。current 指向 root,prev 指向 null。因此 18.compareTo(5)>0 prev 指向根节点, current 指向包含 18 的节点,然后 18.compareTo(18) 导致循环终止。 check(current) 不应该给 NPE;但确实如此。如果我输入 7 和 8 并删除 8,它可以正常工作。
  • 所以请发布堆栈跟踪。此外,您可能需要在第一个 while 循环之后调试并检查 current 是否真的不为空。
  • 我只是将属性键的类型更改为 int 并在各处比较整数。现在一切正常。一定是某处搞错了。谢谢你们..
猜你喜欢
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
相关资源
最近更新 更多