Exemplo n.º 1
0
def test_in_order_alpha():
    bst = Bst()
    vals = ['F', 'B', 'G', 'A', 'D', 'I', 'C', 'E', 'H']
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order('F')):
        assert g == vals[i]
Exemplo n.º 2
0
def test_in_order_long():
    bst = Bst()
    vals = range(100, 0, -3)
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order(100)):
        assert g == vals[i]
Exemplo n.º 3
0
def test_post_order():
    bst = Bst()
    vals = ["F", "B", "G", "A", "D", "I", "C", "E", "H"]
    for val in vals:
        bst.insert(val)
    comparison = ["A", "C", "E", "D", "B", "H", "I", "G", "F"]
    for i, g in enumerate(bst.post_order("F")):
        assert g == comparison[i]
Exemplo n.º 4
0
def test_in_order_long():
    bst = Bst()
    vals = range(100, 0, -3)
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order(100)):
        assert g == vals[i]
Exemplo n.º 5
0
def test_in_order_alpha():
    bst = Bst()
    vals = ["F", "B", "G", "A", "D", "I", "C", "E", "H"]
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order("F")):
        assert g == vals[i]
Exemplo n.º 6
0
def test_in_order_2():
    bst = Bst()
    vals = [44, 19, 6, 7, 8]
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order(44)):
        assert g == vals[i]
Exemplo n.º 7
0
def test_in_order():
    bst = Bst()
    vals = [37, 22, 6, 28, 17, 14]
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order(37)):
        assert g == vals[i]
Exemplo n.º 8
0
def test_in_order():
    bst = Bst()
    vals = [37, 22, 6, 28, 17, 14]
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order(37)):
        assert g == vals[i]
Exemplo n.º 9
0
def test_in_order_2():
    bst = Bst()
    vals = [44, 19, 6, 7, 8]
    for val in vals:
        bst.insert(val)
    vals.sort()
    for i, g in enumerate(bst.in_order(44)):
        assert g == vals[i]
Exemplo n.º 10
0
def test_post_order():
    bst = Bst()
    vals = ['F', 'B', 'G', 'A', 'D', 'I', 'C', 'E', 'H']
    for val in vals:
        bst.insert(val)
    comparison = ['A', 'C', 'E', 'D', 'B', 'H', 'I', 'G', 'F']
    for i, g in enumerate(bst.post_order('F')):
        assert g == comparison[i]
Exemplo n.º 11
0
def test_size():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst.size() == 6
Exemplo n.º 12
0
def test_depth():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst.depth() == 3
Exemplo n.º 13
0
def test_balance_long_right_side():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(11)
    bst.insert(13)
    bst.insert(15)
    bst.insert(19)
    assert bst.balance() == 5
Exemplo n.º 14
0
def test_balance():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst.balance() == 1
Exemplo n.º 15
0
def test_insert_multiple_vals():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst._nodes[7] == [None, float('inf'), 6]
    assert bst._nodes[10] == [9, float('inf'), 8]
Exemplo n.º 16
0
def test_balance_long_left_side():
    bst = Bst()
    bst.insert(8)
    bst.insert(7)
    bst.insert(6)
    bst.insert(5)
    bst.insert(4)
    bst.insert(3)
    assert bst.balance() == -5
Exemplo n.º 17
0
def test_insert_multiple_vals():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst._nodes[7] == [None, float("inf"), 6]
    assert bst._nodes[10] == [9, float("inf"), 8]
Exemplo n.º 18
0
def test_balance_long_left_side():
    bst = Bst()
    bst.insert(8)
    bst.insert(7)
    bst.insert(6)
    bst.insert(5)
    bst.insert(4)
    bst.insert(3)
    assert bst.balance() == -5
Exemplo n.º 19
0
def test_breadth_all_left():
    bst = Bst()
    bst.insert(8)
    bst.insert(7)
    bst.insert(6)
    bst.insert(5)
    bst.insert(4)
    bst.insert(3)
    _list = []
    for node in bst.breadth_first_traversal():
        _list.append(node)

    assert _list == [8, 7, 6, 5, 4, 3]
Exemplo n.º 20
0
def test_breadth_all_left():
    bst = Bst()
    bst.insert(8)
    bst.insert(7)
    bst.insert(6)
    bst.insert(5)
    bst.insert(4)
    bst.insert(3)
    _list = []
    for node in bst.breadth_first_traversal():
        _list.append(node)

    assert _list == [8, 7, 6, 5, 4, 3]
