简体   繁体   中英

Binary search tree Tree Implementation, Program terminates in eclipse. simple insert and display operation

This is a class Trees simply to implement the binary search tree, can't understand whats wrong with this.. The class Node is defined separately whose default constructor I have called from here:

public class Trees {

    static Node root = null;

    static void insert(int data, Node r)
    {
        if(r==null)
        {
            r = new Node();
            r.data = data;
            r.left = null;
            r.right=null;
        //  System.out.println(root.data);
        }

        else
        {   
            Node current;
            current = r;

            if(data >=current.data)
                {
                insert(data,current.right);
                }

            else if(data < current.data)
            {
            insert(data,current.left);
            }
        }
    }


    static void display(Node r)
    {

       if(r!=null)
       {

        display(r.left);
        display(r.right);
        System.out.print(r.data +"  ");

       }

    }

    public static void main(String[] args) {

        insert(2, root);
        insert(6, root);
        insert(1, root);
        insert(5, root);

        display(root);

    }

}

What could be wrong with this?

public class Trees {

static Node root = null;

static Node insert(int data, Node r)
{
    if(r==null)
    {
        r = new Node();
        r.data = data;
        r.left = null;
        r.right=null;
        return r;
    //  System.out.println(root.data);
    }

    else
    {   

    Node current=r;
        if(data >=current.data)
            {
          current.right= insert(data,current.right);
            }

        else if(data < current.data)
        {
        current.left=insert(data,current.left);
        }
    }
    return r;

}


static void display(Node r)
{

   if(r!=null)
   {

    display(r.left);
    display(r.right);
    System.out.print(r.data +"  ");

   }

}

public static void main(String[] args) {

    root=insert(2, root);
    root=insert(6, root);
    root=insert(1, root);
    root=insert(5, root);

    display(root);

}

}

class Node
{
     public int data;
     public Node left;
     public Node right;
}

This code is working, If you want to use recursion then you have to assign value of the reference to the reference variable as well. Try this code , this code is working and printing output as well.

代码输出

          I hope I helped you.

The problem is that in your code root is never assigned to. That is why it works in the code which returns r and assigns to root in the other answer.

static void insert(int data, Node r)
{
    if(r==null)
    {
        r = new Node();
        r.data = data;
        r.left = null;
        r.right=null;
    //  System.out.println(root.data);
    }
    ....
 }

That if block doesn't actually do anything useful. "r" is not passed into the function by reference so the assignment to "r" does not propagate back to the root variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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