0

I would like to know why we have to return the pointer to root node in insert function of BST. Wouldn't the BST be updated even if we don't return the pointer to root node, since we are updating the data in the memory, to which the pointer is mapped? The following code

struct node* insert(struct node* node, int data) 
{
    /* 1. If the tree is empty, return a new, single node */
    if (node == NULL)    
        return(newNode(data));

    /* 2. Otherwise, recurse down the tree */
    else
    {
        if (data <= node->data)
            node->left  = insert(node->left, data);
        else
            node->right = insert(node->right, data);

        /* return the (unchanged) node pointer */
        return node;
    }
}
4
  • 5
    node->left/right = insert would not work properly if insert didn't return the node. Commented Aug 2, 2014 at 19:58
  • 3
    Its part of the chosen algorithm. That node pointer has to come from somewhere. You don't need to do any of this if you use a different algorithm, such as using a pointer-to-pointer and no recursion. Commented Aug 2, 2014 at 20:27
  • Does this code do the same, except that this will not return a pointer?? struct node* insert(struct node* node, int data) { /* 1. If the tree is empty, return a new, single node / if (node == NULL) root =newNode(data); / 2. Otherwise, recurse down the tree */ else { if (data <= node->data) insert(node->left, data); else insert(node->right, data); } } Commented Aug 3, 2014 at 18:50
  • Sorry for the format, using from mobile Commented Aug 3, 2014 at 18:52

3 Answers 3

2

In the case where the caller passed in a empty tree (null pointer), this function must return a pointer to the tree so that the caller now has a non-empty tree. In the case where the tree is non-empty, the function recurses and returns a new sub-tree at some point. You could write a version of this code that returned NULL (or some other value) if the root node did not change, but that would make the code more complicated. This is the simplest way to do it.

Sign up to request clarification or add additional context in comments.

Comments

0

In this implementation, the function must return a node or else the recursion will break. Your conditional says to reassign either the node->left or node->right pointer to the result of a recursive call. So it is eventually going to expect a node value to be returned.

If you took the return statement out, I do not think this code would compile.

Comments

0

As others have suggested, it is just a part of algorithm, otherwise node->left = insert(node->left, data); would not work.

Here is an algorithm (in C/C++) that does not return anything:

void Insert(struct node** node, int key)
{
    if(*node == NULL) *node = newNode(key);
    else insert(*node, key);
}

void insert(struct node* node, int key)
{
    if (key < node->key)
    {
        if(node->left == NULL) node->left = newNode(key);
        else insert(node->left, key);
    }

    else if (key > node->key)
    {
        if(node->right == NULL) node->right = newNode(key);
        insert(node->right, key);
    }
}

Here, newNode(key) is a function that creates a new node and set its key value as supplied in the argument.

Note that using only theinsert function given at the bottom would do the job, except for when node is NULL (that is, the tree is empty). That is why I have used separate Insert function to handle this case.

You would insert elements as:

Insert(&root,50);

I hope this clarifies your doubt.

Comments

Your Answer

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