Wednesday, March 16, 2016

Week 9

This week we get a little deeper into binary tree and I feel much more confident when writing recursive codes, especially the ones for tree classes. I think the most important part is collecting experience. After I practice implementing various tree methods and see different ways of writing, I got to understand which parts I need to care about. For example when we start implementing the recursive codes, we need to first think of based cases, i.e.  thinking of what will happen if root is none and what will happen if the root has no children.

Here is an example:

def parenthesize(b):
    """    
    Return a parenthesized expression equivalent to the arithmetic    
    expression tree rooted at b.
    """
    if b is None:
        return ''    elif b.left is None and b.right is None:
        return str(b.data)
    else:
        return something complicated 

It is like that we need to give every condition an environment to live. True the function 
automatically returns None when it meets a None, but it is still clear to provide the 'what if '
code. 

No comments:

Post a Comment