这是作业;请不要只给我代码
我有两种方法:remove(T data)
和 removeRec(Node<T> node, T data)
.
在目前的状态下,我的代码似乎只删除了 root
BST的节点。
@Override
public T remove(T data) {
if (data == null) {
throw new IllegalArgumentException("Data is null");
}
if (root == null) {
throw new java.util.NoSuchElementException("BST is empty");
} else {
size--;
BSTNode<T> dummy = new BSTNode<T>(null);
return removeRec(root, data, dummy).getData(); //This is probably wrong too
}
}
/**
* Helper method to recursively search for, and remove the BSTNode with
* the given data in it
* @param node is the node we're currently at
* @param data is the data we're looking for
* @param temp I have no idea why
* @return node that was removed
*/
private BSTNode<T> removeRec(BSTNode<T> node, T data, BSTNode<T> temp) {
if (compare(data, node.getData()) < 0) {
temp.setLeft(removeRec(node.getLeft(), data, temp));
} else if (compare(data, node.getData()) > 0) {
temp.setRight(removeRec(node.getRight(), data, temp));
} else if (node.getLeft() != null && node.getRight() != null) {
temp.setData(findMin(node.getRight()).getData());
temp.setRight(removeRec(node.getRight(), data, temp));
} else {
if (node.getLeft() != null) {
temp = node.getLeft();
} else {
temp = node.getRight();
}
}
return temp;
}
private int compare(T a, T b) {
return a.compareTo(b);
}
我的导师告诉我(作为提示)我应该看看将第三个参数传递给方法的内容,在本例中为 BSTNode<T> temp
.我不明白这有什么帮助,或者如何利用它。我看不出使用第三个参数有什么帮助;而且我在网上也找不到任何关于您为什么要这样做的信息。
最佳答案
当您尝试从二叉搜索树中删除data
时,主要有三种可能性:
data
小于当前节点值:在左子树上调用 remove 或如果为 null 则抛出NoSuchElementException
。data
大于当前节点值:在右子树上调用 remove 或如果为 null 则抛出NoSuchElementException
。data
等于当前节点值。
1 和 2 非常简单,但 3 还有四种情况需要考虑:
3.1。 当前节点是一片叶子:左右子树均为空。只需将其 parent 中对当前节点的引用替换为 null。
3.2。 当前节点只有左 child :需要让当前节点的parent指向左子树,从而去掉当前点。为此,您可以实现一个函数来检查当前点是在 parent 的左子树还是右子树上,并相应地替换它。调用它看起来像这样:
replaceNodeInParent(node, node.getLeft(), parent);
3.3。 current node has only the right child:与3.4类似,但使用getRight()
代替getLeft()
。
3.4。 当前节点有左 child 和右 child :你应该保持BST的性质,即左边的所有节点都小于当前节点,右边的所有节点都大于当前节点。为此,您应该找到右边的最小值,将其复制到当前节点,并从右子树中删除它。像这样:
BSTNode<T> successor = findMin(node.getRight());
node.setData(successor.getData());
removeRec(node.getRight(), successor.getData(), node);
看起来您的 BSTNode
没有保存对 parent 节点的引用。如果是这样,我相信这就是 removeRec
的第三个参数 应该是什么。每次替换当前节点时都需要引用父节点,因此您可以根据需要设置父节点的左子树或右子树。
要进一步阅读,您可以查看此 article on Binary Search Trees来自维基百科。
关于java - 从二叉搜索树中递归删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28397521/