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