首页 文章

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

提问于
浏览
0

我正在制作BST(二进制搜索树) . 我想帮助我的删除方法,似乎当我将该节点设置为null时,当我用我的显示方法(preorder,inorder,postorder)显示它时,它仍会出现 .

这是我的节点链表类

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);
}

谢谢您的帮助!

1 回答

  • 0

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

    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
        }
    }
    

相关问题