void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
This function uses a pointer to pointer node ** tree
to represent the head of the binary tree. I came across the same method on many tutorials and websites. I wanted to know why everyone insists on using pointer to pointer and why not just 'node * tree'. I presume even pointer to head of tree would do the job just fine, and using just pointer to head of the tree the recursive calls would be as follows (please correct me if i am wrong):
insert(tree->left, val); // assuming definisiton as void insert(node *tree, int val);
or
insert(tree->right, val); // assuming definisiton as void insert(node *tree, int val);
Please correct me if my understanding is wrong. I am a newbie. Thanks.