def instance2(): """Fun tree fixture.""" fun_tree = Bst() insertions = [11, 17, 9, 10, 8, 7, 4, 12, 19, 18] for fun in insertions: fun_tree.insert(fun) return fun_tree
def test_sample_of_insertions_size(nums): """Test_sample_of_insertions_size.""" items = random.sample(range(1, 100), 99) test = Bst() for num in items: test.insert(num) assert test.size() == 99
def bst_with_stuff(): """Create BST with stuff in it.""" from bst import Bst bst = Bst() for i in range(1, 21): bst.insert(i) return bst
def bst_is_full(): """Create binary search tree with values and full.""" from bst import Bst new = Bst() new.insert(20) new.insert(100) new.insert(100000) return new
def bst_100_rand(): """100 random numbers in bst.""" from bst import Bst from random import shuffle rando = [num for num in range(100)] shuffle(rando) tree = Bst(rando) return tree
def test_rotated_children_after_insert(seq): """Test that children from top of tree are what we expect after rotate.""" tree = Bst() expected_left_child = list(sorted(seq))[0] expected_right_child = list(sorted(seq))[2] for val in seq: tree.insert(val) assert all([tree.left_child.value == expected_left_child, tree.right_child.value == expected_right_child])
def test_inorder_multiple(nums): """test_inorder_multiple.""" nums = random.sample(range(1, 100), 99) test = Bst() for i in nums: test.insert(i) nums = sorted(nums) gen = test.in_order(test.root) assert [i for i in gen] == nums
def test_rotated_root_after_insert(seq): """Test that value at top of tree is what we expect after rotate.""" tree = Bst() expected_root_val = list(sorted(seq))[1] tree.insert(seq[0]) tree.insert(seq[1]) assert tree.value == seq[0] tree.insert(seq[2]) assert tree.value == expected_root_val
def bst_right_left(): """Create BST with right left nodes.""" from bst import Bst bst = Bst() bst.insert(10) bst.insert(6) bst.insert(7) bst.insert(12) bst.insert(11) return bst
def bst_left_right(): """Create BST with left right nodes.""" from bst import Bst bst = Bst() bst.insert(10) bst.insert(6) bst.insert(4) bst.insert(11) bst.insert(12) return bst
def test_rotated_root_after_delete_l2(): """Test that tree rotates as expected after deleting from heavier side.""" tree = Bst() for val in [4, 5, 2, 3]: tree.insert(val) assert tree.value == 4 tree.delete(5) assert all([tree.value == 3, tree.left_child.value == 2, tree.right_child.value == 4])
def test_rotated_root_after_delete_ll(): """Test that tree rotates as expected after deleting from heavier side.""" tree = Bst() for val in [3, 4, 2, 1]: tree.insert(val) assert tree.value == 3 tree.delete(4) assert all([tree.value == 2, tree.left_child.value == 1, tree.right_child.value == 3])
def test_rotated_root_after_delete_rl(): """Test that tree rotates as expected after deleting from lighter side.""" tree = Bst() for val in [2, 1, 5, 4]: tree.insert(val) assert tree.value == 2 tree.delete(1) assert all([tree.value == 4, tree.left_child.value == 2, tree.right_child.value == 5])
def bst_2(): """Make a bst with stuff.""" bst = Bst() bst.insert(3) bst.insert(5) bst.insert(2) bst.insert(4) bst.insert(1) bst.insert(6) bst.insert(7) return bst
def comp(): r"""Create Large binary tree. 10 / \ 6 13 / \ / \ 4 8 11 14 / \ \ \ 7 9 12 15 in_order (4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) """ from bst import Bst return Bst([10, 6, 4, 8, 7, 9, 13, 11, 14, 12, 15])
def bst_test(): """Create bst will values for tests.""" from bst import Bst new = Bst() new.insert(50) new.insert(40) new.insert(60) new.insert(20) new.insert(100) new.insert(11) new.insert(500) new.insert(10) new.insert(80) new.insert(9999) return new
def right_left_most_has_right_child(): r"""Large binary tree. 1 \ 5 / \ 3 10 / \ 8 20 / 6 \ 7 """ from bst import Bst return Bst([1, 5, 3, 10, 8, 6, 20, 7])
def bst_balanced(): r"""Create a binary search tree 5 numbers. 5 / \ 2 6 / \ \ 1 3 7 depth: 3 balance: 0 === Search Transversals === in_order: (1, 2, 3, 5, 6, 7) pre_order: (5, 2, 1, 3, 6, 7) breadth_first: (5, 2, 6, 1, 3, 7) post_order: (1, 3, 2, 7, 6, 5) """ from bst import Bst return Bst([5, 6, 2, 3, 1, 7])
def bst_right_balance(): r"""Create a binary search tree 5 numbers. 5 / \ 2 8 / \ 6 9 \ 7 depth: 4 balance: 2 === Search Transversals === in_order: (2, 5, 6, 7, 8, 9) pre_order: (5, 2, 8, 6, 7, 9) breadth_first: (5, 2, 8, 6, 9, 7) post_order: (2, 7, 6, 9, 8, 5) """ from bst import Bst return Bst([5, 8, 6, 9, 2, 7])
def robust(): r"""More robust tree. 10 / \ 2 18 / \ / \ 1 9 11 19 / \ 4 16 / \ / \ 3 8 12 17 / \ 6 14 / \ / \ 5 7 13 15 """ from bst import Bst return Bst( [10, 2, 1, 9, 4, 3, 8, 6, 5, 7, 18, 11, 19, 16, 12, 17, 14, 13, 15])
def bst_wiki(): r"""Wikipedia's example tree structure. 6 / \ 2 7 / \ \ 1 4 9 / \ / 3 5 8 depth: 4 balance: 0 === Search Transversals === in_order: (1, 2, 3, 4, 5, 6, 7, 8, 9) pre_order: (6, 2, 1, 4, 3, 5, 7, 9, 8) breadth_first: (6, 2, 7, 1, 4, 9, 3, 5, 8) post_order: (1, 3, 5, 4, 2, 8, 9, 7, 6) """ from bst import Bst tree = Bst([6, 7, 9, 8, 2, 1, 4, 3, 5]) return tree
def bst_left_balance(): r"""Create a binary search tree 5 numbers snake. 5 / 4 / 1 \ 3 / 2 depth: 5 balance: -4 === Search Transversals === in_order: (1, 2, 3, 4, 5) pre_order: (5, 4, 1, 3, 2) breadth_first: (5, 4, 1, 3, 2) post_order: (2, 3, 1, 4, 5) """ from bst import Bst return Bst([5, 4, 1, 3, 2])
def multi_tree(request): """Simple tree fixture.""" seq = request.param tree = Bst() for n in seq: tree.insert(n) undeleted = seq[:] if seq: delete_value = random.choice(seq) undeleted.remove(delete_value) else: delete_value = None size = len(seq) try: depth = math.floor(math.log(size, 2)) + 1 except ValueError: depth = 0 in_order = list(sorted(seq)) return Fixture(seq, tree, size, depth, delete_value, random.choice(range(1001, 2000)), undeleted, in_order)
def test_initalizing_with_non_iterable_or_not_numbers_raises_ValueError(): """Init returns Value error with with non-numbers or non-iterables.""" from bst import Bst with pytest.raises(TypeError): Bst("dfsdfadgasdg")
def bst_empty(): """Create a binary search tree.""" from bst import Bst return Bst()
def empty_instance(): """Empty tree fixture.""" tree = Bst() return tree
def left_left_instance(): """Left-balanced fixture.""" tree = Bst() for n in range(0, 20, -1): tree.insert(n) return tree
def three(): """Create three item tree.""" from bst import Bst tree = Bst([2, 1, 3]) return tree
def setUp(self): """ Setup test """ self.bst = Bst()
def instance(): """Full tree fixture.""" tree = Bst() for n in range(20): tree.insert(n) return tree