Example #1
0
def test__AVLTree_is_avl_satisified2():
    avl = AVLTree()
    avl.root = Node(0)
    avl.root.right = Node(1)
    avl.root.right.right = Node(2)
    assert avl.is_bst_satisfied()
    assert not avl.is_avl_satisfied()
Example #2
0
def test__AVLTree_is_avl_satisified0():
    avl = AVLTree()
    avl.root = Node(0)
    avl.root.left = Node(-1)
    avl.root.left.left = Node(-2)
    assert avl.is_bst_satisfied()
    assert not avl.is_avl_satisfied()
Example #3
0
def test__AVLTree_is_avl_satisified9():
    avl = AVLTree()
    avl.root = Node(0)
    avl.root.left = Node(-2)
    avl.root.left.left = Node(-3)
    avl.root.left.left.left = Node(-4)
    avl.root.left.left.left.left = Node(-5)
    avl.root.left.right = Node(-1)
    avl.root.right = Node(2)
    avl.root.right.left = Node(1)
    avl.root.right.right = Node(3)
    avl.root.right.right.right = Node(4)
    assert avl.is_bst_satisfied()
    assert not avl.is_avl_satisfied()
Example #4
0
def test__AVLTree_eq(xs):
    '''
    This test is essentially the same as the previous one,
    but tests the == operator specifically.
    '''
    xs = list(set(xs))

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

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

    assert bst1 == bst2
Example #5
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()
Example #6
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')
Example #7
0
def test__AVLTree_super():
    x = AVLTree()
    assert isinstance(x, BinaryTree)
    assert isinstance(x, BST)
Example #8
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')
Example #9
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')
Example #10
0
def test__AVLTree___init__(xs):
    xs = list(set(xs))
    avl = AVLTree(xs)
    assert avl.is_bst_satisfied()
    assert avl.is_avl_satisfied()
Example #11
0
def test__AVLTree_is_avl_satisified5():
    avl = AVLTree()
    avl.root = Node(0)
    assert avl.is_bst_satisfied()
    assert avl.is_avl_satisfied()
Example #12
0
def test__AVLTree_is_avl_satisified4():
    avl = AVLTree()
    assert avl.is_bst_satisfied()
    assert avl.is_avl_satisfied()
Example #13
0
from containers.BinaryTree import BinaryTree, Node
from containers.BST import BST
from containers.AVLTree import AVLTree

################################################################################
# these tests are specific for AVLTree rotations

avltree0 = AVLTree()
avltree0.root = Node(5)
avltree0.root.left = Node(3)
avltree0.root.left.left = Node(1)
avltree0.root.right = Node(7)

avltree1 = AVLTree()
avltree1.root = Node(5)
avltree1.root.left = Node(3)
avltree1.root.left.right = Node(4)
avltree1.root.right = Node(7)

avltree2 = AVLTree()
avltree2.root = Node(5)
avltree2.root.left = Node(3)
avltree2.root.right = Node(7)
avltree2.root.right.left = Node(6)

avltree3 = AVLTree()
avltree3.root = Node(5)
avltree3.root.left = Node(3)
avltree3.root.right = Node(7)
avltree3.root.right.right = Node(9)