Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating a Binary search tree using recursion , but there is this one thing I am not getting my head around. I have given my Insertion code below, What I am not getting is , inside the insert() method below , why do we have to use root==insertNode(root,data) ? I mean I know it'll invoke the next method , but why do we have to use the "root=" ? I tried removing it which gives me error.
Please explain.

Java
public void insert(int data){
        root=insertNode(root,data);
    }


    private Node insertNode(Node root,int data){

        Node newNode = new Node(data);

        if (root==null){
            root=newNode;
            return root;
        }

        if (data < root.data){
            root.left=insertNode(root.left,data);
        }else {
            root.right=insertNode(root.right,data);
        }
        return root;
    }


What I have tried:

I tried google but didnt got the required answer
Posted
Updated 2-Sep-20 20:32pm
v3
Comments
Sandeep Mewara 2-Sep-20 9:50am    
which line you are talking of. Your code says single '=' and your question talks about '==' (in context of insert method). Where is the confusion?
[no name] 2-Sep-20 11:35am    
root=insertNode(root,data);
THE 2 LINE

Quote:
why do we have to use root==insertNode(root,data) ?

You have to use
Java
root=insertNode(root,data); // note the single '='
because the parent of newly inserted node needs to be updated with the reference to the newly inserted node itself.
 
Share this answer
 
Quote:
root=insertNode(root,data); //THE 2 LINE

You were right on that part. It's not mandatory to put root = and should be okay to just call insert here and ignore the return. Its on you what you want to do with the returned value from a method - use it or ignore.

In your above code, I couldn't locate root defined. Hope that is not related to your issue.


See, I tried the same and it works as expected:
Java
public class JavaFiddle
{
    public static void main(String[] args)
    {
    
      insert(10);
      System.out.println("HelloWorld!");
    }
    
    public static void insert(int data){
        Node root=new Node(1);
        //root=insertNode(root,data);
        insertNode(root,data);
    }
    
    
    private static Node insertNode(Node root,int data){
    
        Node newNode = new Node(data);
    
        if (root==null){
            root=newNode;
            return root;
        }
    
        if (data < root.data){
            root.left=insertNode(root.left,data);
        }else {
            root.right=insertNode(root.right,data);
        }
        return root;
    }
    
    public static class Node
    {
        Node left;
        int data;
        Node right;
        
        Node(int data)
        {
            this.data = data;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900