Thursday, March 26, 2015

Week 9

We took second term test in week 9 and it seems not harder than the first term test. I am guessing it was affected by the strike. Wish it can stop as soon as possible. In week 9 we learned the BTNode again and I know that insert must obey the BST condition. here is the example:
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>
    '''.
After the example is the function code of def insert:
def insert(node, data):
    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

No comments:

Post a Comment