从BinaryTree中删除一片叶子

时间:2016-11-21 03:38:32

标签: java binary-tree

下面的代码将移除一个有两个孩子或一个孩子的树。但是一片叶子(一棵零孩子的树)我似乎无法破解。我的BSTNode<E>还包含.parent,用于标识其父级。我不确定这是否有助于此实施。

@Override
public E delete(E target) {
    return delete(this.root, target);
}

private E delete(BSTNode<E> localRoot, E target) {
    // ... Trying to delete an item that's not in the tree?
    if (localRoot == null) {
        return null;
    }

    int compareResult = c.compare(target, localRoot.data);

    // Traverse the tree...
    if (compareResult < 0) {
        return delete(localRoot.left, target);
    } else if (compareResult > 0) {
        return delete(localRoot.right, target);
    } else {
        // Found the item! Oh boy here we go!
        E retVal = localRoot.data;

        /*
         * Both left and right exist under the targeted node.
         * Find the largest child under the right side of the node.
         * Set the largest data to the 'deleted' node, then delete that node.
         */
        if (localRoot.left != null && localRoot.right != null) {
            /*
             * Two children, find the smallest and assign it.
             */
            localRoot.data = localRoot.right.data;
            localRoot.right = findLargestChild(localRoot.right);
        } else if (localRoot.left != null) {
            localRoot.data = localRoot.left.data;
            localRoot.left = findSmallestChild(localRoot.left);
        } else if (localRoot.right != null) {
            localRoot.data = localRoot.right.data;
            localRoot.right = findLargestChild(localRoot.right);
        } else {
            /*
             * TODO:
             * Remove a leaf.
             */
            System.out.println("Removing leaf..." + localRoot.data.toString());
            localRoot = null;
        }

        return retVal;
    }
}

1 个答案:

答案 0 :(得分:0)

使用localRoot.parent我能够删除null父母的孩子(localRoot)。这感觉倒退了,但它通过了JUnit Tests ......

private E delete(BSTNode<E> localRoot, E target) {
    // ... Trying to delete an item that's not in the tree?
    if (localRoot == null) {
        return null;
    }

    int compareResult = c.compare(target, localRoot.data);

    // Traverse the tree...
    if (compareResult < 0) {
        return delete(localRoot.left, target);
    } else if (compareResult > 0) {
        return delete(localRoot.right, target);
    } else {
        // Found the item! Oh boy here we go!
        E retVal = localRoot.data;

        if (localRoot.right != null) {
            /*
             * Assign the largest value to the tree.
             */
            localRoot.data = localRoot.right.data;
            localRoot.right = findLargestChild(localRoot.right);
        } else if (localRoot.left != null) {
            /*
             * Since the largest value didn't exist, assign the smallest.
             */
            localRoot.data = localRoot.left.data;
            localRoot.left = findSmallestChild(localRoot.left);
        } else {
            /*
             * Remove a leaf.
             */
            System.out.println("Removing leaf or left..." + localRoot.data.toString());
            if (c.compare(target, localRoot.parent.left.data) == 0) {
                localRoot.parent.left = null;
            } else {
                localRoot.parent.right = null;
            }
        }

        return retVal;
    }
}