Exemplo n.º 1
0
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.'
Exemplo n.º 2
0
    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.'
Exemplo n.º 3
0
def test_is_valid():
    '''Test the is_valid_tree method from bst.'''
    
    tree = build_tree([3,0,1,4,6])    
    assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.'
    
    #Manually set a parent pointer to None when the parent BTNode exists
    tree.root.right.parent = None
    assert not bst.is_valid_tree(tree.root), 'Invalid tree was marked as valid.'
    
    #Fix the parent pointer
    tree.root.right.parent = tree.root
    assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.'
    
    #Misdirect a parent pointer to an incorrect BTNode
    tree.root.left.parent = tree.root.left.right
    assert not bst.is_valid_tree(tree.root), 'Invalid tree was marked as valid.'
    
    #Fix the parent pointer
    tree.root.left.parent = tree.root
    assert bst.is_valid_tree(tree.root), 'Valid tree was marked as invalid.'
    
    #Misdirect a left pointer to an incorrect BTNode
    tree.root.left = tree.root.left.right
    assert not bst.is_valid_tree(tree.root), 'Invalid tree was marked as valid.'
Exemplo n.º 4
0
    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.'
Exemplo n.º 5
0
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.'
Exemplo n.º 6
0
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.'
Exemplo n.º 7
0
def play_game(tree_size):
    '''Play the rotation game with tree_size nodes in the tree.'''

    # Blank the screen and make the trees.
    media.add_rect_filled(pic, 0, 0, 400, 400, media.white)
    game_tree = random_tree(tree_size)
    target_tree = random_tree(tree_size)

    # Draw the new trees; the current node is initially the root.
    curr = game_tree.root
    draw(pic, game_tree.root, 0, 0, curr)
    draw(pic, target_tree.root, 0, WIDTH / 2, curr)
    media.show(pic)

    # Ask for user operations until the game tree and target tree are the
    # same.
    while game_tree != target_tree:
        curr = process_user_command(game_tree, target_tree, curr, pic)
        assert bst.is_valid_tree(game_tree.root)