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
def main(): tree = BST() N = 11 _set = set([random.randint(1, N * N) for _ in range(N)]) # _set = [50, 6, 33, 20, 35, 36] for x in _set: tree.insert(x, "") # just set the root to 0 so the full tree is not BST tree.root.key = 0 tree.pretty_print() print "largest bst with nodes: %d" % largest_bst(tree.root)
def main(): tree = BST() N = 6 _set = set([random.randint(1, N * N) for _ in range(N)]) for x in _set: tree.insert(x, "") tree.pretty_print() mirror_image(tree.root) print "" print "after inverting" print "" tree.pretty_print()
def main(): tree = BST() N = 10 _set = set([random.randint(1, N * N) for _ in range(N)]) for x in _set: tree.insert(x, "") tree.pretty_print() postorder = [node.key for node in tree.postorder_nodes()] print postorder postorder_iterator = PostorderIterator(tree.root) idx = 0 while postorder_iterator.has_next(): postorder_iterator.next() is postorder[idx] idx += 1