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
def test_avlbst_balances_on_insert_right_right(): """Test tree self balances with a left rotation.""" from bst import AVLBST avl = AVLBST() avl.insert(5) avl.insert(6) avl.insert(7) assert avl.root.val == 6 assert avl.root.right.val == 7 assert avl.root.left.val == 5
def test_avlbst_balances_on_insert_left_right(): """Test tree self balances with left right rotation.""" from bst import AVLBST avl = AVLBST() avl.insert(5) avl.insert(3) avl.insert(4) assert avl.root.val == 4 assert avl.root.right.val == 5 assert avl.root.left.val == 3
def test_avlbst_balances_on_insert_left_left(): """Test AVLBST balances itself on insert two left.""" from bst import AVLBST avl = AVLBST() avl.insert(5) avl.insert(4) avl.insert(3) assert avl.root.val == 4 assert avl.root.right.val == 5 assert avl.root.left.val == 3
def test_avlbst_delete_method_balances(): """Test tree is balanced after delete method.""" from bst import AVLBST avl = AVLBST() avl.insert(5) avl.insert(3) avl.insert(2) avl.insert(7) avl.delete(7) assert avl.root.val == 3 assert avl.root.right.val == 5 assert avl.root.left.val == 2
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
def test_self_balancing_after_many_rotations(): """Test all balance methods work in tandom.""" from bst import AVLBST bst = AVLBST([50, 25, 15, 60, 70, 10, 12, 80, 75]) assert bst.root.val == 60 assert bst.root.left.val == 25 assert bst.root.left.left.val == 12 assert bst.root.left.left.left.val == 10 assert bst.root.left.left.right.val == 15 assert not bst.root.left.left.left.right assert not bst.root.left.left.left.left assert bst.root.left.right.val == 50 assert not bst.root.left.right.right assert not bst.root.left.right.left assert bst.root.right.val == 75 assert bst.root.right.right.val == 80 assert not bst.root.right.right.right assert not bst.root.right.right.left assert bst.root.right.left.val == 70 assert not bst.root.right.left.left assert not bst.root.right.left.right
def test_avlbst_class_inherits_bst(): """Test the AVLBST class inherits bst methods.""" from bst import AVLBST avlbst = AVLBST() avlbst.insert(5) assert avlbst.root.val == 5