def test_rotate_left(): '''Test left rotation on the root of a tree.''' # A full binary search tree. tree = build_tree([4, 2, 1, 3, 6, 5, 7]) # tree.display() # The expected result. target_tree = build_tree([6, 4, 2, 1, 3, 5, 7]) # target_tree.display() assert target_tree.root.parent == None, \ 'tree.parent should have been None.' assert tree.root.parent == None, 'tree.parent should have been None.' assert bst.size(tree.root) == 7, 'tree size should be 7.' assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.' bst.rotate_left(tree, tree.root) assert bst.size(tree.root) == 7, 'tree size should be 7.' assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.' assert tree.root.parent == None, 'tree.parent should have been None.' assert tree == target_tree, '__eq__ did not work properly.' assert not (tree != target_tree), '__ne__ did not work properly.'
def test_rotate_left(self): '''Test left rotation on the root of a tree.''' # A full binary search tree. tree = build_tree([4, 2, 1, 3, 6, 5, 7]) #tree.display() # The expected result. target_tree = build_tree([6, 4, 2, 1, 3, 5, 7]) # target_tree.display() assert target_tree.root.parent == None, \ 'tree.parent should have been None.' assert tree.root.parent == None, 'tree.parent should have been None.' assert bst.size(tree.root) == 7, 'tree size should be 7.' assert bst.is_valid_tree(tree.root), \ 'Valid tree was marked as invalid.' bst.rotate_left(tree, tree.root) assert bst.size(tree.root) == 7, 'tree size should be 7.' assert bst.is_valid_tree(tree.root), \ 'Valid tree was marked as invalid.' assert tree.root.parent == None, 'tree.parent should have been None.' # This calls tree.__eq__(target_tree). Cool, huh? You need to write # the __eq__ method in class BST to do the tree comparison. assert tree == target_tree, '__eq__ did not work properly.' # This calls tree.__ne__(target_tree). assert not (tree != target_tree), '__ne__ did not work properly.'
def draw(pic, root, d, side, curr): '''Draw on Picture pic a picture of the tree rooted at BTNode root, which appears at depth d in a tree. 'side' is the coordinate that marks the left of the tree being drawn, and curr is the currently selected value. Most values appear in black; only the value in curr appears in red.''' if root != None: left_size = bst.size(root.left) # the coordinate where the root will be drawn; the root has # left_size nodes to the left of it. x = side + left_size * NODE_WIDTH # The y coordinate of the root value. y = d * NODE_HEIGHT if curr == root: color = media.red else: color = media.black # Draw the current node's value. media.add_text(pic, WIDTH_OFFSET + x, HEIGHT_OFFSET + y, \ str(root.data), color) if root.left: draw(pic, root.left, d + 1, side, curr) if root.right: draw(pic, root.right, d + 1, x + NODE_WIDTH, curr)
def size(self): """Returns the number of items in the map Calls size function in bst.py Returns: int: The number of nodes in the TreeMap. """ return size(self.tree)
def test_insert_2_left(): '''Test inserting two nodes into a BST where the second value is inserted into the left of the root; also test size and is_valid_tree.''' tree = build_tree([5, 3]) assert tree.root.data == 5, 'tree.data not correct for tree of size 2.' assert tree.root.parent == None, 'tree.parent should have been None.' assert tree.root.right == None, 'tree.right should have been None.' assert tree.root.left.data == 3, 'tree.left should have been 3.' assert tree.root.left.parent == tree.root, \ "tree.left's parent should be root." assert bst.size(tree.root) == 2, 'tree size should be 2.' assert bst.size(tree.root.left) == 1, "tree.left's size should be 1." assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.'
def test_rotate_subtree_left(self): '''Test left rotation on the root of a tree.''' # A full binary search tree. tree = build_tree([4, 2, 1, 3, 6, 5, 7]) old_tree = build_tree([4, 2, 1, 3, 6, 5, 7]) #tree.display() #print "" # The expected result. target_tree = build_tree([4, 2, 1, 3, 7, 6, 5]) #target_tree.display() assert tree.root.parent == None, 'tree.parent should have been None.' assert bst.size(tree.root) == 7, 'tree size should be 7.' assert bst.is_valid_tree(tree.root), \ 'Valid tree was marked as invalid.' bst.rotate_left(tree, tree.root.right) assert bst.size(tree.root) == 7, 'tree size should be 7.' assert bst.is_valid_tree(tree.root), \ 'Valid tree was marked as invalid.' assert tree.root.right.data == old_tree.root.right.right.data, \ '''incorrect rotate, tree's new right substree should start with 7''' assert tree.root.data == old_tree.root.data, \ '''incorrect rotate, tree's root should not have changed''' assert tree.root.right.left.left.data == \ old_tree.root.right.left.data, \ '''incorrect rotate, subtree's leftmost leaf should not have changed''' #RL is right.left, and LR is left.right # This calls tree.__eq__(target_tree). Cool, huh? You need to write # the __eq__ method in class BST to do the tree comparison. assert tree == target_tree, '__eq__ did not work properly.' # This calls tree.__ne__(target_tree). assert not (tree != target_tree), '__ne__ did not work properly.'
def test_insert_1(): '''Test inserting one node into a BST; also test size and is_valid_tree.''' tree = bst.BST() tree.insert(5) assert tree.root.data == 5, 'tree.data not correct for tree of size 1.' assert tree.root.parent == None, 'tree.parent should have been None.' assert tree.root.left == None, 'tree.left should have been None.' assert tree.root.right == None, 'tree.right should have been None.' assert bst.size(tree.root) == 1, 'tree size should be 1.' assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.'