Tuesday, 17 February 2015

Branching Out With Trees

As a class, our first introduction to Trees was Diane's very amusing classroom activity. In said activity, each student became a node in a tree, with the root at the back of the class "looking over his domain". As the tree moved through the class the more vocal nodes monopolized possible children while the quieter nodes remained childless and eventually became leaves as all possible nodes were claimed.

After we created this abstract data structure, Diane had us apply recursion subtly. This wasn't presented as an exercise where we had to use recursion, however a recursive height calculating algorithm is a beautiful solution to figuring out the depth of a tree. Even though this was done at a point where all students should be familiar with a simple recursive algorithm, I found many of my classmates were confused as to the implementation of this algorithm.

For example, the exact implementation of finding the height of a tree is to add one to the largest height amongst the children of said node. I was an interior node in the class wide tree, and one of my children had a height of 6. So I turned to my parent node and told him that I had height 7, I continued to watch him and dumbfounded I hear him tell his parent that he has height 7. However the parent had heard what I had said and was actually paying attention in class and quickly corrected him, seconds before I would have.

Sunday, 8 February 2015

Tracing Recursion

This is not my first encounter with recursive calls. My first was through a joke that goes as follows:

Student: "How do I learn recursion?"
Teacher: "You have to know recursion to understand recursion..."

It still makes me chuckle and I may have written it on my most recent midterm where we had to trace recursion as a question.

Tracing recursion isn't as easy as I thought it would've been. I fully understand recursion, but to go through each line of the calls is annoying. However this pedantic approach to tracing recursion definitely helped me get the correct answer. At first glance I thought this particular function returned the length of the largest list in a series of nested lists. However after further examination I found that it summed the lengths of all lists in a set of nested lists.

To conclude: recursion is a very useful tool in the arsenal of a computer programmer. However it has to be treated very carefully. While running a certain recursive program, I forgot to fully define the end case, and inadvertently created an infinite loop, which caused my computer to freeze. So a warning to future me, always read your recursive code carefully before running.