예제 #1
0
def main():
    global vis
    default_tree = [5, 6, 1, 2, 5, 8, 3, 10, 12, 13, 15]
    default_tree_two = [5, 4, 3, 2, 1, 6, 7, 8, 9, 10]

    tree = BST()
    [tree.insert(node) for node in default_tree]

    print "{:11s}: {}".format("Traversal", "Sequence")
    print "-" * 45
    run = lambda f, root: globals()[f](root)
    for traversal in traversals:
        run(traversal, tree.root)
        print "{:11s}: {}".format(traversal, ', '.join([str(x) for x in vis]))
        vis = []
예제 #2
0
파일: main.py 프로젝트: iashreya/Algorithms
from tree import BST

tree = BST()
tree.insert(9)
tree.insert(4)
tree.insert(6)
tree.insert(20)
tree.insert(170)
tree.insert(15)
tree.insert(1)
tree.insert(7)
tree.insert(10)

tree.traverse(tree.root)
예제 #3
0
from tree import Node

print(
    "Please enter the relative route to the text file you would like to analyze."
)
path = raw_input("> ")

tree = BST()

with open(path, "r") as input_file:  #open file
    for i, line in enumerate(input_file):  # loop through every line
        line = line.replace('.', '')  #make every word delimator a space
        line = line.replace(',', '')
        line = line.replace('?', '')
        line = line.replace('\n', '')

        if line[0] != "#":
            words = line.split(" ")  #split on spaces
            for word in words:  #\ #for every word
                if not word[0] in [
                        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
                ]:
                    word = word[:10]  #only keep first ten letters
                    tree.insert({
                        "word": word,
                        "lines": [i + 1]
                    })  #insert into tree

tree.display()
# tree.output()
예제 #4
0
from tree import BST
tree = BST()
tree.insert(5)
tree.insert(4)
tree.insert(6)
tree.insert(10)
tree.insert(9)
tree.insert(11)

#tree.find(5)
tree.delete_value(5)
tree.printBST()
in_array = BST.inorder(tree.root)
print(in_array)

tree2 = BST()
tree2.insert(10)
tree2.insert(8)
tree2.insert(7)
tree2.insert(6)
tree2.insert(5)

in_array = BST.inorder(tree2.root)
print(in_array)
bTreeRoot = BST.getBalancedBST(in_array, 0, len(in_array) - 1)
pre_array = BST.preorder(bTreeRoot)
print(pre_array)
print(bTreeRoot.value)  # 7
myBalancedBST = BST()
myBalancedBST.root = bTreeRoot  # now is saved as proper tree