1
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

Edited by Narue: Added code tags and formatting

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by mikrosfoititis
0

There's nothing wrong with your code, it's the conventional recursive inorder traversal. Perhaps if you explained what you expected to happen that differs from what actually happens, someone can help you change the code to meet your needs.

Edited by Narue: n/a

1

It is not behaving unexpectedly. When the "inorder(r->left)" is called for the last time with a NULL pointer, it indeed just returns without having executed the loop. Then, the programm just continues to the next statement, which in your case is the printf function.

0

But plz tell me the reason why this code is behaving unexpectedly ?

Your expectations are wrong, so the code will behave unexpectedly for you. Duh.

Edited by Narue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.