Beispiel #1
0
def main(search_name):

    ## Searches for the node named search_name in the instantiated MyTree object using
    ## MyTree function find_node.  Prints information about the node if found,
    ## as well as names of nodes visited during the search.

    ## Instantiate nodes
    e1 = MyNode("E1", [])
    find_me = MyNode("FindMe", [])
    c1 = MyNode("C1", [])
    d1 = MyNode("D1", [e1])
    b1 = MyNode("B1", [find_me])
    b2 = MyNode("B2", [c1])
    a1 = MyNode("A1", [d1])
    a2 = MyNode("A2", [b1, b2])
    start = MyNode("Start", [a1, a2])

    ## Instatiate tree
    tree_nodes = [start, a1, a2, d1, b1, b2, e1, find_me, c1]
    my_tree = MyTree(start, tree_nodes)

    ## Find the node named search_name
    target_node = my_tree.find_node(search_name)

    ## If found, return node.  Otherwise return null.
    ## In either case, print result.
    if target_node:
        print("\nThe node named " + search_name + " was found.")
        print(target_node)
    else:
        print("\nNo node named " + search_name + " was found.")
Beispiel #2
0
 def test_insert(self):
     try:
         tree_data = "BJQKFAC"
         my_tree = MyTree("D", tree_data)
         for i, c in enumerate(tree_data):
             my_tree.insert(c)
     except ValueError as e:
         self.fail("Unable to insert tree data: {}".format(e))
Beispiel #3
0
 def test_walk(self):
     try:
         tree_data = "BJQKFAC"
         my_tree = MyTree("D", tree_data)
         for i, c in enumerate(tree_data):
                 my_tree.insert(c)
         my_tree.walk()
     except Exception as e:
         self.fail("Unable to walk through the tree: {}".format(e))
Beispiel #4
0
 def test_existing_key(self):
     try:
         tree_data = "BJQKFAC"
         my_tree = MyTree("D", tree_data)
         for i, c in enumerate(tree_data):
                 my_tree.insert(c)
         my_tree.find("J")
     except Exception as e:
         self.fail("Unable to find existing key: {}".format(e))
Beispiel #5
0
from mytree import MyTree, insert, in_order_print, pre_order_print, create_tree

with open("words_42.txt") as file:
    data = file.readlines()

words = []

for i in range(len(data) - 1):
    data[i] = data[i][:len(data[i]) - 1]
    words.append(data[i])

print(words)
b = MyTree(words)
insert(b, words)
tree = create_tree(words)
in_order_print(tree)
# pre_order_print(words)
Beispiel #6
0
from mytree import MyTree

heap = []
tree = MyTree()
tree.push_heap(heap, 1)
tree.push_heap(heap, 3)
tree.push_heap(heap, 33)
tree.push_heap(heap, 5)
tree.push_heap(heap, 7)
tree.push_heap(heap, 3)
print(heap)
tree.get_len(heap)

with open("words_19.txt", 'r') as f:
    lines = f.readlines()
    tree.heap_sort(lines)
Beispiel #7
0
from mytree import MyTree


#for heap
def heapsort(tree):
    """
    accepts tree
    """
    pass


t = MyTree()
with open("words_39.txt", 'r') as f:
    lines = f.readlines()
    for word in lines:
        print(word)
        t.insert(word)
Beispiel #8
0
from mytree import MyTree


def heapsort(tree):
    l = []
    while tree.length() != 0:
        l.append(tree.delete())
    return l[::-1]


t = MyTree()
with open("words_33.txt", "r") as f:
    lines = f.readlines()
    for word in lines:
        t.insert(word[:-1])

    array = heapsort(t)

for word in array:
    print(word)
Beispiel #9
0
 def test_missing_key(self):
     tree_data = "BJQKFAC"
     my_tree = MyTree("D", tree_data)
     for i, c in enumerate(tree_data):
             my_tree.insert(c)
     self.assertRaises(KeyError, my_tree.find, "P")
Beispiel #10
0
from mytree import MyTree

def heapsort (t):
	r = []
	while t.length() != 0:
		r.append(t.delete())
	return r[::-1] 

d = MyTree()
with open("words_1.txt","r") as f:
    lines = f.readlines()
    for word in lines:
        d.insert(word[:-1])
        
    a = heapsort(d)
    
for i in a:
	print(a)