I was first introduced to recursion not long before CSC148, with a very simple example of how it could be used in a factorial function. I greatly admired the elegance of being able to call a function within itself. The most difficult part of tracing - but also the most interesting - is that the base case is not called until the very end; when you have dug as deep into the call as you can go. For n! this looks like n * (n-1) repeatedly, until n = 1.
Now, having taken a look at recursion in several other examples, I've found that it can be much more complex. Not only can functions be defined recursively, but this can also be applied to class methods. Recursion can be very helpful for tasks such as learning about tree-like data structures, where the values held at nodes can't be indexed. In class, we implemented recursive functions for things like finding the height of a tree, or how many leaves it had.
Tracing an implementation, as a result is significantly more difficult. However, because of the nature of the data structures that I've encountered so far, visualizing recursion is relatively easy. I'm sure I will be drawing plenty of pictures in order to handle upcoming labs and assignments.
No comments:
Post a Comment