繁体   English   中英

为什么我的节点在设置为null时仍然出现?

[英]Why does my node still appear when I set it to null?

我正在制作一个BST(二进制搜索树)。 我想对我的delete方法有所帮助,似乎当我将该节点设置为null时,当我使用显示方法(预排序,顺序排序,后排序)显示它时,它仍然会出现。

这是我的节点链表类

public class Node<T>
{
    public int value;
    public Node leftValue;
    public Node rightValue;
    public Node(int _value)
    {
        value=_value;
    }
}

这是我在BST课程中的删除方法

public void findDelete(Node root, int value)
    {
        if (root== null)
        {
            System.out.println(" Not founded ");
        }
        else if (value < root.value)
        {
            findDelete(root.leftValue,value);
        }
        else if (value > root.value)
        {
            findDelete(root.rightValue,value);
        }
        else if (value == root.value)
        {
            //// checks if have children 
            if (root.leftValue==null&&root.rightValue==null)
            {
                  root = null; 
            }
            //// one
            else if ( root.leftValue==null)
            {
                  root.value=root.rightValue.value;
            }
            else if ( root.rightValue==null)
            {
                root.value=root.leftValue.value;
            }
            //// two
             else if ( root.leftValue!=null && root.rightValue!=null)
            {
                root.value=findMin(root.rightValue);
                findDelete(root.rightValue,value);
            }
        }
        else 
        {
            System.out.println(" Not founded ");
        }
    }

如果节点有子节点(叶子),我的删除方法也会尝试处理分配新的后继节点。 如果我做得正确,是否还可以得到一些反馈? 它试图处理3个案件。 病例1无子女,病例2 1儿童,病例3 2儿童。

我想可能是我的delete方法中的行通过将它设置为null(如果它没有子节点)来删除它。

if (root.leftValue==null&&root.rightValue==null)
          {
                root = null; 
          }

它将它设置为null,但root.value仍然有一个int值,当我用display方法显示它时,它仍然存在。 至少我认为这就是问题所在。 我需要一些帮助和反馈,谢谢!

我的一种显示方法

public void printPre(Node root)
{
    if (root==null)
    {
        return;
    }
    System.out.print(root.value + ", ");
    printPre(root.leftValue);
    printPre(root.rightValue);
}

谢谢您的帮助!

当您调用一个方法并将变量用作参数时,变量不会传递给方法-只会传递给它们的值。 这就是为什么:

class Test1 {
    static void addOne(int i) {
        i++;
    }
    public static void main(String[] args) {
        int x = 5;
        addOne(x);
        System.out.println(x);
    }
}

打印5,而不是6。要调用addOne ,分配参数i空间,将值5从x复制到i ,方法体运行(将i设置为6),然后方法的局部变量在返回时被销毁。 x未被调用修改。

同样,这个:

class Test2 {
    static void delete(Node root) {
        root = null;
    }
    public static void main(String[] args) {
        Node n = (something); // not actually (something), but something that returns a Node
        delete(n.leftValue);
    }
}

不修改n.leftValue 和以前一样,分配局部变量root ,将值(对Node对象的引用)从n.leftValue复制到root ,方法主体执行(将root设置为null),然后在以下情况下销毁方法的局部变量它返回。 n.leftValue不被修改,只有root

一种选择是仅返回新节点,因为您的函数当前不返回任何内容。 在上面的简化示例中:

class Test3 {
    // returns the new node
    static Node delete(Node root) {
        // in this example it always deletes the node by replacing it with null;
        // obviously your actual code is more complicated than this
        return null;
    }
    public static void main(String[] args) {
        Node n = (something); // not actually (something), but something that returns a Node
        n.leftValue = delete(n.leftValue); // <-- note the change
    }
}

暂无
暂无

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

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