1
votes

A problem I'm solving is asking to insert a node inside a binary search tree, then return the root of the entire binary tree at the end.

The issue I seem to be having is saving the inserted node inside the original tree given it's root. My code is below:

static Node Insert(Node root,int value) {
    insertAux(root, value);
    return root;
}

static void insertAux(Node root, int value) {
    if (root == null) {
        root = new Node();
        root.data = value;
        root.left = root.right = null;
    } else {
        if (value > root.data) {
            insertAux(root.right, value);
        } else {
            insertAux(root.left, value);
        }
    }
}

When I test this with the following tree:

    4
   / \
  2   7
 / \
1   3

it should result in:

      4
   /    \
  2      7
 / \    /
1   3  6

I have tested this and my insertAux function does in fact assign root to a new Node() and the data for that node to the value 6 when it hits the null case at the end of the function. I have also tested and made sure that my Insert function returns the original root at the end of the entire call. However when I try to see if insertAux has assigned root.right.left to a new Node() and its data to 6 inside of the Insert function, I get a null pointer exception where root.right.left is null. Why is this, is my understanding of pointers or node assignment for Java wrong?

2
Java is pass by value. Initializing the root variable in insertAux won't have any impact on the root variable in Insert. Also, please respect the Java naming conventions. - JB Nizet
what did opmt do wrong java naming convention wise, is it the capital i in Insert? I looked at this link for java naming conventions oracle.com/technetwork/java/codeconventions-135099.html - user2512888
The reason why I had the i capitalized in Insert is because the problem that I got this from(HackerRank) named it with the capital i, otherwise their compiler wouldn't detect the function - OpMt

2 Answers

2
votes
static Node insertAux(Node root, int value) {
if (root == null) {
    root = new Node();
    root.data = value;
    root.left = root.right = null;
} else {
    if (value > root.data) {
        root.right = insertAux(root.right, value);
    } else {
        root.left = insertAux(root.left, value);
    }
}

return root;
}

Your insertAux() method should be like this.

First of all it should have a return type as Node.

What your doing wrong here is that you are not creating a link between the parent node and child node. So in your case, a new Node with data 6 is created but it is actually never assigned to its parent.

So you need to assign the newly created node by returning it and assigning it to parent.In your method here

if (value > root.data) {
        root.right = insertAux(root.right, value);
    } else {
        root.left = insertAux(root.left, value);
    }
1
votes

Java pass parameters by value (i.e not by reference), internal assignment to root is lost after return from function. Try return in different way, maybe

static Node insertAux(Node root, int value) {