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

时间:2015-02-12 01:31:03

标签: java binary-search-tree nodes

我正在制作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 :(得分: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
    }
}
相关问题