简体   繁体   中英

Does not remove last item in Boolean remove(object o) method

This removes almost all of what is supposed to, except for the last item. This is what I get back when I submit it:

Input: [thing, word, stuff, and, both, zoo, yes]

----------Expected size: 0 BST actual number of nodes: 1

Invalid tree after removing thing

Code Below:

@SuppressWarnings("unchecked")
public boolean remove(Object o) {
        Node n = root;
        while (n != null) {
            int comp = n.value.compareTo(o);
            if (comp == 0) {
                size--;
                remove(n);
                return true;
            } else if (comp > 0) {
                n = n.left;
            } else {
                n = n.right;
            }
        }
        return false;
    }

    private void remove(Node root) {
        if (root.left == null && root.right == null) {
            if (root.parent == null) {
                root = null;
            } else {
                if (root.parent.left == root) {
                    root.parent.left = null;
                } else {
                    root.parent.right = null;
                }
            }
        } else if (root.left == null || root.right == null) {
            Node child = root.left; 
            if (root.left == null) {
                child = root.right;
            }
            if (root.parent == null) {   
                root = child;
            } else if (root.parent.left == root) {
                root.parent.left = child;
            } else {
                root.parent.right = child;
            }
            child.parent = root.parent;
        } else {                          
            Node successor = root.right;
            if (successor.left == null) {
                root.value = successor.value;
                root.right = successor.right;
                if (successor.right != null) {
                    successor.right.parent = root;
                }
            } else {
                while (successor.left != null) {
                    successor = successor.left;
                }
                root.value = successor.value;
                successor.parent.left = successor.right;
                if (successor.right != null) {
                    successor.right.parent = successor.parent;
                }
            }
        }
    }

Removal of a node in a Binary-search-tree consists of the following steps:

  1. Find the node

You need to make sure that you have a function which is used for searching in order to find the node to be removed.

  1. Handle the node's subtree

If the node has less than two children, then the subtree can be trivially changed. If there is a child, then the current node will be replaced by its child. Otherwise, if there are two children of the node to be removed, then you will just need to replace the node to be removed with the rightmost node of the left subtree or the leftmost node of the right subtree of the element to be removed.

  1. Ensure that if you have replaced your current node with something else, then the other node will not exist as a duplicate.

In order to achieve this you will need methods like: - search - find leftmost/rightmost node of subtree - remove

Your current code is over-complicated. I would rewrite it using atomic methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM