def test_post_order_big(): """.""" from bst import BinTree beast = BinTree([ 45, 33, 1, 8, 93, 10, 63, 44, 100, 4, 79, 69, 7, 54, 0, 16, 94, 14, 49, 11 ]) generator = beast.post_order() assert list(generator) == [ 0, 7, 4, 11, 14, 16, 10, 8, 1, 44, 33, 49, 54, 69, 79, 63, 94, 100, 93, 45 ]
def test_breadth_first_big(): """.""" from bst import BinTree beast = BinTree([ 45, 33, 1, 8, 93, 10, 63, 44, 100, 4, 79, 69, 7, 54, 0, 16, 94, 14, 49, 11 ]) generator = beast.breadth_first() assert list(generator) == [ 45, 33, 93, 1, 44, 63, 100, 0, 8, 54, 79, 94, 4, 10, 49, 69, 7, 16, 14, 11 ]
def binary_tree(): """For testing.""" from bst import BinTree bst = [ 5, -1, 1, 8, 9, 10, 17, -3, -10, 4, 2, -100, 7, -5, 0, 16, -22, 14, 3, 11 ] bin_tree = BinTree(bst) return bin_tree
def test_init_with_iter(): """Test to see if instantiating with an iterable works.""" from bst import BinTree new_tree = BinTree([3, 2, 1, 4, 5]) assert new_tree._root.val == 3 assert new_tree._root.left.val == 2 assert new_tree._root.right.val == 4 assert new_tree._root.left.left.val == 1 assert new_tree._root.right.right.val == 5
def test_delete_one_child(): """Delete works on a node with one child.""" from bst import BinTree beast = BinTree([ 45, 33, 1, 8, 93, 10, 63, 44, 100, 4, 79, 69, 7, 54, 0, 16, 94, 14, 49, 11 ]) assert beast.size() == 20 beast.delete(16) assert beast.size() == 19 assert not beast.contains(16) assert beast.search(10).right.val == 14
def test_delete_no_children(): """Delete works on a node with no children.""" from bst import BinTree beast = BinTree([ 45, 33, 1, 8, 93, 10, 63, 44, 100, 4, 79, 69, 7, 54, 0, 16, 94, 14, 49, 11 ]) assert beast.size() == 20 beast.delete(11) assert beast.size() == 19 assert not beast.contains(11) assert beast.search(14).left is None
def test_delete_two_children(): """Delete works on a node with one child.""" from bst import BinTree beast = BinTree([ 45, 33, 1, 8, 93, 10, 63, 44, 100, 4, 79, 69, 7, 54, 0, 16, 94, 14, 49, 11 ]) assert beast.size() == 20 beast.delete(93) assert beast.size() == 19 assert not beast.contains(93) assert beast._root.right.val == 79 assert beast.search(63).right.val == 69 assert beast.search(79).right.val == 100
def test_delete_complex_root(): """Delete works on a node with one child.""" from bst import BinTree beast = BinTree([ 45, 33, 1, 8, 93, 10, 63, 44, 100, 4, 79, 69, 7, 54, 0, 16, 94, 14, 49, 11 ]) assert beast.size() == 20 beast.delete(45) assert beast.size() == 19 assert not beast.contains(45) assert beast._root.val == 44 assert beast._root.right.val == 93 assert beast._root.left.val == 33 generator = beast.in_order() assert list(generator) == [ 0, 1, 4, 7, 8, 10, 11, 14, 16, 33, 44, 49, 54, 63, 69, 79, 93, 94, 100 ]
def test_init_with_empty_bst(): """Test to see if an empty BST has a root or size.""" from bst import BinTree new_tree = BinTree() assert new_tree._root is None assert new_tree._size == 0
def test_depth_none(): """Test that we get none back if empty bst.""" from bst import BinTree binary_tree = BinTree() binary_tree.depth() == 0
def bigger_binary_tree(): """Larger binary tree for testing.""" from bst import BinTree new_tree = BinTree([8, 2, 3, 1, 7, 9, 10]) return new_tree
def test_v_shaped_bst(): """Test odd shape.""" from bst import BinTree beast = BinTree([5, 4, 3, 2, 1, 6, 7, 8, 9, 10]) generator = beast.in_order() assert list(generator) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def test_post_order_small(): """.""" from bst import BinTree beast = BinTree([10, 5, 11, 13, 4, 6, 12, 20, 21, 7]) generator = beast.post_order() assert list(generator) == [4, 7, 6, 5, 12, 21, 20, 13, 11, 10]
def small_bst(): """For small testing.""" from bst import BinTree bst = BinTree([10, 5, 11, 13, 2, 0, 20]) return bst
def test_insert_with_non_int(): """Test that it only accepts a list or tuple as the optional param.""" from bst import BinTree with pytest.raises(ValueError): BinTree('this should break it')
def test_breadth_first_small(): """.""" from bst import BinTree beast = BinTree([10, 5, 11, 13, 4, 6, 12, 20, 21, 7]) generator = beast.breadth_first() assert list(generator) == [10, 5, 11, 4, 6, 13, 7, 12, 20, 21]
def test_breadth_firwt_v_shaped_bst(): """Test odd shape.""" from bst import BinTree beast = BinTree([5, 4, 3, 2, 1, 6, 7, 8, 9, 10]) generator = beast.breadth_first() assert list(generator) == [5, 4, 6, 3, 7, 2, 8, 1, 9, 10]
def binary_tree(): """Binary tree for testing.""" from bst import BinTree new_tree = BinTree() new_tree.insert(5) return new_tree
def empty_bst(): """For more testing.""" from bst import BinTree bst = BinTree() return bst