Monday, 23 March 2015

week 10

Because of term test two, we only had one hour class. Our class firstly took a little bit of time to look back assignment 2. I am still have trouble with implementation of minimax. I hope it will get clear after office hour next week. Then we covered new material ---assertion. Assert calls make our coding life easier as it could check the bugs that we have in our program. Not only checking bugs, but also tell me exactly which case was failed. It saves time and makes more convenient  for us to debug.

More over, we have discuessed in BST again but more deeply such that insert and delete function(lab) which was similar to what we covered couple weeks ago.

def insert(node, data):
    ''' (BTNode, object) -> BTNode

    Insert data in BST rooted at node if necessary, and return new root.

    >>> b = BTNode(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> print(b)
            14
        12
            10
    8
            6
        4
            2
    <BLANKLINE>
    '''
    return_node = node
    if not node:
        return_node = BTNode(data)
    elif data < node.data:
        node.left = insert(node.left, data)
    elif data > node.data:
        node.right = insert(node.right, data)
    else:  # nothing to do
        pass
    return return_node

The complete  insert function as show above. The insert function basically just search the correct position and insert value into corresponding place. Function runs recursively that go through elements in tree and put value in right position.

1 comment:

  1. Mingyi,

    I agree that Minimax was definitely a difficult function to implement that required a great understanding of the recursive process. I also found that recursion on Binary Search Trees was one of the easier recursions to implement since all the cases are laid out for you. All you have to do is analyze whether the value in question is less, greater than the root, or within certain range and then decide accordingly through which subtree to recurse. Also, from our recent lectures on efficiency, a balanced BST is O(logn) for functions like insert, delete, or search which makes it a very useful data structure.

    ReplyDelete