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

Upvotes: 3

Views: 1511

Answers (4)

cyroxis
cyroxis

Reputation: 3711

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

Upvotes: 3

G&#225;l B&#233;ni
G&#225;l B&#233;ni

Reputation: 1

You should somehow return the newly created node and assign it to the left or right node of the parent. Try this:

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

And your public insert function should be:

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

Also preorderPrivate needs a null-checking:

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

Upvotes: 0

user4910279
user4910279

Reputation:

Change insert(int x) like this:

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

Upvotes: 1

hpopiolkiewicz
hpopiolkiewicz

Reputation: 3538

Try : Node left = new Node(); Node right = new Node();

Upvotes: 0

Related Questions