2015年3月1日星期日

Week7: OOP and Tree Class

Please grade my previous post as my chosen topic is OOP:
http://ruolancsc148.blogspot.ca/2015/02/week-6-object-oriented-programming.html

Week 7 – continued

This week we continued on the topic of object oriented programming – Tree Class. We continued implementing recursive functions, but unlike the previous week, the input was a BTNode , the root of a binary tree.

After initializing a BTNode, we constructed different functions by using the concept of recursion. During the lab I've found drawing diagrams is super helpful for clearing thoughts. I couldn't figure out many functions until drew a diagram. For example, the last function list_longest_path:
def list_longest_path(t: BTNode):
   ''' List the data in a longest path of t.'''
    L = []
    if not t:
        pass
    else:
        L = [t.data] #collect the value of the root of the tree
        if len(list_longest_path(t.left)) >= len(list_longest_path(t.right)):#collect only left path if it is longer
            L += list_longest_path(t.left)
        if len(list_longest_path(t.left)) < len(list_longest_path(t.right)): #collect right  path if it is longer
            L += list_longest_path(t.right)

Based on my understanding I kind-of summarized a recursive algorithm for collecting data:
1. Create an empty list or dictionary;
2. Separate the base case (which is usually very simple) and other cases that need recursive concept;
3. Set the requirements for data to be collected.

All we need are a base case and a way of proving that for any list, we will eventually get to the base case.

Also, I would like to thank Julian's post(on which I commented):


It helped me to clarify and summarize every concept Diane mentioned this week.

没有评论:

发表评论