简体   繁体   English

不删除布尔 remove(object o) 方法中的最后一项

[英]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 ----------预期大小:0 BST 实际节点数: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.我会使用原子方法重写它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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