Ejemplo n.º 1
0
def test_depth():
    aBST = BST()
    assert aBST.depth() == 0
    for num, depth in zip([4, 2, 6, 3, 7, 8, 9, 10, 11],
                          [1, 2, 2, 3, 3, 4, 5,  6,  7]):
        aBST.insert(num)
        assert aBST.depth() == depth
Ejemplo n.º 2
0
def test_rebalance():
    """Tests my rebalancing algorithm"""
    bintree = BST()

    for i in xrange(100):
        bintree.insert(random.randint(0, 1e6))
    assert bintree.size() == 100

    original_depth = bintree.depth()

    bintree.rebalance()
    assert abs(bintree.balance()) < 2
    assert original_depth > bintree.depth()

    bintree = BST()

    for i in xrange(100):
        bintree.insert(i)  # Horribly unbalanced
    assert bintree.size() == 100
    assert bintree.depth() == 100
    assert bintree.balance() == -99

    bintree.rebalance()
    assert abs(bintree.balance()) < 2
    assert bintree.depth() < 10  # Much better, anyway
Ejemplo n.º 3
0
def test_depth():
    bst = BST()
    bst.insert(5)
    assert bst.depth(node=bst.root) == 1
    bst.insert(3)
    bst.insert(4)
    bst.insert(6)
    assert bst.depth(node=bst.root) == 3
Ejemplo n.º 4
0
def test_depth():
    bst = BST()
    bst.insert(5)
    assert bst.depth(node=bst.root) == 1
    bst.insert(3)
    bst.insert(4)
    bst.insert(6)
    assert bst.depth(node=bst.root) == 3
Ejemplo n.º 5
0
def test_depth():
    tree = BST()
    assert tree.depth() == 0
    items = [0, -2, -1, -4, -3]
    for item in items:
        tree.insert(item)
    assert tree.depth() == 4
    items = [1, 2, 3, 4]
    for item in items:
        tree.insert(item)
    assert tree.depth() == 5
Ejemplo n.º 6
0
def test_bst_can_take_tuple_at_initialization():
    """Test that the BST can take a tuple."""
    from bst import BST
    b = BST((1, 2, 3))
    assert b.size() == 3
    assert b.depth() == 2
    assert b.left_depth == 0
    assert b.right_depth == 2
Ejemplo n.º 7
0
def test_bst_can_take_string_at_initialization():
    """Test that the BST can take a string."""
    from bst import BST
    b = BST('abc')
    assert b.size() == 3
    assert b.depth() == 2
    assert b.left_depth == 0
    assert b.right_depth == 2
Ejemplo n.º 8
0
def test_depth_collatz():
    bst = BST()
    for i in range(1, 11):
        if not i % 2:
            i /= 2
        else:
            i = i * 3 + 1
        print('i = {}'.format(i))
        bst.insert(i)
    assert bst.depth(bst.root) == 5
Ejemplo n.º 9
0
def test_depth_collatz():
    bst = BST()
    for i in range(1,11):
        if not i % 2:
            i /= 2
        else:
            i = i * 3 + 1
        print('i = {}'.format(i))
        bst.insert(i)
    assert bst.depth(bst.root) == 5
Ejemplo n.º 10
0
def test_depth():
    """depth(self) should return an integer representing the total number of
    levels in the tree. If there is one value, the depth should be 1, if two
    values it will be 2, if three values it may be 2 or three, depending."""
    bintree = BST()
    with pytest.raises(TypeError):
        bintree.depth(1)  # No values allowed

    assert bintree.depth() == 0
    bintree.insert(5)
    #   5
    assert bintree.depth() == 1
    bintree.insert(3)
    #   5
    #  /
    # 3
    assert bintree.depth() == 2
    bintree.insert(7)
    #   5
    #  / \
    # 3   7
    assert bintree.depth() == 2
    bintree.insert(2)
    bintree.insert(4)
    #     5
    #    / \
    #   3   7
    #  /\
    # 2  4
    assert bintree.depth() == 3
    bintree.insert(1)
    bintree.insert(6)
    #     5
    #    / \
    #   3   7
    #  /\   /
    # 2  4  6
    # |
    # 1
    assert bintree.depth() == 4
Ejemplo n.º 11
0
def test_depth_empty():
    '''Test depth empty.'''
    bst = BST()
    assert bst.depth() == 0
Ejemplo n.º 12
0
def test_size_empty_depth():
    """Assert an empty tree has a depth of zero."""
    from bst import BST
    b = BST([])
    assert b.depth() == 0
Ejemplo n.º 13
0
def test_depth_returns_0_when_bst_is_empty():
    """Test that empty bst returns none when checked for depth."""
    from bst import BST
    t = BST()
    assert t.depth(t.root) == 0
Ejemplo n.º 14
0
def test_depth_returns_0_when_one_node_in_bst():
    """Test that depth returns 0 when only one node in bst."""
    from bst import BST
    t = BST([1])
    assert t.depth(t.root) == 0
Ejemplo n.º 15
0
def test_depth_all_left():
    bst = BST()
    for i in range(110,10,-10):
        bst.insert(i)
    assert bst.depth(bst.root) == 10
Ejemplo n.º 16
0
def test_depth_returns_1_when_at_most_two_nodes_in_bst():
    """Test that depth returns one when two nodes in bst."""
    from bst import BST
    t = BST([4, 3])
    assert t.depth(t.root) == 1
Ejemplo n.º 17
0
def test_depth_all_left():
    bst = BST()
    for i in range(110, 10, -10):
        bst.insert(i)
    assert bst.depth(bst.root) == 10