2015年3月22日星期日

Week 10

Last week we had a term test. I made a mistake in a question about preorder. Preorder, inorder and postorder was in lab 06. But it’s the last three functions in lab 06, so I guess I didn't pay much attention to it when I go through the lab 06 sample solution. And also, I implement the required functions based on my cheat sheet. In term test 1, I forgot to make a cheat sheet so I implement all functions on my own during the test and got a good mark, which made me really happy and confident. But now I don’t know if I can implement them all by myself without the help from cheat sheet. And I know we should still go through the lab handout and implement all those functions by ourselves even though TAs are on strike, but I was lazy and didn't treat the lab material seriously since we didn't need to go to lab.
We only had one lecture last week. We talked about some binary search tree insert and delete functions. What I learned from the lecture is if you let a node refer to nothing or nothing refers to it, python will delete it automatically.
Unfortunately, TAs are still on strike so we didn't have lab last week.

The weather is warm. I am so happy the winter is over.  

Week 9

Last week we talked about linked list in lecture.

Linked list is another kind of data structure. It consists a group of nodes, with every node be the reference of next node. We defined two classes: LLNode and LinkedList. We use LLNode to represent each node and LinkedList to represent the whole linked list. Each node has a value and a “.nxt”, and each linked list has a back, a front, and a size.
(linked list)

Because the TAs are still on strike, we didn’t have lab this week. But I read the lab handout and the sample solution. This lab was mainly about the basic property of linked list. We were asked to implement functions like delete a node, or find a node. We haven’t use recursion in linked list yet.


I noticed it’s really easy to forget to change the size of a linked list after you delete or insert a node in it. I should pay attention to it in the test. And the test is coming, I should start to do some past papers.

2015年3月21日星期六

Week 8


In week 7 we learned about another abstract data type, binary tree. Binary tree is a kind of tree with special property.
(binary tree)


(binary search tree)
Each node in a binary tree has at most two children, left child and right child. There is a special kind of binary tree called binary search tree. The value of its left child is smaller than the value of the current node, and the value of its right child is bigger than the value of the current node. Because of this property, we can search for data efficiently in a binary search tree. For example, if you want to know if a binary search tree contains a value:
def bst_contains_v1(node, v):
    ''' (BTNode, v) -> bool
   
    Return whether the binary tree rooted at node contains v.

    Assumption: node is the root of a binary search tree.
     '''
    if v == None:
        return False
    elif v == node.data:
        return True
    elif v < node.data:
        return contains(node.left, v)
    else:
        return contains(node.right, v)   
If you want to search for a value in a binary search tree, you don’t need to search through the entire tree. You can compare the value with the value of current node. If the value is bigger than the value of current node, you don’t need to compare it with the nodes that are rooted at its left children.

We had an assignment due the following week, my teammates and I all worked very hard. Using recursion in our assignment is really interesting and challenging.

2015年3月8日星期日

Summary of Recursion

This week we talked about binary tree in class. The recursion we saw before were all in the style of:
  if: ……
  else: ……, but in the code of bst_contains_vl(node, v), we saw recursion in:   if: ……
  elif: ……
  elif: ……
  else: ……
Recursion means a function calls itself to solve problems. It’s really convenient to use recursion in some situation. For example, count the element of a list. People might say ”Oh, this is easy, we can choose a counter n, then for every element in the list n = n + 1. We can do it without recursion. ” This is doable when the list is very simple like this: [1, 2, 3, 4, 5]. But what if the list look like this: [1,[2,[3,[4, 5]]]]? Then it’s very difficult to solve this problem in a traditional way. This is why we use recursion. If we use recursion, the function itself will look for the numbers in the list:
  if not isinstance(L, list):
      return 1
  else:
      return sum([count_element(c) for c in L])
You may think you get the idea of recursion after some practice, but you will know you are wrong when you see this:
  def M(n):
    if n > 100:
        return n – 10
    else:
        return f(f(n + 11))
It’s called McCrathy 91 function. The results of evaluating the function are given by M(n) = 91 for all integer arguments n ≤ 100, and M(n) = n − 10 for n > 100. Indeed, the result of M(101) is also 91 (101 - 10 = 91). All results of M(n) after n = 101 are continually increasing by 1, e.g. M(102) = 92, M(103) = 93. I try to trace this function, but the process is really hard.

Recursion can be very challenging, the only way to understand a recursive function is to trace it.