Friday, March 20, 2015

Week 10: Binary Search Tree Mutation

During week 9, I learnt more about binary search tree, mainly about its mutation. One thing that I had to take into account before going to the class BSTNode is that all data in root.left is less than root.data and all data in root.right is more than root.data. At first, binary search tree sounds really difficult for me, but soon, I realized that the concept of BST is really similar to BTNode that I went over the past two weeks.

Now, I am really good at implementing a BST mutation code such as insert and delete which the professor went over for the class because of continuous practices. Not only that, the term test 2 also covered up to BST mutation, thus I was required to learn the materials.

The below example is a basic BST mutation code that the class visited which is "insert".

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  #need one instance variable
    if not node: #case when node is None
        return_node = BTNode(data)
    elif data < node.data: # case when data in parameter is smaller than BSTNode's data.
        node.left = insert(node.left, data)
    elif data > node.data: # case when data in parameter is bigger than BSTNode's data.
        node.right = insert(node.right, data)
    else:  # nothing to do
        pass
    return return_node

(Source: http://www.cdf.utoronto.ca/~csc148h/winter/Lectures/Diane/w9/bst_mutations.py)

Please see the comments which I have added for a brief instruction on insert(node, data). I actually found this exercise really helpful, as I can see how recursive code can be used differently. However, I am starting to be curious that instead of BTNode, if I had to use a Tree with three branches rooted at data, how would I approach to a new method of mutation insert? This was a question which I wanted to ask to T.A., but sadly, with strike ongoing, I was not able to ask about this question.  

Lab 8 was a similar exercise to BST insert and delete code, so it was easy for me to complete it in few hours. 

Because of the term test 2, I only had one class during week 9. Thus, there was not actually many things that I learnt besides BST mutation. 

What I found difficult during week 9 is how for BST mutation, one needs to be able to identify every cases that can happen given the docstring and parameters. 

Just as a side note, I hope everyone can resolve their problem well whether it is a strike, or exam. With so many things going on recently at the University, I just hope there is a solution which makes everyone happy. Thank you for reading!!! 


No comments:

Post a Comment