Inorder binary search tree


I am trying to understand recursion for inorder traversal of :

// A utility function to do inorder traversal of BST
void inorder(struct node *root)
    if (root != NULL)
        printf("%d ", root->key);

Say the tree is the following...

    /* Let us create following BST
           /     \
          30      70
         /  \        /  \
       20   40  60   80 */

For inorder, we get 20,30,40,50, 60,70,80

Am i correct in understanding the recursion code is that, until you have not reached the most left, just keep on going left... is this right? any help to get me started is really appreciated.
  if (root != NULL)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
yes, this is correct understanding
the "output (printf function here)" is after the "inorder" function, so you need to proceed to get the leftmost node, then print that node (as there both left and right are null then, and the called function is not doing anything anymore)
zizi21Author Commented:
Thanks for your reply.

But, if both left and right are null, this does not get executed if (root != NULL)
sorry, if this sounds stupid but how  does it get printed then..
Guy Hengel [angelIII / a3]Billing EngineerCommented:
consider running the procedure calling the node (20)

there, we have these lines:
 if (root != NULL)  => the root (20) is obviously not null
        inorder(root->left);   => run the procedure for "left of 20", which is null, hence does nothing tehre
        printf("%d ", root->key);  => here we print the "20" (the node itself)
        inorder(root->right);   => run the procedure for the "right of 20", which is also null, so nothing get's done here.

in the full run, the call of the function goes back in the stacktrace to the call of node 30, which had just called 'inorder(node->left)', will then "printf" the 30 itself, and then call inorder(node->right) which is the node 40 processing

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zizi21Author Commented:
Thanks for replying.

Am I right, that big oh notation for this is O(n) ?
The goal of every tree traversal algorithm is to process every node. The call of a function (one recursive step) takes a constant time. Because of this it really is of the O(n) time complexity.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.