Esempio n. 1
0
def construct_binary_tree():
    # initialize root node of a tree
    root = Node(1)

    # initialize a binary tree
    tree = binary_tree.Tree(root)

    root.left = Node(2)
    root.right = Node(3)
    root.right.left = Node(4)
    root.right.right = Node(5)
    root.right.left.right = Node(6)

    return tree
def construct_tree(preorder, maximum, minimum, key):

    global preIndex

    if preIndex > len(preorder):
        return

    if key > minimum and key < maximum:

        root = binary_tree.Node(key)

        preIndex += 1

        if preIndex < len(preorder):

            root.left = construct_tree(preorder, root.data, minimum,
                                       preorder[preIndex])

            root.right = construct_tree(preorder, maximum, root.data,
                                        preorder[preIndex])

        return root


if __name__ == "__main__":

    root = construct_tree([3, 2, 1, 5, 4, 6], 100000, -100000, 3)

    tree = binary_tree.Tree(root)

    tree.inorder(root)
def rand_tree():
    tree = bt.Tree()
    for i in range(25):
        tree.add(randint(1, 50))
    return tree
def empty_tree():
    tree = bt.Tree()
    return tree