Beispiel #1
0
def main():
    tree = BST()
    N = 11
    _set = set([random.randint(1, N * N) for _ in range(N)])
    for x in _set:
        tree.insert(x, "")

    print "original tree"
    tree.pretty_print()

    inorder = [node.key for node in tree.inorder_nodes()]
    preorder = [node.key for node in tree.preorder_nodes()]

    # now build a new tree from these traversals
    root = binary_tree_from_traversals(preorder, inorder)
    new_tree = BST()
    new_tree.root = root
    print "reconstructed tree"
    new_tree.pretty_print()

    # verify the correctness
    for orig_node, cloned_node in zip(tree.levelorder_nodes(),
                                      new_tree.levelorder_nodes()):
        assert orig_node is not cloned_node
        assert orig_node.key == cloned_node.key
def main():
    N = 22
    arr = sorted([random.randint(1, N * N) for _ in range(N)])
    print arr
    root = balanced_bst_from_sorted_array(arr, 0, len(arr) - 1)
    tree = BST()
    tree.root = root
    tree.pretty_print()

    inorder = [node.key for node in tree.inorder_nodes()]
    assert arr == inorder