def test_search(): """Test that the search returns a node connected to the selected value.""" from bst import Bst, Node new_bst = Bst() new_bst.insert(1) for item in new_bst.head._search(): assert isinstance(item, Node)
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 test_balance_left_two_nodes(): """Test balance left heavy.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(2) assert new_bst.balance() == 1
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 test_isempty(self): tree = Bst() self.assertTrue(tree.isempty()) ARBITRARY_SIZE = 5 tree.size = ARBITRARY_SIZE self.assertFalse(tree.isempty())
def test_balance_right_two_nodes(): """Test balance right heavy.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(4) assert new_bst.balance() == -1
def test_right_left_conversion_six_nodes(): """ Test that given six nodes in a left-right state converts to left-left. """ from bst import Bst, Node new_bst = Bst() node1 = Node(10) node2 = Node(5) node3 = Node(15) node4 = Node(20) node5 = Node(14) node6 = Node(13) new_bst.head = node1 node1.left = node2 node2.parent = node1 node1.right = node3 node3.parent = node1 node3.right = node4 node4.parent = node3 node3.left = node5 node5.parent = node3 node5.left = node6 node6.parent = node5 new_bst.head.right_left_conversion() assert new_bst.head.right == node5 assert new_bst.head.right.right == node3 assert new_bst.head.right.right.right == node4
def test_balance_left_complex(): """Test a left heavy list with multiple nodes.""" from bst import Bst r = range(10, 0, -1) new_bst = Bst() for i in r: new_bst.insert(i) assert new_bst.balance() == 9
def test_depth(): """Test depth works.""" from bst import Bst r = range(10) new_bst = Bst() for i in r: new_bst.insert(i) assert new_bst.depth() == 10
def test_insert_value_already_exists(): """Test if value exists it is ignored.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(3) assert new_bst.head.right is None assert new_bst.head.left is None assert new_bst.head.value == 3
def test_insert_two_values(): """Test insert works for two values.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(4) assert new_bst.head.value == 3 assert new_bst.head.right.value == 4 assert new_bst.head.left is None
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_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_insert_three_values(): """Test three values.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(5) new_bst.insert(4) assert new_bst.head.value == 3 assert new_bst.head.right.value == 5 assert new_bst.head.right.left.value == 4
def test_check_balance_left_left(): """Test for balance of tree from selected Node.""" from bst import Bst new_bst = Bst() new_bst.insert(15) new_bst.insert(12) new_bst.insert(10) assert new_bst.head.check_balance() == 0 assert new_bst.head.left.check_balance() == 0 assert new_bst.head.right.check_balance() == 0
def test_depth_from_node_three_nodes(): """Test the depth of the tree from a node.""" from bst import Bst new_bst = Bst() new_bst.insert(10) new_bst.insert(15) new_bst.insert(20) # fails until balancing function is created assert new_bst.head.depth() == 2 assert new_bst.head.right.depth() == 1
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 test_breadth_first(): """Test if function returns nodes in order of parent, left, right.""" from bst import Bst tree_vals = [10, 8, 15, 7, 9, 13, 20] results = [] new_bst = Bst() for val in tree_vals: new_bst.insert(val) traversal = new_bst.tree_traversal('breadth') for item in traversal: results.append(item) assert results == [10, 8, 15, 7, 9, 13, 20]
def test_balance_left_complex(): """Test a left heavy list with multiple nodes.""" from bst import Bst r = range(4, -1, -1) new_bst = Bst() for i in r: new_bst.insert(i) assert new_bst.balance() == 1 assert new_bst.head.value == 3 assert new_bst.head.right.value == 4 assert new_bst.head.left.value == 1 assert new_bst.head.left.right.value == 2 assert new_bst.head.left.left.value == 0
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 test_off_balance_delete_tree_non_head(): """Test deletion on tree from complex tree.""" from bst import Bst new_bst = Bst() new_bst.insert(10) new_bst.insert(20) new_bst.insert(5) new_bst.insert(15) new_bst.insert(25) new_bst.delete(20) assert new_bst.contains(20) is False assert new_bst.size == 4 assert new_bst.head.value == 10 assert new_bst.head.right.value == 25 assert new_bst.head.left.value == 5 assert new_bst.head.right.left.value == 15
def test_left_rotation_three_nodes(): """Test that a right-right becomes balanced.""" from bst import Bst, Node new_bst = Bst() node1 = Node(10) node2 = Node(15) node3 = Node(20) new_bst.head = node1 node1.right = node2 node2.parent = node1 node2.right = node3 node3.parent = node2 new_bst.head.left_rotation() assert node2.parent is None assert node2.left == node1 assert node2.right == node3 assert node2.left.parent == node2
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 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_right_left_conversion_three_nodes(): """ Test that given three nodes in a right-left state converts to right-right. """ from bst import Bst, Node new_bst = Bst() node1 = Node(10) node2 = Node(15) node3 = Node(12) new_bst.head = node1 node1.right = node2 node2.parent = node1 node2.left = node3 node3.parent = node2 new_bst.head.right_left_conversion() assert new_bst.head.right == node3 assert new_bst.head.right.right == node2 assert new_bst.head.right.right.parent == node3 assert new_bst.head.right.parent == node1
def test_edge_cases_delete_tree(): """Test deletion on tree from EdgeCases example tree.""" from bst import Bst new_bst = Bst() new_bst.insert(10) new_bst.insert(20) new_bst.insert(5) new_bst.delete(10) assert new_bst.contains(10) is False assert new_bst.size == 2 assert new_bst.head.value == 20 assert new_bst.head.right is None assert new_bst.head.left.value == 5
def test_get_size(): """Test the size method works.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(8) new_bst.insert(5) assert new_bst.get_size() == 3
def test_balance_equal(): """Test that tree is balanced.""" from bst import Bst new_bst = Bst() new_bst.insert(3) new_bst.insert(2) new_bst.insert(4) assert new_bst.balance() == 0
def left_chain_tree(): tree = Bst() # Add root tree.add(ROOT_DATA) # Add left child tree.add(LEFT_DATA) # Add left's child tree.add(LL_DATA) return tree
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 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 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_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_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 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 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 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_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 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 setUp(self): """ Setup test """ self.bst = Bst()
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 test_initialize_bst_iteratble_returns_size_n(): """Test initialize with iterable returns proper size tree.""" from bst import Bst tree = Bst([1, 2, 3, 4, 5]) assert tree._size == 5
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 three(): """Create three item tree.""" from bst import Bst tree = Bst([2, 1, 3]) return tree
def instance(): """Full tree fixture.""" tree = Bst() for n in range(20): tree.insert(n) return tree
def left_left_instance(): """Left-balanced fixture.""" tree = Bst() for n in range(0, 20, -1): tree.insert(n) return tree