简体   繁体   English

二叉树:为什么一个插入方法起作用而另一个不起作用

[英]Binary Tree: Why Does One Insert Method Work and the Other Not

I just learned about binary trees and I tried to create an insert method. 我刚刚了解了二叉树,并尝试创建一个insert方法。 My first method did not work and I did a bit of tweaking. 我的第一种方法不起作用,我做了一些调整。 It now works but I do not understand why the previous method failed. 现在可以使用,但是我不明白为什么以前的方法失败了。

The method that does not work is: 不起作用的方法是:

if(root == null)
    {
        root = new Node(data);
    }
    else if(data < root.getData())
    {
        insertNode(root.getLeft(), data);
    }
    else
    {
        insertNode(root.getRight(), data);
    }

The method that does work is: 起作用的方法是:

    if(data < root.getData())
    {
         if(root.getLeft() == null)
         {
             root.left = new Node(data);
         }
         else
         {
             insertNode(root.getLeft(), data);
         }
     }

     else
     {
         if(root.getRight() == null)
         {
             root.right = new Node(data);
         }
         else
         {
            insertNode(root.getRight(), data);
         }
     }

Any explanations as to why this is the case? 为什么会这样呢? Because the way I see it, root should be equal to root.left, so setting root to a new Node should be the same as setting root.left/right to a new Node. 因为从我的角度来看,root应该等于root.left,因此将root设置为新的节点应该与将root.left / right设置为新的节点相同。

In your first method, you would give null into your insertNode method, but no reference pointer. 在第一个方法中,将null插入insertNode方法中,但不提供引用指针。 Therefore you set root = new Node() in the insertNode method, but the parent node does not know any of this, it still points to null. 因此,您可以在insertNode方法中设置root = new Node(),但是父节点不知道其中任何一个,它仍然指向null。

Since this is some very basic Java understanding, I recommend reading some articles about "java parameter passing" eg http://javadude.com/articles/passbyvalue.htm 由于这是一些非常基本的Java理解,因此我建议阅读一些有关“ java参数传递”的文章,例如http://javadude.com/articles/passbyvalue.htm

Assuming that you call the method recursively, insertNode(root, data) , you have to be sure that root is not null , which means executing root = new Node(data); 假设您递归地调用方法insertNode(root, data) ,则必须确保root不为null ,这意味着执行root = new Node(data); insertNode(root, data) creates an object whose visibility is limited to insertNode method. 创建一个对象,其可见性仅限于insertNode方法。

If not, you can rewrite insertNode(data) to be non-recursive and create the root inside it if it is null . 如果不是,则可以将insertNode(data)重写为非递归的,如果它为null ,则在其中创建root

public void insert(int data) {
    if(root == null){
        root = new Node(data);
    }
    else {
        Node current = root;
        Node previous;
        String from;
        while(current != null) {
            previous = current;
            if(data < current.getData()) {
                current = current.left();
                from = "left";
            }
            else {
                current = current.right();
                from = "right";
            }
        }
        current = new Node(data);
        if(from.equals("left")) {
            previous.left() = current;
        } else {
            previous.right() = current;
        }
    }
}

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

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