Exemple #1
0
def test_insert_many_times_balance():
    """Test self balancing tree still balanced after 50 random insertions."""
    from bst import AVLBST
    avl = AVLBST()
    for _ in range(50):
        random_int = random.randint(1, 1000)
        avl.insert(random_int)
    assert avl.balance() == 1 or avl.balance() == 0 or avl.balance() == -1
Exemple #2
0
def test_insert_items_rebal_right_left_rotation():
    """Test that the tree rebalances on a left right rotation."""
    from bst import AVLBST
    avl = AVLBST()
    avl.insert(85)
    avl.insert(2)
    avl.insert(88)
    avl.insert(79)
    avl.insert(55)
    assert avl.root.val == 85
    assert avl.root.right.val == 88
    assert avl.root.left.val == 55
    assert avl.root.left.left.val == 2
    assert avl.root.left.right.val == 79
    avl.insert(50)
    assert avl.balance() == 1 or avl.balance() == 0 or avl.balance() == -1
    assert avl.root.val == 55