def test_balance_4_nodes_insert(tree_list): tree = Bst() for i in tree_list: tree.insert(i) tree.delete(0) tree.delete(1) tree.delete(2) assert not tree.contains(2)
def test_traversals(): """Fixture for traversals.""" from src.bst import Bst fixture = { 'tree': Bst(['F', 'B', 'A', 'D', 'C', 'E', 'G', 'I', 'H']), 'empty': Bst(), 'pre_order': ['F', 'B', 'A', 'D', 'C', 'E', 'G', 'I', 'H'], 'in_order': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], 'post_order': ['A', 'C', 'E', 'D', 'B', 'H', 'I', 'G', 'F'], 'breadth': ['F', 'B', 'G', 'A', 'D', 'I', 'C', 'E', 'H'] } return fixture
def test_balance_6_nodes_insert(tree_list): tree = Bst() for i in tree_list: tree.insert(i) assert tree.balance() in [-1, 0, 1] for i in range(5): tree.delete(i) assert not tree.contains(i) assert tree.balance() in [-1, 0, 1]
def test_balance_4_nodes_delete(): tree = Bst() for i in [3, 1, 0, 2]: tree.insert(i) assert tree.balance() in [-1, 0, 1] tree.delete(0) assert tree.balance() in [-1, 0, 1]
def test_balance_3_nodes_delete(tree_list): tree = Bst() for i in tree_list: tree.insert(i) for i in range(3): tree.delete(i) assert tree.balance() in [1, 0, -1]
def test_bsts(): """Fixture for bst.""" from src.bst import Bst empty = Bst() one = Bst([5]) three = Bst([5, 3, 7]) balance = Bst([5, 3, 2, 4, 9, 7, 10]) leftheavy = Bst([5, 3, 2, 1]) rightheavy = Bst([5, 6, 7, 8, 9, 10]) return empty, one, three, balance, leftheavy, rightheavy
def test_balance1(): tree = Bst() lst = [5, 3, 2, 4, 1, 0] for i in lst: tree.insert(i) assert list(tree.breadth_first()) == [3, 1, 5, 0, 2, 4] tree.delete(0) assert list(tree.breadth_first()) == [3, 1, 5, 2, 4] tree.delete(1) assert list(tree.breadth_first()) == [3, 2, 5, 4] tree.delete(2) assert not tree.contains(2)
def test_three(): tree = Bst() tree.insert(3) tree.insert(2) tree.insert(4) tree.insert(4.5) tree.insert(5) tree.delete(2) assert not tree.contains(2)
def test_balance_6_nodes_insert(tree_list): tree = Bst() for i in tree_list: tree.insert(i) assert tree.balance() in [-1, 0, 1]
def test_balance_3_nodes_insert(tree_list): tree = Bst() for i in tree_list: tree.insert(i) assert list(tree.breadth_first()) == [1, 0, 2]
def bst_root_fifty(): test_tree = Bst() test_tree.insert(50) test_tree.insert(25) test_tree.insert(100) test_tree.insert(12) test_tree.insert(35) test_tree.insert(75) test_tree.insert(150) return test_tree
def empty_bst(): return Bst()
def three_bst(): test_tree = Bst() test_tree.insert(50) test_tree.insert(25) test_tree.insert(100) return test_tree