In class, we have used recursion on trees. One way to think of recursion when working with trees is by assuming that it works for one level of the tree, say level n. If the recursive step solves the sub-problem from n to n+1, then we can assume that the recursive function works for all sub-problems of the tree. The concept of recursion is very similar to induction. We first check for the base case, and when writing recursive code, it is always best to check if the recursive function works for the base case. If it works for the base case, we check to see if the recursive step solves a problem correctly from one level of the tree to the next level, or as we can say, from one node to its children. If the recursive function successfully solves the problem from one level to the next, then we can assume it works for the entire tree.
When writing recursive code, it can be useful to remember a few steps:
1. Write what the function should return in the base case, or what it should return when the tree has no children when working with trees.
2. What it should do when the root of the tree has children.
3. The recursive step.
When the function reaches it's recursive step, the function will break the problem down into smaller pieces and solve the problem for each of it's children, until it finally arrives at the base case. Therefore, it is very important to ensure that the function does exactly what it should correctly for the base case.
So far, I have been working to understand the exercises in the lab hand outs and understand the examples that have been done in class by drawing out the trees, tracing through examples, and thinking about what I would need to do at each step in order to successfully implement a recursive function. I look forward to becoming a master at recursion in the coming weeks.
1. Write what the function should return in the base case, or what it should return when the tree has no children when working with trees.
2. What it should do when the root of the tree has children.
3. The recursive step.
When the function reaches it's recursive step, the function will break the problem down into smaller pieces and solve the problem for each of it's children, until it finally arrives at the base case. Therefore, it is very important to ensure that the function does exactly what it should correctly for the base case.
So far, I have been working to understand the exercises in the lab hand outs and understand the examples that have been done in class by drawing out the trees, tracing through examples, and thinking about what I would need to do at each step in order to successfully implement a recursive function. I look forward to becoming a master at recursion in the coming weeks.
I totally agree, recursion is amazing and unique! You can take what you applied in the base case of a tree and apply it to the rest of the tree. From outside, it looks awkward to see a function calling itself, but when you actually understand it, it makes much more sense and is a lot more enjoyable to use as well.
ReplyDelete