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

Recommended Answers

All 6 Replies

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.

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

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.

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.

@mikrosfoititis thanks dude ..................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.