Ejemplo n.º 1
0
def main():
    tree = LinkedBST()
    print("Adding D B A C F E G")
    tree.add("D")
    tree.add("B")
    tree.add("A")
    tree.add("C")
    tree.add("F")
    tree.add("E")
    tree.add("G")

    print("\nExpect True for A in tree: ", "A" in tree)

    print("\nString:\n" + str(tree))

    clone = LinkedBST(tree)
    print("\nClone:\n" + str(clone))

    print("Expect True for tree == clone: ", tree == clone)

    print("\nFor loop: ", end="")
    for item in tree:
        print(item, end=" ")

    print("\n\ninorder traversal: ", end="")
    for item in tree.inorder(): print(item, end=" ")

    # print("\n\npreorder traversal: ", end="")
    # for item in tree.preorder(): print(item, end = " ")

    # print("\n\npostorder traversal: ", end="")
    # for item in tree.postorder(): print(item, end = " ")

    # print("\n\nlevelorder traversal: ", end="")
    # for item in tree.levelorder(): print(item, end = " ")

    print("\n\nRemoving all items:", end=" ")
    for item in "ABCDEFG":
        print(tree.remove(item), end=" ")

    print("\n\nExpect 0: ", len(tree))

    tree = LinkedBST(range(1, 16))
    print("\nAdded 1..15:\n" + str(tree))

    lyst = list(range(1, 16))

    random.shuffle(lyst)
    tree = LinkedBST(lyst)
    print("\nAdded ", lyst, "\n" + str(tree))

    lyst = [113, 30, 68, 74, 45, 91, 88]
    # random.shuffle(lyst)
    tree = LinkedBST(lyst)
    print(tree, tree.height())
    print(tree.isBalanced())
    print(tree.rangeFind(30, 91))
    print(tree.successor(20))
    print(tree.predecessor(50))
    tree.rebalance()
    print(tree)
Ejemplo n.º 2
0
from binary_search_tree.linkedbst import LinkedBST

tree = LinkedBST()
tree.add("A")
tree.add("B")
tree.add("C")
tree.add("D")
tree.add("E")
tree.add("F")
tree.add("G")
tree.add("H")
tree.add("I")
tree.add("J")
tree.add("K")

print(tree)
print("Answer must be False: ", tree.isBalanced())
tree.rebalance()
print(tree)
print("Answer must be True: \n", tree.isBalanced())
print("Answer must be D : \n", tree.successor("C").data)
print("Answer must be B : \n", tree.predecessor("C").data)
print("Answer must be C: \n")
for i in tree.rangeFind("B", "D"):
    print(i.data)