Thread: Please help me to undersatnd recursion in printInorder() function.

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    5

    Please help me to undersatnd recursion in printInorder() function.

    Code:
    // C program for different tree traversals
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* A binary tree node has data, pointer to left child
       and a pointer to right child */
    struct node
    {
         int data;
         struct node* left;
         struct node* right;
    };
    
    /* Helper function that allocates a new node with the
       given data and NULL left and right pointers. */
    struct node* newNode(int data)
    {
         struct node* node = (struct node*)
                                      malloc(sizeof(struct node));
         node->data = data;
         node->left = NULL;
         node->right = NULL;
    
         return(node);
    }
    
    /* Given a binary tree, print its nodes according to the
      "bottom-up" postorder traversal. */
    void printPostorder(struct node* node)
    {
         if (node == NULL)
            return;
    
         // first recur on left subtree
         printPostorder(node->left);
    
         // then recur on right subtree
         printPostorder(node->right);
    
         // now deal with the node
         printf("%d ", node->data);
    }
    
    /* Given a binary tree, print its nodes in inorder*/
    void printInorder(struct node* node)
    {
         if (node == NULL)
              return;
    
         /* first recur on left child */
         printInorder(node->left);
    
         /* then print the data of node */
         printf("%d ", node->data);  
    
         /* now recur on right child */
         printInorder(node->right);
    }
    
    /* Given a binary tree, print its nodes in preorder*/
    void printPreorder(struct node* node)
    {
         if (node == NULL)
              return;
    
         /* first print data of node */
         printf("%d ", node->data);  
    
         /* then recur on left sutree */
         printPreorder(node->left);  
    
         /* now recur on right subtree */
         printPreorder(node->right);
    }    
    
    /* Driver program to test above functions*/
    int main()
    {
         struct node *root  = newNode(1);
         root->left             = newNode(2);
         root->right           = newNode(3);
         root->left->left     = newNode(4);
         root->left->right   = newNode(5); 
    
         printf("\nPreorder traversal of binary tree is \n");
         printPreorder(root);
    
         printf("\nInorder traversal of binary tree is \n");
         printInorder(root);  
    
         printf("\nPostorder traversal of binary tree is \n");
         printPostorder(root);
    
         getchar();
         return 0;
    }
    
    I think in printInorder() function, this statement

    /* first recur on left child */
    printInorder(node->left);

    will occur again and will not move to printf, could you please tell me how exactly it would work?

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Stolen code and can't understand it? Woe for you. The GeeksforGeeks page you stole the code from actually explains it well.

    Step through it in a debugger and you wouldn't have to ask silly questions.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    5
    Quote Originally Posted by Hobbit View Post
    Stolen code and can't understand it? Woe for you. The GeeksforGeeks page you stole the code from actually explains it well.

    Step through it in a debugger and you wouldn't have to ask silly questions.
    I was just trying to understand the recursion in this code on GeeksforGeeks, was not able to understand, I asked on this forum. What is wrong in it. And I think its no wrong to explore on GeeksforGeeks as this code is opensource. I never said this is my code. If you don't want to answer, please don't and keep your sorrows with you.

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    I told you what to do. Step through it in a debugger. You can manage that much can't you?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Here for reference -> Tree Traversals - GeeksforGeeks

    > I never said this is my code.
    You never said that it wasn't either.
    Watch the 8 minute video.

    If you can't understand tree traversal, then start with something simpler, like say recursive factorial calculation.

    You can see recursion in action here as well.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Salem View Post
    You can see recursion in action here as well.
    It took me two clicks to get that.

    Dear Mr. OP:

    People are very sensitive about posting other's code without attribution. Even "open source" code requires attribution. Just remember to do so in the future.

    To properly understand recursion it is extremely important to resist the beginner's urge to follow the recursive calls. The entire point of recursion is that you don't need to follow the details. It might be useful to follow the details once or twice just to convince yourself that the mechanism actually works, but after that it's a total waste of time.

    Take printInorder, for example:
    Code:
    void printInorder(struct node *node) {
        if (node == NULL) return;   // if the tree is empty, print nothing
        printInorder(node->left);   // print the left side of the tree in order
        printf("%d ", node->data);  // print the root element
        printInorder(node->right);  // print the right side of the tree in order
    }
    Clearly, if it does what the comments say then it will print the entire tree in order. And that's that. It is obviously correct and does what we want. The power of recursion comes from the fact that we don't need to follow the recursive calls.

  7. #7
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    I found it funny the guy was asking about in order traversal when the previous function in the code was post order traversal. Didn't he have a problem understanding that? Probably didn't need to. He probably has homework on an in order traversal.
    If he had run the code before even asking his silly question he would have seen what happens by stepping through it in a debugger. I can't abide laziness. Here's some code I found with Google, I can't be bothered to study it myself, tell me how it works. The page he stole it from has a good explanation and as Salem pointed out an 8 minute video too.
    A recursive function is simply a function that calls itself over and over until a base case is met then work is done as the calls unwind. Often the problem understanding recursion is being able to see the flow of execution. Well a debugger shows that brilliantly when you step through the code.
    And yes I also loved Salem's sense of humour.
    Last edited by Hobbit; 12-16-2016 at 10:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with a recursion function
    By nicz888 in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2007, 09:25 AM
  2. help with recursion function
    By robasc in forum C++ Programming
    Replies: 12
    Last Post: 12-10-2006, 05:24 PM
  3. Function Recursion
    By iSlak in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2005, 04:08 PM
  4. recursion function
    By jdr30 in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2003, 10:58 PM
  5. Need help for this recursion function.
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2002, 03:51 PM

Tags for this Thread