Sunday, March 15, 2015

Week 7

In week 7 all of us learnt the new knowledge of Binary Tree Node. Binary tree is harder enough to understand and it cost me a lot of times to consider about it. This was the code of a basic btnode function:
class BTNode:
    '''Binary Tree node.'''

    def __init__(self, data, left=None, right=None):
        ''' (BTNode, object, BTNode, BTNode) -> NoneType

        Create BTNode (self) with data and children left and right.
        '''
        self.data, self.left, self.right = data, left, right

    def __repr__(self):
        ''' (BTNode) -> str

        Represent BTNode (self) as a string that can be evaluated to
        produce an equivalent BTNode.

        >>> BTNode(1, BTNode(2), BTNode(3))
        BTNode(1, BTNode(2, None, None), BTNode(3, None, None))
        '''
        return 'BTNode({}, {}, {})'.format(repr(self.data),
                                           repr(self.left),
                                           repr(self.right))

    def __str__(self, indent=''):
        ''' (BTNode) -> str

        Return a user-friendly string representing BTNode (self) inorder.
        Indent by indent.

        >>> b = BTNode(1, BTNode(2, BTNode(3)), BTNode(4))
        >>> print(b)
            4
        1
            2
                3
        <BLANKLINE>
        '''
        right_tree = self.right.__str__(indent + '    ') if self.right else ''
        left_tree = self.left.__str__(indent + '    ') if self.left else ''
        return right_tree + '{}{}\n'.format(indent, str(self.data)) + left_tree


if __name__ == '__main__':
    import doctest 
doctest.testmod() 
Fortunately our assignment do not required btnode inside. Although it do not need it, I and our team were absolutely stuck in the assignment part minimax. It`s so hard that let us working on it for two nights, both night we stay in the lab till three A.M.. At last we still not solved the problem and our team feel disappointment. It`s almost the due date we have to finished it. Wish next night we can finish it..

No comments:

Post a Comment