3

I have found this Java recursive program for adding an element into a binary search tree:

public void add(int e){
    root=add(root, e);
}
public Node add(Node n, int e){
    if (n==null){
        n=new Node(e);
    } else {
        if (e<n.elem){
            n.left=add(n.left,e);
        } else{
            n.right=add(n.right,e);
        }
    }
    return n;
}

What I do not understand is why it has to return n at the end and then assign it to root again.

2
  • 1
    The return n statement at the end is called after the recursive calls to the left and right have finished. You should review a tutorial (or textbook) covering recursion. Commented Jun 17, 2016 at 1:25
  • 1
    The best way to understand this is to do it with pencil and paper and start with an empty tree. Then trace through the code and add the values 2, 4, 1, and 5. You'll notice that root refers to the actual tree root only on the first call. After that, as you get more than one level down, root refers to the root of the current subtree. Commented Jun 17, 2016 at 1:26

2 Answers 2

4

The reason for the assignment is that Java has only one method of passing parameters - by value.

A reference to root is passed to add method by value. However, add needs to modify a node passed to it as a root: for example, when you add the first node to the tree, the value of root is null, but it needs to become non-null after adding a node.

An idiom to work around this limitation is to make a method that returns the modified value, and assign it back to the parameter. That is what your add method is doing here

root=add(root,e);

and here

n.left = add(n.left, e);
n.right = add(n.right, e);
Sign up to request clarification or add additional context in comments.

2 Comments

Is this version of the algorithm considered better or would checking for null and acting accordingly before going down the next layer of recursion be equally as valid and good?
@HopefullyHelpful You could do without assignment, but then you'd need to null-check in three places - the top level void add, and both branches of the conditional. This implementation, on the other hand, has a single null check, so it is shorter.
0

If you want to understand the recursion, you need to look at the conditions that end It: if (n==null) n=new Node(e);.

This means that the last call will be when Node n is null. When will that be? In one of the leafs. Only then will there be created another object which is added to the tree.

After that, it's only the logic of the return n: After the last call, n is: new Node(e). Who will the new Node be assigned to? Some (former) leaf's n.left or n.right. The same logic precipitates up the tree until the first call to the method add is reached in the stack. That will return the full updated tree - the final product you wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.