Problem in inorder traversal in binary search tree ...?


void inorder(struct node *r)
{
if(r!=NULL)
{
inorder(r->left);
printf("\t %d",r->data);
inorder(r->right);
}
}

in the above code when the last element is reached i.e r->left becomes null
then when inorder(r->left) is called it will send a null value to the function
and if condition becomes false so the it'll come out of the function.

But the problem is that instead of coming out of the loop it executes the
printf("\t %d",r->data); statement when it comes to the last element.


Please explain me(if you don't understand the question plz read it twice i need your help)

Thanks
It should be like this.

1
2
3
4
5
6
inorder(struct node *r)
{
 if(r->left != NULL) inorder(r->left);  
 printf("\t %d", r->data); 
 if(r->right != NULL) inorder(r->right); 
}
Last edited on
Topic archived. No new replies allowed.