Esempio n. 1
0
def test__BST_insert(xs):
    xs = list(set(xs))
    bst = BST()
    for x in xs:
        bst.insert(x)
        assert x in bst.to_list('inorder')
        assert bst.is_bst_satisfied()
Esempio n. 2
0
def test__BST_remove_list2(xs, ys):
    xs = list(set(xs))
    bst = BST(xs)
    bst.remove_list(ys)
    for y in ys:
        if y in xs:
            xs.remove(y)
    assert bst.to_list('inorder') == sorted(xs)
Esempio n. 3
0
def test__BST_inorder_property(xs):
    '''
    The order we insert objects into a BST can affect the structure of the tree,
    but it should NOT affect the list we get out from an inorder traversal.
    (Recall that the inorder traversal of a BST should always be a sorted list.)
    This test randomly shuffles the input list two different ways
    and checks that both shufflings give the same output list.
    This tests both the insertion functions and the traversal functions
    to ensure that there are no bad interactions between theses functions.
    '''
    xs = list(set(xs))

    xs1 = copy.copy(xs)
    random.shuffle(xs1)
    bst1 = BST(xs1)

    xs2 = copy.copy(xs)
    random.shuffle(xs2)
    bst2 = BST(xs2)

    assert bst1.to_list('inorder') == bst2.to_list('inorder')
Esempio n. 4
0
def test__BST_remove_and_insert1(xs, ys):
    '''
    This test performs a mixture of both insertions and removals.
    This ensures that there are no weird interactions between inserting and removing.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    for y in ys:
        bst.insert(y)
        x = random.choice(bst.to_list('inorder'))
        bst.remove(x)
        assert bst.is_bst_satisfied()
Esempio n. 5
0
def test__BST_remove2(xs):
    '''
    If we remove something from the BST that is not in the BST,
    then the BST should remain unchanged.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    y = 0
    while y in xs:
        y += 1
    bst.remove(y)
    assert bst.to_list('inorder') == sorted(xs)
Esempio n. 6
0
def test__BST_remove1(xs):
    '''
    This tests the remove function.
    In order to test the remove function, we must be able to generate valid BSTs.
    Therefore, you must have all the insert functionality completed before this test can pass.
    '''
    xs = list(set(xs))
    bst = BST(xs)
    while len(xs) > 0:
        x = random.choice(xs)
        xs.remove(x)
        assert x in bst
        bst.remove(x)
        assert x not in bst
        assert bst.to_list('inorder') == sorted(xs)
        assert bst.is_bst_satisfied()