Saturday 13 April 2013

understand recursion

In my school days i was afraid of a thing called Recursion. For me it works like a magick. I never try to take recursion problem head on and worked on them. Instead i take shelter to conditional loop and for few years it worked fine, untill i faced one problem.

Problem was specific to my job but to put it simple it was a problem of traversing a tree and to tell generate its  position matrix.


            A
          /     \
       B        C
      /   \       /  \
    D    E   F    G

Problem:  List down all the node along with their relative position to Root 'A'
eg.
A:  0  (Assume A's position as 0 :root)
B : 0 0
C: 0 0 1
D: 0 0 1  .




There is wonderful Article about recursion on stackoverflow. I strongly recommend it for those who want to see the real logic behind this magick. 

http://stackoverflow.com/questions/717725/understanding-recursion

No comments:

Post a Comment