It’s been a while but I’ve decided to go ahead and brush up on my Data Structures & Algorithms every week as a way to strengthen my problem-solving skills. I’m a strong believer in the concept of compound “learning,” so I’m hoping to see results in the near future. This week I began my review of Recursion which a truly fascinating concept.
Learning things visually is pretty much the best way for me to retain any kind of information so I found it helpful that a TA from a couple semesters back would attempt to help us visualize how it is that recursion works. She plainly stated that it can be thought of as a “grapevine” situation where a person at the front of a line decides to share a thought or an idea which is then passed on until it reaches the end of the line, then back again to the person who stated it at the very beginning.
In a programming environment, we must be careful not to cause a “stack overflow” situation where in which we are unaware of what the base case actually needs to be in order for the recursive function to actually stop. The “return” function is also greatly relevant in helping the function read the end of the recursion.
Primarily, any time we are forced to work with trees or graphs (i.e. traversing a file system) recursion may be the cleanest method in helping us solve the given problem. However, we should not forget that any problem that can be solved iteratively, can also be solved recursively and like any piece of knowledge in the computer science world—it should not be abused. Therefore, if the iterative solution is a lot more helpful (for instance, if we are worried about memory allocation), then we should be careful not to force a recursive solution.
Here is an article on recursion that I found to be the most helpful: https://medium.com/better-programming/when-to-loop-when-to-recurse-b786ad8977de