Sunday, April 5, 2015

Week 6: Recursion by using Tree model

This week we are still working on recursion but with a new way to understand it: tree. 

Using tree model made me confused at beginning because I cannot read the tree properly. The way tree looks like in python is different because it is hard to write a algorighm to draw a tree the way i usually do on paper, which looks like:  
       10
    9      12
  1      11  13.
Instead, the tree is being drew horizontally from left to right. Therefore, it is hard for me to read the tree. After reading it this way in many times, i start to get familiar with the tree. By using tree as a model, we start to write recursive function for counting the leaves on tree, the nodes on tree, etc. 

From my experience of writing recursive function, i get the "steps" of writing it. First of all, writing a basic case which is usually something is None, which does not occur at all. Therefore, it return something directly. Secondaly, go further, write a one-time recursion. Using counting leaves on tree as an example, recursively calculate the number of leaves on the left, and then do it again on the right. Then return the sum of them.

It's kinda abstract and hard to express the idea by using words itself. Next time i will try to give some more detailed explaination. Studying computer science, in my opinion, a major part is to understand the concepts. Getting used to the concepts would make coding much faster and easier.
             

No comments:

Post a Comment