what’s the difference in
[ insert_node(&(tmp->left),value);] VS [ tmp=tmp->right; insert_node(&(tmp),value);]
void insert_node(struct btree **bt,int value)
{
btree *tmp= *bt, *r ;
if(*bt==NULL)// first node
{
(*bt)=(struct btree *)malloc(sizeof(btree));
(*bt)->data=value;
(*bt)->left=NULL;
(*bt)->right=NULL;
}
else
{
if(value > tmp->data)
insert_node(&(tmp->right),value);
else
insert_node(&(tmp->left),value);
}
#if 0 //start
OR /** COMMENT START
earlier I had below piece of code but not working
can any please explain what's the difference in
insert_node(&(tmp->left),value); VS [ tmp=tmp->right; insert_node(&(tmp),value);]
COMMENT END **/
else
{
if(value > tmp->data)
tmp=tmp->right;
else
tmp=tmp->left ;
insert_node(&tmp,value);
}
#endif //end
}
In the non-working case, you are giving it the address of your
tmpvariable, and therefore updating that pointer, which is on your stack and not going to be used for anything.In the working case, you are giving the address of the actual pointer in your bt node which is what you want.