14 java algorithm recursion binary-search-tree
这是作业; 请不要只给我代码
我有两种方法: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);
}
Run Code Online (Sandbox Code Playgroud)
我的导师告诉我(作为提示)我应该看到在这种情况下将第三个参数传递给方法的内容BSTNode<T> temp.我不明白这有什么帮助,或者如何利用它.我没有看到使用第三个参数有何帮助; 我在网上找不到你为什么这么做的事.
当您尝试data从二进制搜索树中删除时,有三种主要可能性:
data小于当前节点值:在左子树上调用remove,NoSuchElementException如果为null则抛出a .data大于当前节点值:在右子树上调用remove,NoSuchElementException如果为null则抛出a .data 等于当前节点值.1和2非常简单,但3还有四个案例要考虑:
3.1.当前节点是一个叶子:左右子树都是空的.只需将其父节点中当前节点的引用替换为null即可.
3.2.当前节点只有左子节点:您需要使当前节点的父节点指向左子树,从而删除当前点.为此,您可以实现一个函数,该函数将检查当前点是否在父节点的左子树或右子树上,并相应地替换它.调用它看起来像这样:
replaceNodeInParent(node, node.getLeft(), parent);
Run Code Online (Sandbox Code Playgroud)
3.3.当前节点只有正确的子节点:类似于3.4,但使用getRight()而不是getLeft().
3.4.当前节点同时具有左子节点和右子节点:您应该保持BST的属性,即左侧的所有节点都小于当前节点,右侧的所有节点都大于当前节点.为此,您应找到右侧的最小值,将其复制到当前节点,然后从右侧子树中删除它.像这样的东西:
BSTNode<T> successor = findMin(node.getRight());
node.setData(successor.getData());
removeRec(node.getRight(), successor.getData(), node);
Run Code Online (Sandbox Code Playgroud)
看起来您BSTNode没有对父节点的引用.如果是这样,我认为那是什么第三个参数为removeRec要.每次替换当前节点时都需要对父项的引用,因此您可以根据需要设置父左侧或右侧子树.
| 归档时间: |
|
| 查看次数: |
4699 次 |
| 最近记录: |