gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:48:04 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com