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