Esempio n. 1
0
def test_is_balanced():
    tree1 = TreeNode.from_list([
        [0],
        [1, None],
        [2, None, None, None]])

    assert not is_balanced(tree1)

    tree2 = TreeNode.from_list([
        [0],
        [1, 1],
        [2, None, None, None]])

    assert is_balanced(tree2)

    tree3 = TreeNode.from_list([
        [0],
        [1, 1],
        [2, None, None, None],
        [3, None, None, None, None, None, None, None]])

    assert not is_balanced(tree3)

    tree4 = TreeNode.from_list([
        [0],
        [1, 1],
        [2, 2, 2, None],
        [3, None, None, None, None, None, None, None]])

    assert is_balanced(tree4)
Esempio n. 2
0
def test_is_bst():
    tree1 = TreeNode.from_list([])

    assert is_bst(tree1)

    tree2 = TreeNode.from_list([
        [1]
    ])

    assert is_bst(tree2)

    tree3 = TreeNode.from_list([
        [1],
        [0, 2]
    ])

    assert is_bst(tree3)

    tree4 = TreeNode.from_list([
        [1],
        [1, 2]
    ])

    assert is_bst(tree4)

    tree5 = TreeNode.from_list([
        [1],
        [2, 2]
    ])

    assert not is_bst(tree5)

    tree6 = TreeNode.from_list([
        [1],
        [None, 2],
        [None, None, None, 3]
    ])

    assert is_bst(tree6)