Example #1
0
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)
Example #2
0
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
Example #3
0
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
Example #4
0
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
Example #5
0
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
Example #6
0
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
Example #7
0
    def test_isempty(self):
        tree = Bst()
        self.assertTrue(tree.isempty())

        ARBITRARY_SIZE = 5
        tree.size = ARBITRARY_SIZE
        self.assertFalse(tree.isempty())
Example #8
0
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
Example #9
0
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
Example #10
0
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
Example #11
0
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
Example #12
0
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
Example #13
0
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
Example #14
0
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
Example #15
0
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])
Example #16
0
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
Example #17
0
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
Example #18
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
Example #19
0
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
Example #20
0
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]
Example #21
0
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
Example #22
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
Example #23
0
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
Example #24
0
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
Example #25
0
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
Example #26
0
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
Example #27
0
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
Example #28
0
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
Example #29
0
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
Example #30
0
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
Example #31
0
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
Example #32
0
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
Example #33
0
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
Example #34
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
Example #35
0
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])
Example #36
0
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
Example #37
0
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)
Example #38
0
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])
Example #39
0
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])
Example #40
0
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])
Example #41
0
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])
Example #42
0
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])
Example #43
0
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])
Example #44
0
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])
Example #45
0
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
Example #46
0
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])
Example #47
0
 def setUp(self):
     """ Setup test """
     self.bst = Bst()
Example #48
0
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")
Example #49
0
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
Example #50
0
def bst_empty():
    """Create a binary search tree."""
    from bst import Bst
    return Bst()
Example #51
0
def empty_instance():
    """Empty tree fixture."""
    tree = Bst()
    return tree
Example #52
0
def three():
    """Create three item tree."""
    from bst import Bst
    tree = Bst([2, 1, 3])
    return tree
Example #53
0
def instance():
    """Full tree fixture."""
    tree = Bst()
    for n in range(20):
        tree.insert(n)
    return tree
Example #54
0
def left_left_instance():
    """Left-balanced fixture."""
    tree = Bst()
    for n in range(0, 20, -1):
        tree.insert(n)
    return tree