Ejemplo n.º 1
0
    if (min_value is not None and tree.value <= min_value
            or max_value is not None and tree.value >= max_value):
        return False

    if not is_bst_recursive_solution(
            tree.left, min_value, tree.value) or not is_bst_recursive_solution(
                tree.right, tree.value, max_value):
        return False

    return True


def copy_bst(tree, array=None):
    if not tree:
        return

    if array is None:
        array = []

    copy_bst(tree.left, array)
    array.append(tree.value)
    copy_bst(tree.right, array)
    return array


if __name__ == "__main__":
    tree = build_tree(list(range(0, 101)), 0, 99)
    print("Solution -> ", is_bst_copying_solution(tree))
    print("Solution -> ", is_bst_recursive_solution(tree))
Ejemplo n.º 2
0
    while to_visit:
        root = to_visit.pop()

        if not root:
            continue

        nodes.append(root)
        to_visit.append(root.left)
        to_visit.append(root.right)

    random_index = random.randint(0, len(nodes))
    return nodes[random_index]


if __name__ == "__main__":
    root = build_tree(range(0, 100), 0, 99)

    tree = TreeNode(root.value)
    to_visit = [root]
    while to_visit:

        node = to_visit.pop()

        if node.left:
            tree.insert_in_order(node.left.value)
            to_visit.append(node.left)

        if node.right:
            tree.insert_in_order(node.right.value)
            to_visit.append(node.right)
Ejemplo n.º 3
0
    if larger.value == smaller.value and match_tree(larger, smaller):
        return True

    return is_subtree(larger.left, smaller) or is_subtree(
        larger.right, smaller)


def match_tree(larger, smaller):
    # Nothing left in the subtree
    if not larger and not smaller:
        return True

    # Exactly one tree is empty, therefore trees do not match
    if not larger or not smaller:
        return False

    # Data does not match
    if larger.value != smaller.value:
        return False

    return match_tree(larger.left, smaller.left) and match_tree(
        larger.right, smaller.right)


if __name__ == "__main__":
    larger = build_tree(range(0, 1000), 0, 999)
    # Create a smaller tree matching a root from larger tree
    smaller = build_tree(range(0, 249), 0, 248)

    print(is_subtree(larger, smaller))
Ejemplo n.º 4
0
            result.extend(weaved)

    return result


def weave_lists(first, second, results, prefix):
    if not first or not second:
        result = prefix[:]
        result.extend(first)
        result.extend(second)
        results.append(result)
        return results

    head = first.pop(0)
    prefix.append(head)
    weave_lists(first, second, results, prefix)
    prefix.pop()
    first.insert(0, head)

    head_second = second.pop(0)
    prefix.append(head_second)
    weave_lists(first, second, results, prefix)
    prefix.pop()
    second.insert(0, head)


if __name__ == "__main__":
    array = [3, 6, 7, 8, 12, 14, 16]
    tree = build_tree(array, 0, len(array) - 1)
    print(solution(tree))
Ejemplo n.º 5
0
    current = []
    current.append(tree)

    while len(current) > 0:
        lists.append(current)
        previous = current

        current = []

        for node in previous:
            if node.left:
                current.append(node.left)

            if node.right:
                current.append(node.right)

    return lists


if __name__ == "__main__":
    tree = build_tree(list(range(1, 1001)), 0, 999)

    results = dfs_solution(tree)
    print("Depths => ", len(results))
    print([(index, len(depth)) for index, depth in enumerate(results)])

    results = bfs_solution(tree)
    print("Depths => ", len(results))
    print([(index, len(depth)) for index, depth in enumerate(results)])