Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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]
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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]
Пример #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
Пример #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]
Пример #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
Пример #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]
Пример #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]
Пример #21
0
def test_insert_bigger_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    assert bst._root == 8
Пример #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
Пример #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]
Пример #24
0
def test_contains_true():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(1) is True
Пример #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]
Пример #26
0
def test_insert_no_root():
    bst = Bst()
    bst.insert(8)
    assert bst._root == 8
Пример #27
0
def test_insert_bigger_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(10)
    assert bst._root == 8
Пример #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]
Пример #29
0
def test_insert_smaller_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(3)
    assert bst._root == 8
Пример #30
0
def test_insert_no_root():
    bst = Bst()
    bst.insert(8)
    assert bst._root == 8
Пример #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
Пример #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]
Пример #33
0
def test_balance_three_nodes():
    bst = Bst()
    bst.insert(7)
    bst.insert(8)
    bst.insert(6)
    assert bst.balance() == 0
Пример #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
Пример #35
0
def test_initalize_is_Bst():
    bst = Bst()
    assert isinstance(bst, Bst)
Пример #36
0
def test_contains_false():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(2) is False
Пример #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]
Пример #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
Пример #39
0
def test_contains_true():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(1) is True
Пример #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]
Пример #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
Пример #42
0
def test_insert_smaller_than_root():
    bst = Bst()
    bst.insert(8)
    bst.insert(3)
    assert bst._root == 8
Пример #43
0
def test_initialize_empty():
    bst = Bst()
    assert bst._nodes == {}
Пример #44
0
def test_contains_false():
    bst = Bst()
    bst._nodes[1] = (0, None, 2)
    assert bst.contains(2) is False
Пример #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
Пример #46
0
def test_balance_three_nodes():
    bst = Bst()
    bst.insert(7)
    bst.insert(8)
    bst.insert(6)
    assert bst.balance() == 0