我正在尝试创建一种方法来删除 BST 中的节点并且它不起作用..我正在使用我自己制作的 Node 类..尝试调试但在解决代码中的错误方面没有得到太多帮助。
感谢有关如何使其工作的任何帮助。
public boolean delete(Node z)
{
if (z == null)
return false;
Node x,y;
if( z.getLeft() == null || z.getRight()== null)
y = z;
else {
y = (Node) successor(z);
}
if (y.getLeft() != null)
x = y.getLeft();
else x = y.getRight();
if(x != null)
x.setParent(y.getParent());
if(y.getParent() == null) {
this.node=x;
}
else if (y == y.getParent().getLeft())
{
y.getParent().setLeft(x);
}
else y.getParent().setRight(x);
if(y==z)
z.setKey(y.getKey());
return true;
}
public Node treeMinimum(Node x) {
while (x.getLeft() != null)
x = x.getLeft();
return x;
}
public Node successor(Node node) {
Node x = node;
if (x.getRight() != null)
return treeMinimum(x.getRight());
Node y = x.getParent();
while (y != null && x == y.getRight()) {
x = y;
y = y.getParent();
}
return y;
}