Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Search Tree insertion - root remains null

I'm new to Java, I want to create a Binary Search Tree class with insertion and preorder traversal, but when I finish the insertion the root object remains null and the compiler throws NullPointerException during the preorder traversal.

My node class:

class Node {
    int info;
    Node left;
    Node right;

    public Node() {
        info = 0;
        left = null;
        right = null;
    }

    Node(int x) {
        info = x;
        left = null;
        right = null;
    }
}

My Binary Search Tree class:

public class BinarySearchTree {

    private Node root;

    public BinarySearchTree() {
        root = null;
    }

    private void insertPrivate(Node node, int x) {
        if(node == null) {
            node = new Node(x);
        } 
        else {
            if(x < node.info) {
                insertPrivate(node.left, x);
            } 
        else if (x > node.info) {
                insertPrivate(node.right, x);
            }
        }
    }

    public void insert(int x) {
        insertPrivate(root, x);
    }

    private void preorderPrivate(Node node) {
        if(node != null) {
            System.out.println(node.info);
            preorderPrivate(node.left);
            preorderPrivate(node.right);
        }
    }

    public void preorder() {
        preorderPrivate(root);
    }

    public static void main(String[] args) {
        BinarySearchTree t = new BinarySearchTree();
        t.insert(12);
        t.insert(13);
        t.preorder();
    }
}
like image 708
Ambrus Adrián-Zoltán Avatar asked Nov 28 '25 06:11

Ambrus Adrián-Zoltán


2 Answers

The issue is a misunderstanding of Java referencing as seen with this section of code.

  private void insertPrivate(Node node, int x) {
    if(node == null) {
        node = new Node(x);
    } 
    ....

Java references are passed by value into function arguments.

Let me give you an example to clarify.

Node root = new Node(x);
// root == Node(x);

doSomething(root);
// Pass Node(x) into function;

void doSomething(Node node) {
    // root == Node(x);
    // node == Node(x); 

    node = new Node(y); // This updates node but not root
    // root == Node(x);
    // node == Node(y);

}    

You are going to have to restructure your program. One way would be to have insertPrivate return a Node and assign that value to root. It is not the most efficient but it will work.

public void insert(int x) {
    root = insertPrivate(root, x);
}

private Node insertPrivate(Node node, int x) {
    if(node == null) {
        node = new Node(x);
    } 
    else {
        if(x < node.info) {
            node.left = insertPrivate(node.left, x);
        } 
    else if (x > node.info) {
            node.right = insertPrivate(node.right, x);
        }
    }
    return node;
}
like image 77
cyroxis Avatar answered Nov 29 '25 18:11

cyroxis


Change insert(int x) like this:

public void insert(int x) {
    if (root == nul)
        root = new Node(x);
    else
        insertPrivate(root, x);
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!