Recursion: The processes of breaking down a complex problem of a specific type, into much smaller problems of the same type. This week we applied recursive solutions to a couple of data structures, namely, Trees and LinkedLists. These are two great data structures to apply recursive solutions because both structures are just made of smaller instances of themselves. For example, a tree can be thought of being made up of many smaller trees, each with their own "value", "root" and "branches". So if a function can work on a single tree, it can be easily modified to work on a more complex tree. Same thing for LinkedLists. Each node can be though of as another LL. So lets say I want to remove an item from a LL, instead of writing a loop to traverse the LL, delete the item, and then fix all the pointers, what I can do is: traverse till the node and just call the decapitate method of that node. This is possible because that internal node has the exact same properties as the external LL, but only on a smaller, simpler LL.
Over the past couple of weeks I have really started to see the use in recursion, and see that it is actually very widely used, is very useful and is usually a very sleek alternative to iteration.
No comments:
Post a Comment