コード例 #1
0
ファイル: test_AVLTree.py プロジェクト: yismaeel21/trees
def test__AVLTree__right_rotate4():
    rotated = AVLTree()
    rotated.root = AVLTree._right_rotate(avltree4.root)
    print('avltree4=', avltree4)
    print('rotated=', rotated)
    assert rotated.is_bst_satisfied()
    assert rotated.to_list('inorder') == avltree4.to_list('inorder')
コード例 #2
0
ファイル: test_AVLTree.py プロジェクト: yismaeel21/trees
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()
コード例 #3
0
ファイル: test_AVLTree.py プロジェクト: ekurz20/trees
def test__AVLTree_remove2(xs):
    '''
    If we remove something from the AVLTree that is not in the AVLTree,
    then the AVLTree should remain unchanged.
    '''
    xs = list(set(xs))
    avl = AVLTree(xs)
    y = 0
    while y in xs:
        y += 1
    avl.remove(y)
    assert avl.to_list('inorder') == sorted(xs)
コード例 #4
0
ファイル: test_AVLTree.py プロジェクト: yismaeel21/trees
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')
コード例 #5
0
ファイル: test_AVLTree.py プロジェクト: ekurz20/trees
def test__AVLTree_remove1(xs):
    '''
    This tests the remove function.
    In order to test the remove function, we must be able to generate valid AVLTrees.
    Therefore, you must have all the insert functionality completed before this test can pass.
    '''
    random.seed(0)
    xs = list(set(xs))
    avl = AVLTree(xs)
    while len(xs) > 0:
        x = random.choice(xs)
        xs.remove(x)
        assert x in avl
        avl.remove(x)
        assert x not in avl
        assert set(avl.to_list('inorder')) == set(xs)
        assert avl.is_avl_satisfied()
コード例 #6
0
ファイル: test_AVLTree.py プロジェクト: yismaeel21/trees
def test__AVLTree__right_rotate3():
    rotated = AVLTree()
    rotated.root = AVLTree._right_rotate(avltree3.root)
    assert rotated.is_bst_satisfied()
    assert rotated.to_list('inorder') == avltree3.to_list('inorder')
コード例 #7
0
ファイル: test_AVLTree.py プロジェクト: yismaeel21/trees
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')