def test_rebalance(): """Tests my rebalancing algorithm""" bintree = BST() for i in xrange(100): bintree.insert(random.randint(0, 1e6)) assert bintree.size() == 100 original_depth = bintree.depth() bintree.rebalance() assert abs(bintree.balance()) < 2 assert original_depth > bintree.depth() bintree = BST() for i in xrange(100): bintree.insert(i) # Horribly unbalanced assert bintree.size() == 100 assert bintree.depth() == 100 assert bintree.balance() == -99 bintree.rebalance() assert abs(bintree.balance()) < 2 assert bintree.depth() < 10 # Much better, anyway
def test_balance(): aBST = BST() assert aBST.balance() == 0 for num, bal in zip([4, 2, 6, 3, 7, 8, 9, 10, 11], [0, 1, 0, 1, 0, -1, -2, -3]): aBST.insert(num) assert aBST.balance() == bal
def test_balance(): tree = BST() assert tree.balance() == 0 items = [0, -2, -1, -4, -3] for item in items: tree.insert(item) assert tree.balance() == 3 items = [1, 2, 3, 4] for item in items: tree.insert(item) assert tree.balance() == -1
def test_balance(): """balance(self) should return a positive or negative integer that represents how well balanced the tree is. Trees deeper on the left return a positive value, trees deeper on the right return a negative value. An ideally-balanced tree should return 0.""" bintree = BST() with pytest.raises(TypeError): bintree.balance(1) # No values allowed assert bintree.balance() == 0 # Empty tree bintree.insert(5) # 5 assert bintree.balance() == 0 # no children, it's balanced bintree.insert(3) # 5 # / # 3 assert bintree.balance() == 1 # Deeper on the left bintree.insert(7) # 5 # / \ # 3 7 assert bintree.balance() == 0 bintree.insert(2) bintree.insert(4) # 5 # / \ # 3 7 # /\ # 2 4 assert bintree.balance() == 1 # Deeper on the left bintree.insert(1) # 5 # / \ # 3 7 # /\ # 2 4 # | # 1 assert bintree.balance() == 2 bintree.insert(9) bintree.insert(10) # 5 # / \ # 3 7 # /\ \ # 2 4 9 # | | # 1 10 assert bintree.balance() == 0 bintree.insert(12) assert bintree.balance() == -1
def test_balance(): b = BST() b.insert(10) b.insert(5) b.insert(15) b.insert(3) b.insert(2) b.insert(1) assert b._depth == 5 assert b._size == 6 assert b.balance(b._root) == -3 assert b.balance(b._root._left) == -3 assert b.balance(b._root._right) == -0
def test_left_balance(): bst = BST() bst.insert(5) bst.insert(4) bst.insert(3) bst.insert(6) assert bst.balance() == 1
def test_right_balance(): bst = BST() bst.insert(5) bst.insert(6) bst.insert(7) bst.insert(3) assert bst.balance() == -1
def test_make_balanced_randomly(): aBST = BST() for y in range(300): for x in range(1000): aBST.insert(randint(0, 1000)) aBST.make_balanced() b = aBST.balance() assert b in [1, 0, -1]
def test_balance_left_super_heavy(): '''Test balance is positive if left is much greater than right depth.''' bst = BST() bst.insert(100) bst.insert(90) bst.insert(88) bst.insert(70) assert bst.balance() == 3
def test_right_rotate(): '''checks right_rotate balances the BTS''' bst = BST() bst.insert(20) bst.insert(10) bst.insert(5) balance = bst.balance() assert balance == 0
def test_delete_one_child(): """delete(self, val) should remove val from the tree if present, if not present it should do nothing. Return None in all cases""" bintree = BST() bintree.insert(5) bintree.insert(3) bintree.insert(7) bintree.insert(2) bintree.insert(4) bintree.insert(1) bintree.insert(9) bintree.insert(10) # 5 # / \ # 3 7 # /\ \ # 2 4 9 # | | # 1 10 assert bintree.size() == 8 assert bintree.delete(7) is None # 5 # / \ # 3 9 # /\ \ # 2 4 10 # | # 1 assert bintree.size() == 7 assert not bintree.contains(7) assert bintree.contains(9) assert bintree.contains(10) assert bintree.balance() == 1 assert bintree.delete(2) is None # 5 # / \ # 3 9 # /\ \ # 1 4 10 assert bintree.size() == 6 assert not bintree.contains(2) assert bintree.contains(1) assert bintree.leftchild.leftchild.value == 1 assert bintree.balance() == 0
def test_delete_two_children(): bintree = BST() bintree.insert(5) bintree.insert(3) bintree.insert(7) bintree.insert(6) bintree.insert(2) bintree.insert(4) bintree.insert(1) bintree.insert(9) bintree.insert(10) # 5 # / \ # 3 7 # /\ / \ # 2 4 6 9 # | | # 1 10 assert bintree.size() == 9 assert bintree.leftchild.balance() == 1 # The subtree at 3 is left-heavy assert bintree.delete(3) is None # So this will delete left-style # 5 # / \ # 2 7 # /\ / \ # 1 4 6 9 # | # 10 assert bintree.size() == 8 assert bintree.leftchild.value == 2 assert bintree.balance() == -1 # The whole tree is now right-heavy assert bintree.delete(5) is None # So deleting the top will go right-style # 6 # / \ # 2 7 # /\ \ # 1 4 9 # | # 10 assert bintree.value == 6 assert bintree.size() == 7 assert bintree.rightchild.value == 7 assert bintree.balance() == -1
def test_balane_returns_negative_number_if_left_branch_is_lower(): """Test that balance returns a negative number when left side of tree is higher.""" from bst import BST t = BST([9, 8, 7, 4, 6, 5, 1, 10, 14, 11]) assert t.balance() == -2
def test_balance_root_only(): '''Test balance is 0 if bst only has root.''' bst = BST() bst.insert(5) assert bst.balance() == 0
def test_balance_empty_bst(): '''Test balance is 0 if bst is empty.''' bst = BST() assert bst.balance() == 0
def test_balance_is_even(): """Test for balance of 0.""" a = BST([5, 4, 6, 3, 7, 2, 8]) assert a.balance() == 0
def test_balance_is_negative(): """Test negative balance.""" a = BST([5, 4, 3, 2, 1, 6]) assert a.balance() == -3
def test_balance_1_node(): """Test balance of tree of only head.""" from bst import BST new_bst = BST() new_bst.insert(10) assert new_bst.balance() == 0
def test_balance_empty_tree(): """Test balance of empty tree.""" from bst import BST new_bst = BST() assert new_bst.balance() == 0
def test_balance_returns_positive_number_if_right_branch_is_lower(): """Test that balance returns a positive number when right side is higher.""" from bst import BST t = BST([9, 8, 5, 14, 25, 15, 29]) assert t.balance() == 1
def test_balance_returns_string_if_bst_is_empty(): """Test that balance returns 'tree is empty' if bst is empty.""" from bst import BST t = BST() assert t.balance() == 'Tree is empty.'