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;
}
}
node->left/right = insertwould not work properly ifinsertdidn't return the node.