从二叉搜索树中递归删除
这是作业; 请不要只给我代码
我有两种方法: remove(T data)
和removeRec(Node node, T data)
。
在当前状态下,似乎我的代码只删除了BST的root
。
@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 dummy = new BSTNode(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 removeRec(BSTNode node, T data, BSTNode temp) { 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 temp
。 我不明白这有什么帮助,或者如何利用它。 我没有看到使用第三个参数有何帮助; 我在网上找不到你为什么这么做的事。
当您尝试从二进制搜索树中删除data
时,有三种主要可能性:
-
data
小于当前节点值:在左子NoSuchElementException
调用remove或如果为null则抛出NoSuchElementException
。 -
data
大于当前节点值:在右子NoSuchElementException
调用remove,如果为null,则抛出NoSuchElementException
。 -
data
等于当前节点值。
1和2非常简单,但3还有四个案例需要考虑:
3.1。 当前节点是叶子 :左右子树都为空。 只需将其父节点中当前节点的引用替换为null即可。
3.2。 当前节点只有左子节点 :您需要使当前节点的父节点指向左子树,从而删除当前点。 为此,您可以实现一个函数,该函数将检查当前点是否在父节点的左子树或右子树上,并相应地替换它。 调用它看起来像这样:
replaceNodeInParent(node, node.getLeft(), parent);
3.3。 当前节点只有正确的子节点 :类似于3.4,但使用getRight()
而不是getLeft()
。
3.4。 当前节点同时具有左子节点和右子节点 :您应该保持BST的属性,即左侧的所有节点都小于当前节点,右侧的所有节点都大于当前节点。 为此,您应找到右侧的最小值,将其复制到当前节点,然后从右侧子树中删除它。 像这样的东西:
BSTNode successor = findMin(node.getRight()); node.setData(successor.getData()); removeRec(node.getRight(), successor.getData(), node);
看起来您的BSTNode
不包含对父节点的引用。 如果是这样,我相信这是removeRec
的第三个参数应该是什么。 每次替换当前节点时都需要对父项的引用,因此您可以根据需要设置父左侧或右侧子树。
有关进一步阅读,您可以查看有关维基百科二进制搜索树的文章 。