Link to home
Start Free TrialLog in
Avatar of zizi21
zizi21

asked on

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);
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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)
Avatar of zizi21
zizi21

ASKER

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..
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zizi21

ASKER

Thanks for replying.

Am I right, that big oh notation for this is O(n) ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial