Saturday, March 7, 2015

Week 8: Binary Tree

During week 7, I was learning about the binary tree. Throughout labs and many exercises I did, I was able to understand how I can be able to do parenthesizing of the tree as well as how to do binary search trees. Thanks to TA's assistance during the tutorial, I was able to get a sense of how different types of tree can be implemented in many different ways to find out the specific points which the user is trying to figure out. One of the most challenging problem I had during lab06 was the very first problem of implementing parenthesizing. However, when I started to think of the implementing this function by parts, I was able to easily figure out the trick to do so.

Moreover, during the lecture, with Danny, I worked on how to evaluate an arithmetic expression that is represented as a binary tree which was interesting for me to look at. After working with several problems involving binary tree, I realized that binary tree is slightly modified version of tree. One thing that caught my attention during the lecture was use of __str__ and __repr__ methods for a binary tree.

    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

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

The above example is str method that was used by the professor to explain how we can represent a binary tree when knowing self.right and self.left. It was actually my first time seeing the use of __str__ method to show a string representation of a binary tree. From the previous CSC108 class, I already knew what was a binary tree, however, it was still interesting for me to see how use of a binary tree can be represented with some modification to __str__ method of class. Thus, this function can let the user to identify the depth of a binary tree clearly. 

During week 8, I learnt about the linked list which is a linear sequence of nodes. Some term I had to familiar of with linked list is the concept of tail and head, front and back which are just explanations of how and what linked list is.  However, I will go over more about linked list in detail with an example on the next SLOG entry.

Also I was busy doing my assignment 2 with my partner this week. The most challenging part in this assignment 2 was to implement minimax function (in our assignment the name of minimax function was suggest_move) in game strategy which allow the computer to pick the best move out of available moves considering all the possibilities. My partner and I knew that there has to be some recursion involved which let the minimax function to check all the possibilities of the game. Later on, I also figured out that minimax was actually the beginning of the artificial intelligence. Throughout assignment 2, it made me realize that there is no easy way when you implement the code like minimax where we have to consider all the possible outcomes and ways to find the best move. I better start studying for upcoming term test 2!

Another thing I want to comment is about TA strike. This week, all students did not get to have a tutorial on lab07. I hope T.A. strike ends soon with both parties, TA union and UofT to reach a reasonable contract.  


No comments:

Post a Comment