简体   繁体   中英

Understanding Recursion logic

I really need your help for me to understand recursion properly. I can understand basic recursions and their logic like fibonacci

int factorial(int n)
   if(n <=1)
      return n
    else
       return(n*factorial(n-1))

That's easy the function keep calling factorial until n becomes zero and finally multiply all the results. But recursions like tree traversal is hard for me to understand

void inorderTraverse(Node* head)
    if(head!=NULL){

       inorderTraverse(head->left)
       cout << head-> data
        inorderTraverse(head->right)
     }

 }

Here I lost the logic how does this function goes if first recursion call will go back to function how can it goes to cout line or how can it show right child data. I really need your help.

Thank you

A binary search tree in alphabetical order:

  B
 / \
A   C

inorderTraverse(&A) will first descend to A and print it (recursively printing any subtree), then print B , then descend to C and print it (recursively printing any subtree).

So in this case, ABC . For a more complicated tree:

    D
   / \
  B   E
 / \
A   C

This will be printed as ABCDE . Notice how the original tree is on the left of D , so is printed in its entirety first; the problem is reduced to a smaller instance of the starting problem. This is the essence of recursion. Next D is printed, then everything on the right of D (which is just E ).

Note that in this implementation, the nodes don't know about their parent. The recursion means this information is stored on the call stack. You could replace the whole thing with an iterative implementation, but you would need some stack-like data structure to keep track of moving back up through the tree.

Inorder traversal says you need to traverse as Left-Root-Right .So for one level it is fine we print in left-root-right format. But With the level increases you need to makesure your algorithm traverse in the same way.So you need to print the leftSubtree first then the root of that subTree and then the right subTree at each level.

The Recursive code inorderTraverse(head->left) tells till the node is not null go to its leftside of the tree.Once it reaches the end it prints the left node then print the Root node of that subTree and wahtever operation u performed on leftSubTree you need to perform the same on Right subTree that's why you write inorderTraverse(head->right) . Start debugging by creating 3level trees. Happy Learning.

Try to imagine binary tree and then start traversing it from root. You always go left. If there is no more lefts then you go right and after that you just go up. And you will finish back in root (from right side).

It is similar as going thought maze. You can choose that you will always go to left. (you will always touch left wall). At the end you will finish in exit or back in entrance if there isn't another exit.

In this code is important that you have two recursive calls in body. First is for left subtree and second is for right subtree. When you finish one function returns you back to node where you started.

Binary search trees have the property that for every node, the left subtree contains values that are smaller than the current node's value, and the right subtree contains values that are larger.

Thus, for a given node to yield the values in its subtree in-order the function needs to:

  1. Handle the values less than the current value;
  2. Handle its value;
  3. Handle the values greater than the current value.

If you think of your function initially as a black box that deals with a subtree, then the recursion magically appears

  1. Apply the function to the left subtree;
  2. Deal with the current value;
  3. Apply the function to the right subtree.

Basically, the trick is to initially think of the function as a shorthand way to invoke an operation, without worrying about how it might be accomplished. When you think of it abstractly like that, and you note that the current problem's solution can be achieved by applying that same functionality to a subset of the current problem, you have a recursion.

Similarly, post-order traversal boils down to:

  1. Deal with all my children (left subtree, then right subtree, or vice-versa if you're feeling contrary);
  2. Now I can deal with myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM