So, in week 7, a lot of stuff was happening. Prof. Heap was discussing A2 in class because the assignment instructions were almost too detailed to be understandable. (To me, anyways) It took a long time for my partner and me to implement minimax. I understood how it was supposed to work, and how the game theory worked and that the evaluation would be heuristic in nature if the run time were to be improved, but it was difficult to actually implement. It was necessary to have minimax play through a copied game state, and not the actual game. There were also issue with the .over method, in which an error kept occurring. Then, I was able to fix the problem by modifying apply move, in which it was necessary to do a deep copy of the board. I also found the starter code confusing at first, but I got used to it and it turned out to work very well with the rest of our methods' implementations. Hopefully the results of the assignment will be back soon, so I can see if our hard work payed off! Assignment 2 aside, we started working with Binary Trees. A binary tree is a specific type of tree ADT, where each node can have a maximum of two children. We also talked about Binary Search Trees; these are an even more specific type of Binary Tree. Everything in the right of the tree contains a greater value than the current node, and everything to the left contains a smaller value than the current node. Binary search trees are useful from an efficiency standpoint. For example, we did a lot of recursive exercises from the labs that asked us to return specific things from a binary search tree, such as a list of values of the children, or internal nodes, etc. Therefore, the search is efficient in the sense that you can recursively go through the left and right parts of the tree separately , and not go through any information that is not contained within the parameters of the method. (i.e.. if you want to find and return values in a certain range, you know exactly which part of the tree to recurse through) For example, this is my code for one of the methods in lab 6 (with part of the doctoring omitted for space conservation):
def list_between(node, start, end):
""" (BTNode, object, object) -> list
Return a Python list of all values in the binary search tree
rooted at node that are between start and end (inclusive).
>>> list_between(None, 0, 15)
[]
>>> list_between(b, 2, 3)
[2]
>>> L = list_between(b, 3, 11)
>>> L.sort()
>>> L
[4, 6, 8, 10]1
"""
if node is None:
return []
else:
return (list_between(node.left, start, end)
if node.data > start else []) + ([node.data]
if (start <= node.data <= end)
else []) + (list_between(node.right, start, end)
if node.data < end
else [])
I will admit that this function stumped me for a couple hours. I was confused as to how to go about it. But, then I realized that the base case was simple, and from there, all that needed to be done was to recurse through the left and right sides of the tree (using node.left and node.right, respectively), and then only include the nodes that are between the specified start and end. The syntax was difficult to grasp, and I still struggle with it when doing recursion, but it has been getting better through practice. We also touched on inorder, postorder, and preorder; they are very simple concepts to grasp, I find, but it's definitely interesting to draw a simple tree and trace out the order in which the nodes are visited. For inorder evaluation, you visit the left subtree first, then the node itself, then the right subtree. For preorder, you visit the node itself, then the left, then the right subtrees. And for postorder, you visits the left subtree, then the right, then the node itself. I think I am beginning to get a good grasp on binary trees, and I hope that my understand of recursion continues to get better!
No comments:
Post a Comment