简体   繁体   English

二进制搜索树节点删除不删除替换Java

[英]Binary Search Tree Node Removal not removing replacement Java

I am trying to remove nodes from a Binary Search Tree. 我试图从二进制搜索树中删除节点。 I can successfully remove any other node on the tree except for one particular case. 除了一个特定的情况,我可以成功删除树上的任何其他节点。 If the targeted node has 2 children, and the left child has a right subtree, I can locate the correct replacement Node and switch the value to the targeted Node, but then the replacement node is never deleted. 如果目标节点有2个子节点,并且左子节点具有右子树,我可以找到正确的替换节点并将值切换到目标节点,但是永远不会删除替换节点。

二叉搜索树

Looking at the picture above, if I try to delete 17, the program will correctly navigate to 13 and replace 17 with 13, but it will not then delete the original 13 as it is supposed to. 看看上面的图片,如果我尝试删除17,程序将正确导航到13并用13替换17,但它不会删除原来的13,因为它应该。

I have attached my remove methods and those referenced within. 我附上了我的删除方法和其中引用的方法。

public Node root;    

    public void delete(int value){

    Node node = new Node<>(value);
    Node temp;

    if(root == null) {
        System.out.println("The tree is already empty!");           //tree is empty
        return;     
    }

    if (root.value == node.value) {                                 //Root is target value
        temp = node.left;

        if(temp.right == null){
            node.value = temp.value;
            temp = null;
        }
        else{
            while(temp.right != null){
                temp = temp.right;
            }
            node.value = temp.value;
            temp = null;
        }
        return;
    }
    deleteRec(root, node);
}

private void deleteRec(Node lastRoot, Node node){

    Node temp;

    if (lastRoot.value >= node.value){

        if (lastRoot.left.value == node.value){
            node = lastRoot.left;
            if(node.left == null && node.right == null){        //No children
                node = null;
                lastRoot.left = null;
            }
            else if(node.left == null && node.right != null){   //Right Child
                lastRoot.left = node.right;
                node = null;
                lastRoot.left = null;
            }
            else if(node.left != null && node.right == null){   //Left Child
                lastRoot.left = node.left;
                node = null;
            }
            else{                                               //Two Children

                if(node.left.right == null){
                    node.value = node.left.value;
                    node.left = node.left.left;
                    node.left = null;
                }
                else{
                    node = findReplacement(node.left);
                    lastRoot.left.value = node.value;
                    node.left = null;
                }
            }
        }
        else{
            deleteRec(lastRoot.left, node);
        }
    }
    else{
        if (lastRoot.right.value == node.value){
            node = lastRoot.right;
            if(node.left == null && node.right == null){        //No Children
                node = null;
                lastRoot.right = null;
            }
            else if(node.left == null && node.right != null){   //Right Child
                lastRoot.left = node.right;
                node = null;
                lastRoot.right = null;
            }
            else if(node.left != null && node.right == null){   //Left Child
                lastRoot.right = node.left;
                node = null;
            }
            else{                                               //Two Children

                if(node.left.right == null){
                    node.value = node.left.value;
                    node.left = node.left.left;
                    node.left = null;
                }
                else{
                    node = findReplacement(node.left);
                    lastRoot.left.value = node.value;
                    node.left = null;
                }
            }
        }
        else{
            deleteRec(lastRoot.right, node);
        }
    }
}

private Node findReplacement(Node node) {

    while(node.right != null){
        node = node.right;
    }        
    return node;
}

And here is my Node class: 这是我的Node类:

 public class Node<T> {
    public int value;
    public Node left;
    public Node right;
    public Node parent;

    public Node(int value) {
        this.value = value;
    }
}

Consider this part of your code: 考虑这部分代码:

Node rep = findReplacement(node.left);
node.value = rep.value;
rep = null;

You're finding the replacement, and making rep point to it. 你找到了替代品,并让rep指向它。 Then, essentially what you're doing is making rep point to null . 然后,基本上你正在做的是使rep指向null This doesn't remove the node! 这不会删除节点! The parent is still pointing to it! 父母仍然指着它!

There are several places in your code where you're doing something along these lines. 您的代码中有几个地方沿着这些方向做某事。 The way you're expected to remove nodes from a tree in this Java implementation is by changing what parents point to. 在Java实现中,您希望从树中删除节点的方式是更改父项指向的内容。 The garbage collector takes care of the other details. 垃圾收集器负责其他细节。 I hope addressing this issue helps you resolve your problem! 我希望解决这个问题可以帮助您解决问题!

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

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