# 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?

x
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)
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..
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