Creating the node
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left = null;
right = null;
}
}
Creating the tree
//create tree
class Tree{
Node root = null;
void insert(Node temp, int data){
Node NewNode = new Node(data);
if(temp == null){
temp = NewNode;
}
else{
if(data<temp.data){
insert(temp.left,data);
}
else{
insert(temp.right,data);
}
}
}
}
Main function
public class test{
public static void main(String[] args) {
Tree t = new Tree();
t.insert(t.root,3);
// t.insert(t.root,1);
System.out.println(t.root.data);
}
}
Upon running I get t.root is null. I don't understand why. Need some help. I am passing t.root as temp, so when temp gets modified doesn't that mean t.root does too?