Week 7 began with more recursion and more function writing in recursion. This time, recursion was done alongside trees. Traversing a tree can be very tiresome if the tree is very big especially when we do not know the exact size of it. This is when recursion comes in and allows us to traverse the tree without having to know how big or small it is.
We applied this idea to trees in this week's CSC148 course. An example of this is comparing the root of a tree to its children. We recursively call the function with every recursive call checking to see whether the root is greater or less than the left or right subtree. With a given condition, the recursive call will stop once we have reached a subtree that is less than that of the root. This is very easy to do with recursion as the function will keep going until the condition is met to stop recursing. When doing recursion like mentioned before, we must keep in mind of a condition to stop the recursion or else the computer will run out of memory and the maximum recursive depth is reached. This condition is usually one that is not met immediately and only reached after going through the tree (answer found in one of the children of the root). This method allows us to not care about the size of the tree as the recursion will do that for us. If we didn't do recursion, it might become extremely difficult to program as the tree is not defined as a finite number.
Recursion is extremely helpful in many situations not just in trees. We however explored trees this week and the traverse of it became much much easier with recursion. Without recursion, tree traversal will be very annoying and hard to do!
No comments:
Post a Comment