Exemplo n.º 1
0
def test_random_node__simple():
    values = [1, 2, 3, 4, 5, 6, 7, 8]
    root = to_bst(values)
    tree = RandomTree(root)

    for index in range(0, len(values)):
        with patch('exercises.trees_graphs.random_node.randrange', return_value=index):  # noqa: E501
            found = tree.get_random_node()
            # broke encapsulation here, difficult to test
            assert list(tree._nodes)[index] == found
Exemplo n.º 2
0
def test_to_bst__many_odd():
    array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    root = to_bst(array)
    traversal = root.traverse_inorder()
    assert list(map(lambda n: n.value, traversal)) == array
    assert root.height <= maximum_height(len(array))
Exemplo n.º 3
0
def test_to_bst__one():
    array = [1]
    traversal = to_bst(array).traverse_inorder()
    assert list(map(lambda n: n.value, traversal)) == array
Exemplo n.º 4
0
def test_to_bst__nothing():
    assert to_bst([]) is None
Exemplo n.º 5
0
def test_is_bst__complex():
    root = to_bst([1, 2, 3, 4, 5, 6, 7, 8])
    assert is_bst(root)
Exemplo n.º 6
0
def test_to_linked_lists__one():
    lists = to_linked_lists(to_bst([9000]))
    assert len(lists) == 1
    assert len(lists[0]) == 1
    assert lists[0][0] == 9000
Exemplo n.º 7
0
def test_to_linked_lists__many():
    lists = to_linked_lists(to_bst([1, 2, 3, 4, 5, 6, 7, 8]))
    assert len(lists) == 4

    assert [1, 1, 2, 4] == sorted([len(li) for li in lists])