Saturday, March 12, 2016

Slog Week8

    This week we were still working on trees, especially Binary trees that have two children, left and right. We focused on different orders to visit nodes through a tree. The professor introduced us three different orders to visit the tree and the one that I felt confused with is level order. The following is an example:

def visit_level(t, n, act):
      """
   Visit each node of BinaryTree t at level n and act on it.  Return
   the number of nodes visited visited.
   """
   if t is None:
       return 0
   elif n == 0:
       return 1
   else:
       return (visit_level(t.left, n-1, act) + 
               visit_level(t.right, n-1, act)

I'm struggling with the else part. I don't understand why it should be n-1 but not n instead. if n = 1, aren't we visit the tree at level 0? I guess the outcome would be 2 but not 3 as we expected. 
    Also, term test two is coming, and we have assignment two to due by two weeks. Hopefully I will be well prepared for the upcoming test and get started for the assignment. 

    

No comments:

Post a Comment