0

new node not assigning to a tree when we make it by passing into method but working when made directly, not passing into method.

I have made Node class and MYBinarySearchTree class. when i pass the root node and data in insertIt method then it is giving nullpointerexception in runtime.

class Node{
    int data;
    Node left, right;
    Node(int d){
        data = d;
        left = right  = null;
    }
}
class MyBinarySearchTree{
    Node root ;

    MyBinarySearchTree(){
        root = null;
    }

    void insert(int data){
        //root = new Node(data);
        insertIt(root,data);

    }

    static void insertIt(Node node, int data){
        node = new Node(data);

    }

}
class BinarySearchTree{
    public static void main(String[] args) {
        MyBinarySearchTree bst = new MyBinarySearchTree();
        bst.insert(45);
        System.out.println(bst.root.data);

    }
}

this above code is not working.



    void insert(int data){
        root = new Node(data);
        //insertIt(root,data);

    }

    static void insertIt(Node node, int data){
        node = new Node(data);

    }

this above code is working

i don't understand what is difference between the two, because in java objects are pass by reference, so it should give same result.

Abhishek Patil
  • 502
  • 1
  • 4
  • 12
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – luk2302 May 24 '19 at 14:11
  • You change the *reference*, not the object itself. If you pass `null` as `node` and then write `node = ...` the one who passed `null` will not see the newly created value. You should spend more team understanding https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – luk2302 May 24 '19 at 14:12
  • There is a major difference between your code and the code in the link: `insertIt` / `insertRec` is not `void`. – luk2302 May 24 '19 at 14:15
  • [link](https://www.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/) i am trying to implement this code – Abhishek Patil May 24 '19 at 14:15
  • i have not totally implementted insertRec because that was also not working due to the same above problem. that is also giving null pointer exception – Abhishek Patil May 24 '19 at 14:17

1 Answers1

1

This is not really true :

because in java objects are pass by reference

Java is pass-by-value ONLY. When node is passed to the insertIt() method, a copy of value of node (a reference) is passed. The method insertIt() creates another object new Node(data) and it has a different reference. It is the variable node that changes its reference(to new Node(data)), not the object itself.

Here, your function insertIt() should be like that :

Node insertIt(int data){
    return new Node(data);
}

And in your insert() function :

void insert(int data){
    this.root = insertIt(data);
}

Furthermore, I don't understand what is the purpose of the insertIt() function, except creating a new Node.

criviere
  • 11
  • 1