Exemplo n.º 21
0
def test_insert_bigger_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    assert bst._root == 8
Exemplo n.º 22
0
def test_delete_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(4)
    bst.insert(15)
    bst.insert(7)
    bst.insert(3)
    bst.delete(8)
    assert bst.contains(8) is False
    assert bst._root == 10
Exemplo n.º 23
0
def test_delete_two_children():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(4)
    bst.insert(15)
    bst.insert(7)
    bst.insert(3)
    bst.delete(4)
    assert bst.contains(4) is False
    assert bst._nodes[7] == [3, float('inf'), 8]
    assert bst._nodes[3] == [None, float('inf'), 7]
    assert bst._nodes[8] == [7, 10, None]
Exemplo n.º 24
0
def test_contains_true():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(1) is True
Exemplo n.º 25
0
def test_breadth_complex():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(4)
    bst.insert(15)
    bst.insert(7)
    bst.insert(3)
    _list = []
    for node in bst.breadth_first_traversal():
        _list.append(node)

    assert _list == [8, 4, 10, 3, 7, 15]
Exemplo n.º 26
0
def test_insert_no_root():
    bst = Bst()
    bst.insert(8)
    assert bst._root == 8
Exemplo n.º 27
0
def test_insert_bigger_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    assert bst._root == 8
Exemplo n.º 28
0
def test_breadth_complex():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(4)
    bst.insert(15)
    bst.insert(7)
    bst.insert(3)
    _list = []
    for node in bst.breadth_first_traversal():
        _list.append(node)

    assert _list == [8, 4, 10, 3, 7, 15]
Exemplo n.º 29
0
def test_insert_smaller_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(3)
    assert bst._root == 8
Exemplo n.º 30
0
def test_insert_no_root():
    bst = Bst()
    bst.insert(8)
    assert bst._root == 8
Exemplo n.º 31
0
def test_delete_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(4)
    bst.insert(15)
    bst.insert(7)
    bst.insert(3)
    bst.delete(8)
    assert bst.contains(8) is False
    assert bst._root == 10
Exemplo n.º 32
0
def test_delete_two_children():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(4)
    bst.insert(15)
    bst.insert(7)
    bst.insert(3)
    bst.delete(4)
    assert bst.contains(4) is False
    assert bst._nodes[7] == [3, float("inf"), 8]
    assert bst._nodes[3] == [None, float("inf"), 7]
    assert bst._nodes[8] == [7, 10, None]
Exemplo n.º 33
0
def test_balance_three_nodes():
    bst = Bst()
    bst.insert(7)
    bst.insert(8)
    bst.insert(6)
    assert bst.balance() == 0
Exemplo n.º 34
0
def test_balance():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst.balance() == 1
Exemplo n.º 35
0
def test_initalize_is_Bst():
    bst = Bst()
    assert isinstance(bst, Bst)
Exemplo n.º 36
0
def test_contains_false():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(2) is False
Exemplo n.º 37
0
def test_insert_smaller_and_bigger_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    assert bst._nodes[bst._root] == [3, 10, None]
Exemplo n.º 38
0
def test_depth():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst.depth() == 3
Exemplo n.º 39
0
def test_contains_true():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(1) is True
Exemplo n.º 40
0
def test_insert_smaller_and_bigger_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    assert bst._nodes[bst._root] == [3, 10, None]
Exemplo n.º 41
0
def test_size():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(3)
    bst.insert(6)
    bst.insert(7)
    bst.insert(9)
    assert bst.size() == 6
Exemplo n.º 42
0
def test_insert_smaller_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(3)
    assert bst._root == 8
Exemplo n.º 43
0
def test_initialize_empty():
    bst = Bst()
    assert bst._nodes == {}
Exemplo n.º 44
0
def test_contains_false():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(2) is False
Exemplo n.º 45
0
def test_balance_long_right_side():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    bst.insert(11)
    bst.insert(13)
    bst.insert(15)
    bst.insert(19)
    assert bst.balance() == 5
Exemplo n.º 46
0
def test_balance_three_nodes():
    bst = Bst()
    bst.insert(7)
    bst.insert(8)
    bst.insert(6)
    assert bst.balance() == 0