#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2

    Smile Can any one explain how this works?


    /* This is a prog for tree inorder traversal and I don't know how this gives me the perfect o/p. Here we are calling inorder again and printf also works. */

    void inorder(struct tree *root)
    {
    if(root != NULL)
    {
    inorder(root->left);
    printf(" %d",root->info);
    inorder(root->right);
    }
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    Well you could try it on a couple of simple trees, then step through the code with a debugger, watching the recursive calls as they happen.

    Here's the first simple tree
    Code:
        T
    Here's the second simple tree
    Code:
        T
       / \
      L   R
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo