Ejemplo n.º 1
0
def test_rand_100_depth_remains_less_than_8():
    """Test 100 numbers depth rational."""
    from bbst import Bst
    from random import shuffle
    max_depth = 0
    for x in range(10):
        rando = [x for x in range(100)]
        shuffle(rando)
        tree = Bst(rando)
        tree_depth = tree.depth()
        if tree_depth > max_depth:
            max_depth = tree_depth
    assert max_depth == 8
Ejemplo n.º 2
0
def hard_mode():
    r"""Test The First Rule of Hard Mode.

                     20
                   /    \
                  5      40
                /  \    /   \
               3   10  30   45
                   /  /  \    \
                  8  25  35    50
                         /
                        33
    in_order: (3, 5, 8, 10, 20, 25, 30, 33, 35, 40, 45, 50)
    breadth_first: (20, 5, 40, 3, 10, 30, 45, 8, 25, 35, 50, 33)

    Try and delete 3!

                     30
                   /    \
                  20    40
                /  \   /  \
               8   25 35  45
              / \     /     \
             5  10   33      50
    in_order: (4, 8, 10, 20, 25, 30, 33, 35, 40, 45, 50)
    breadth_first: (30, 20, 40, 8, 25, 35, 45, 5, 10, 33, 50)
    """
    from bbst import Bst
    return Bst([20, 5, 40, 3, 10, 30, 45, 8, 25, 35, 50, 33])
Ejemplo n.º 3
0
def bst_100_rand():
    """100 random numbers in bst."""
    from bbst import Bst
    from random import shuffle
    rando = [num for num in range(100)]
    shuffle(rando)
    tree = Bst(rando)
    return tree
Ejemplo n.º 4
0
def test_left_rotation_four_node_tree():
    """Test four nodes rotate left, no root change."""
    from bbst import Bst
    tree = Bst([1, 2, 3, 4, 5])
    assert tuple(tree.in_order()) == (1, 2, 3, 4, 5)
    assert tuple(tree.breadth_first()) == (2, 1, 4, 3, 5)
    assert tuple(tree.pre_order()) == (2, 1, 4, 3, 5)
    assert tuple(tree.post_order()) == (1, 3, 5, 4, 2)
    assert tree.depth() == 3
    assert tree.balance() == 1
Ejemplo n.º 5
0
def test_left_rotation_three_node_tree_including_root():
    """Test three nodes rotate right."""
    from bbst import Bst
    tree = Bst([3, 4, 5])
    assert tuple(tree.in_order()) == (3, 4, 5)
    assert tuple(tree.breadth_first()) == (4, 3, 5)
    assert tuple(tree.pre_order()) == (4, 3, 5)
    assert tuple(tree.post_order()) == (3, 5, 4)
    assert tree.depth() == 2
    assert tree.balance() == 0
Ejemplo n.º 6
0
def testt_left_right_rotation_five_node_tree():
    """Test three nodes rotate right, no root change."""
    from bbst import Bst
    tree = Bst([4, 5, 1, 3, 2])
    assert tuple(tree.in_order()) == (1, 2, 3, 4, 5)
    assert tuple(tree.breadth_first()) == (4, 2, 5, 1, 3)
    assert tuple(tree.pre_order()) == (4, 2, 1, 3, 5)
    assert tuple(tree.post_order()) == (1, 3, 2, 5, 4)
    assert tree.depth() == 3
    assert tree.balance() == -1
Ejemplo n.º 7
0
def robust():
    r"""More robust tree.

                     8
                /        \
             4            13
           /   \        /    \
         2      6      11     16
        / \     /\     /\    /   \
       1   3   5  7  10 12   14   18
                     /        \   / \
                    9         15 17  19
    Is Robust.
    """
    from bbst import Bst
    return Bst(
        [10, 2, 1, 9, 4, 3, 8, 6, 5, 7, 18, 11, 19, 16, 12, 17, 14, 13, 15])
Ejemplo n.º 8
0
def comp():
    r"""Create Large binary tree.

                          11
                      /        \
                    8           13
                  /   \        /  \
                6     10     12    14
               / \    /              \
              4   7  9                15
    in_order (4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
    pre_order: (11, 8, 6, 4, 7, 10, 9, 13, 12, 14, 15)
    breadth_first: (11, 8, 13, 6, 10, 12, 14, 4, 7, 9, 15)
    post_order: (4, 7, 6, 9, 10, 8, 12, 15, 14, 13, 11)
    """
    from bbst import Bst
    return Bst([10, 6, 4, 8, 7, 9, 13, 11, 14, 12, 15])
Ejemplo n.º 9
0
def right_left_most_has_right_child():
    r"""Large binary tree.

                     5
                    /  \
                   3    8
                  /    /  \
                 1    6    10
                       \     \
                       7     20

    in_order (1, 3, 5, 6, 7, 8, 10, 20)
    pre_order: (5, 3, 1, 8, 6, 7, 10, 20)
    breadth_first: (5, 3, 8, 1, 6, 10, 7, 20)
    post_order: (1, 3, 7, 6, 20, 10, 8, 5)
    """
    from bbst import Bst
    return Bst([1, 5, 3, 10, 8, 6, 20, 7])
Ejemplo n.º 10
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 bbst import Bst
    return Bst([5, 6, 2, 3, 1, 7])
Ejemplo n.º 11
0
def bst_right_balance():
    r"""Create a binary search tree 5 numbers.

                     6
                   /   \
                  5     8
                 /     / \
                2     7   9
    depth: 3
    balance: 0
    === Search Transversals ===
    in_order: (2, 5, 6, 8, 7, 9)
    pre_order: (6, 5, 2, 8, 7, 9)
    breadth_first: (6, 5, 8, 2, 7, 9)
    post_order: (2, 5, 7, 9, 8, 6)
    """
    from bbst import Bst
    return Bst([5, 8, 6, 9, 2, 7])
Ejemplo n.º 12
0
def bst_all_to_left():
    r"""Create a binary search tree 5 numbers snake.

                  4
                 /  \
                2    5
               / \
              1   3

    depth: 3
    balance: -1
    === Search Transversals ===
    in_order: (1, 2, 3, 4, 5)
    pre_order: (4, 2, 1, 3, 5)
    breadth_first: (4, 2, 5, 1, 3)
    post_order: (1, 3, 2, 5, 4)
    """
    from bbst import Bst
    return Bst([5, 4, 1, 3, 2])
Ejemplo n.º 13
0
def bst_wiki():
    r"""Wikipedia's example tree structure.

                      7
                   /     \
                  4       9
                /   \    /
              2      6   8
            /  \    /
           1    3  5

    depth: 4
    balance: -1
    === Search Transversals ===
    in_order: (1, 2, 3, 4, 5, 6, 7, 8, 9)
    pre_order: (7, 4, 2, 1, 3, 6, 5, 9, 8)
    breadth_first: (7, 4, 9, 2, 6, 8, 1, 3, 5)
    post_order: (1, 3, 2, 5, 6, 4, 8, 9, 7)
    """
    from bbst import Bst
    tree = Bst([6, 7, 9, 8, 2, 1, 4, 3, 5])
    return tree
Ejemplo n.º 14
0
def three_del():
    """Test Simple balanced three for base of tests."""
    from bbst import Bst
    return Bst([10, 20, 30])
Ejemplo n.º 15
0
def bst_empty():
    """Create a binary search tree."""
    from bbst import Bst
    return Bst()
Ejemplo n.º 16
0
def test_initalizing_with_non_iterable_or_not_numbers_raises_ValueError():
    """Init returns Value error with with non-numbers or non-iterables."""
    from bbst import Bst
    with pytest.raises(TypeError):
        Bst("dfsdfadgasdg")
Ejemplo n.º 17
0
def test_backwards_100_balance_remains_between_1_and_negative_1():
    """Test 100 numbers in a row still has balance."""
    from bbst import Bst
    tree = Bst([x for x in range(100)][::-1])
    assert tree.balance() in range(-1, 2)
Ejemplo n.º 18
0
def test_straight_100_balance_remains_between_1and_negative_1():
    """Test 100 numbers in a row still has balance."""
    from bbst import Bst
    tree = Bst(x for x in range(100))
    assert tree.balance() in range(-1, 2)
Ejemplo n.º 19
0
def three():
    """Create three item tree."""
    from bbst import Bst
    tree = Bst([2, 1, 3])
    return tree