Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinarySearchTree on deleteInternal and findMin has bug #76

Open
ac-tech-madcoz opened this issue Aug 11, 2015 · 0 comments
Open

BinarySearchTree on deleteInternal and findMin has bug #76

ac-tech-madcoz opened this issue Aug 11, 2015 · 0 comments
Assignees
Labels

Comments

@ac-tech-madcoz
Copy link

The following function has bug, which miss detecting when the node's left subtree has right subtree, the node's left subtree should be set to the node's left subtree's right subtree instead of setting nil.

Another problem is that when the deleted node's right subtree has no left subtree, then although the node's subtree is marked is deleted but it was not deleted when return, so after return the code should also check if the node's right subtree is marked deleted or not, if marked then set the node's right subtree to null to avoid missing deleting the node's right subtree.

The corrected function as follows:

    private void deleteInternal(BinarySearchTreeNode<K> node) {
        size--;
        // No children exist, mark this node as deleted
        if (!(node.leftExists() || node.rightExists())) {
            node.setDeleted(true);
            return;
        }

        // Only the left child exists, move the left node to this position
        if (node.leftExists() && !node.rightExists()) {
            node.setKey(node.getLeft().getKey());
            if (node.getLeft().rightExists()) {
                node.setRight(node.getLeft().getRight());
            }
            if (node.getLeft().leftExists()) {
                node.setLeft(node.getLeft().getLeft());
            } else {
                node.setLeft(null);
            }
            return;
        }

        // Only the right child exists, move the right node to this position
        if (node.rightExists() && !node.leftExists()) {
            node.setKey(node.getRight().getKey());
            if (node.getRight().leftExists()) {
                node.setLeft(node.getRight().getLeft());
            }
            if (node.getRight().rightExists()) {
                node.setRight(node.getRight().getRight());
            } else {
                node.setRight(null);
            }
            return;
        }

        // Both children exist, replace this node with with minimum node from
        // the right sub-tree
        K min = findMin(node.getRight());
        if(node.getRight().isDeleted()) {
            node.setRight(null);
        }
        node.setKey(min);
    }
    private K findMin(BinarySearchTreeNode<K> node) {
        if (!node.leftExists()) {
            if(!node.rightExists() {
                node.setDeleted(true);
            }
            return node.getKey();
        }

        K min = findMin(node.getLeft());
        if (node.getLeft().isDeleted()) {
            node.setLeft(null);
        } else {
            node.setLeft(node.getLeft().getRight());
        }
        return min;
    }
@ac-tech-madcoz ac-tech-madcoz changed the title BinarySearchTree on findMin has bug when the deleted node's left subtree has right subtree BinarySearchTree on deleteInternal and findMin has bug Aug 11, 2015
@Tyriar Tyriar self-assigned this Aug 12, 2015
@Tyriar Tyriar added the bug label Aug 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants