Esempio n. 1
0
def test__AVLTree_insert(xs):
    xs = list(set(xs))
    avl = AVLTree()
    for x in xs:
        avl.insert(x)
        assert x in avl.to_list('inorder')
        assert avl.is_bst_satisfied()
        assert avl.is_avl_satisfied()
Esempio n. 2
0
def test__AVLTree_inorder_property(xs):
    '''
    The order we insert objects into a AVLTree 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 AVLTree 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))
    random.seed(0)

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

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

    assert bst1.to_list('inorder') == bst2.to_list('inorder')
Esempio n. 3
0
def test__AVLTree__right_rotate4():
    rotated = AVLTree()
    rotated.root = AVLTree._right_rotate(avltree4.root)
    assert rotated.is_bst_satisfied()
    assert rotated.to_list('inorder') == avltree4.to_list('inorder')
Esempio n. 4
0
def test__AVLTree__left_rotate1():
    rotated = AVLTree()
    rotated.root = AVLTree._left_rotate(avltree1.root)
    assert rotated.is_bst_satisfied()
    assert rotated.to_list('inorder') == avltree1.to_list('inorder')