从二叉搜索树中递归删除

时间:2015-02-08 18:13:35

标签: java algorithm recursion binary-search-tree

这是作业;请不要只给我代码

我有两种方法:remove(T data)removeRec(Node<T> 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<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。我不明白这有什么帮助,或者如何利用它。我没有看到使用第三个参数有何帮助;我在网上找不到你为什么要这样做的事。

1 个答案:

答案 0 :(得分:5)

当您尝试从二进制搜索树中删除data时,有三种主要可能性:

  1. data小于当前节点值:在左子树上调用remove,如果为null,则抛出NoSuchElementException
  2. data大于当前节点值:在右子树上调用remove,如果为null,则抛出NoSuchElementException
  3. data等于当前节点值。
  4. 1和2非常简单,但3还有四个需要考虑的案例:

    3.1。 当前节点是叶子:左右子树都为空。只需将其中当前节点的引用替换为null。

    3.2。 当前节点只有左子节目:您需要使当前节点的指向左子树,从而删除当前点。为此,您可以实现一个函数,该函数将检查当前点是否位于的左侧或右侧子树上,并相应地替换它。调用它看起来像这样:

    replaceNodeInParent(node, node.getLeft(), parent);
    

    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);
    

    您的BSTNode似乎没有对节点的引用。如果是这样,我相信removeRec第三个论点应该是什么。每次替换当前节点时都需要对父项的引用,因此您可以根据需要设置父左侧或右侧子树。

    如需进一步阅读,您可以从维基百科查看article on Binary Search Trees