# Inorder binary search tree

Hi,

I am trying to understand recursion for inorder traversal of : http://geeksquiz.com/binary-search-tree-set-2-delete/

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

Say the tree is the following...

/* Let us create following BST
50
/     \
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)
{
inorder(root->left);
###### Who is Participating?
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.

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)
0
Author Commented:

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..
0
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
0

Experts Exchange Solution brought to you by

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

Author Commented:

Am I right, that big oh notation for this is O(n) ?
0
Commented:
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.
0
###### 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
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.