java - Java 中的二叉搜索树可视化

标签 java swing visualization binary-search-tree

您好,我目前正在进行项目的测试阶段(算法可视化工具)。我的 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 作为输入(尝试 del 10)或 5 和 12(尝试 del 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;}

我真的需要这方面的帮助。如果需要的话我可以发布整个类(class).. 谢谢!

最佳答案

几点说明:

  1. 如果您传递 null 进行检查,则会得到一个 NPE。
  2. if( check(current) == 0) 等 -> 你应该检查一次,然后执行 if (甚至是一个开关)

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
}

编辑://实际上,忘记这个编辑,只是看到这不应该发生,但它仍然不是一个好的样式

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

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随后将为空。

关于java - Java 中的二叉搜索树可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5422533/

相关文章:

java - 使用 IgnoreCase 进行 Spring Data MongoDB 查询时来自 MongoQueryCreator 的 NullPointerException

java - 斐波那契算法的时间复杂度

java - 尝试在java项目中执行唯一性?

java - JTextArea/JTextComponent setText 导致崩溃

python - 根据百分位数绘制直方图

java - String[] 到单个字符串

java - Java Swing JPanel 可以保存一个字符串值吗?

java - 使用 EventDispatchThread 与不使用 EventDispatchThread 显示图像

javascript - d3.js 强制定向图不起作用

python - 什么轻量级 python 库,用于简单的 3D 科学可视化