def main(argv): input_file = argv[1] # Instance of the binary search tree tree = BST() with open(input_file, 'r') as file_ob: for line in file_ob: line = line.split() if line[0] == 'insert': tree.insert(int(line[1])) if line[0] == 'delete': tree.delete(int(line[1])) if line[0] == 'preorder': tree.traverse('preorder', tree.getRoot()) print("") if line[0] == 'inorder': tree.traverse('inorder', tree.getRoot()) print("") if line[0] == 'postorder': tree.traverse('postorder', tree.getRoot()) print("") # Close the file file_ob.close()
def main(): #create BST: print('create BST with elements: 10, 5, 2, 6, 1, 4, 20, 15, 25') bst = BST(10) insert_in_BST = [5, 2, 6, 1, 4, 20, 15, 25] for item in insert_in_BST: bst.insert(item) # test find() -> find root with key = 2 and print key: print('find root with key=2 and print key:') print(bst.find(2).key) # find min/max: print('find node with min key and print key (should be 1):') print(bst.findMin().key) print('find node with max key and print key (should be 25):') print(bst.findMax().key) # traverse tree / delete nodes: print('traverse tree in order and print all keys:') bst.traverseInOrder() print('delete node with key=5 and traverse tree again:') bst.delete(5) bst.traverseInOrder() # get root and print it's key: print('get root key (should be 10)') print(bst.getRoot().key)
def main(argv): #create a new tree tree = BST() #open file argument file = open(argv[1], "r") # goes for amount of of lines in f for line in file: text = line.split() #test cases for what the first element in the list is if text[0] == "insert": #saves the second element, since it exists data = text[1] tree.insert(data) elif text[0] == "delete": #saves element, then calls delete data = text[1] tree.delete(data) elif text[0] == "preorder" or text[0] == "postorder" or text[0] == "inorder": #tests for if a traverse then calls the function tree.traverse(text[0], tree.getRoot()) else: print("incorrect format") return 0
def main(argv): myBST = BST() # Loop over input file (filename passed via argv). input_file = argv[1] with open(input_file, 'r') as file_ob: # Split each line into a task and number (if one exists) #hint: use the split method for strings # Perform the function corresponding to the specified task # i.e., insert, delete, inorder, preorder, postorder # Close the file when you're done. for line in file_ob: if ' ' in line: command, num = line.split() if command == "insert": myBST.insert(num) if command == "delete": myBST.deleteBook(num) #print command #print num else: line = line.split() myBST.traverse(line[0], myBST.getRoot()) print '' if 'str' in line: break pass
def main(argv): tree = BST( ) # Creating the instance of a binary search tree, and going to be # referring to it as tree. input_file = argv[ 1] # This will be the input file with the tree instructions # passed in as an argument when the program is compiled. with open(input_file, 'r') as file_object: # Looping over the input file (file is passed in as argv[1]) for line in file_object: # splitting each line into a task and a number, so this would mean that # the task will be refered to as line[0] and then the actual number # is going to be referred to as line[1] after the split. line = line.rstrip('\n') line = line.split() if line[0] == "insert": # calling the insert function. tree.insert((line[1])) if line[0] == "inorder": # for the tree traversals, these will only call the traverse function # which will then decide which traversal to execute. tree.traverse(line[0], tree.getRoot()) print "\n", if line[0] == "preorder": tree.traverse(line[0], tree.getRoot()) print "\n", if line[0] == "postorder": tree.traverse(line[0], tree.getRoot()) print "\n", if line[0] == "delete": tree.delete(line[1]) file_object.close() # closing the input file after we finish reading it.
def main(argv): # Loop over input file (filename passed via argv). # Split each line into a task and number (if one exists) # hint: use the split method for strings https://docs.python.org/2/library/string.html#string.split # Perform the function corresponding to the specified task # i.e., insert, delete, inorder, preorder, postorder # Close the file when you're done. filename = argv[1] file = open(filename, "r") bst = BST() for line in file: line = line.strip('\n') task = line.split(" ") # If task is insert if (task[0] == "insert"): val = int(task[1]) bst.insert(val) # If task is delete elif (task[0] == "delete"): val = int(task[1]) bst.delete(val) # If task is inorder elif (task[0] == "inorder"): bst.traverse(task[0], bst.getRoot()) # If task is preorder elif (task[0] == "preorder"): bst.traverse(task[0], bst.getRoot()) # If task is postorder elif (task[0] == "postorder"): bst.traverse(task[0], bst.getRoot()) # Close file file.close()
def main(argv): # Loop over input file (filename passed via argv). # Split each line into a task and number (if one exists) # hint: use the split method for strings https://docs.python.org/2/library/string.html#string.split # Perform the function corresponding to the specified task # i.e., insert, delete, inorder, preorder, postorder # Close the file when you're done. file = argv[1] tasklist = [] tree = BST() with open(file, 'r') as file_ob: for line in file_ob: tasks = line.strip() #strips whitespace calls = tasks.split( " ") #separates commands (some require 2 arguments) tasklist.append(calls) for task in tasklist: #these types of commands are the traversals inorder, preorder, postorder if task[0] == "inorder": tree.traverse("inorder", tree.getRoot()) print("") # just to make my output a little more readable elif task[0] == "preorder": tree.traverse("preorder", tree.getRoot()) print("") # just to make my output a little more readable elif task[0] == "postorder": tree.traverse("postorder", tree.getRoot()) print("") # just to make my output a little more readable elif len(task) == 2: command = task[0] #here are the insert/delete commands value = int(task[1]) if command == "insert": tree.insert(value) elif command == "delete": tree.delete(value) else: print("this is not an appropriate BST method")
def testPut(self): tree = BST() keys = [10,5,15,2,7,12,20] vals = ["a","b","c","d","e","f","g","h","i"] #function that calls put continually on set of keys/vals tree.makeBST(keys,vals) #check manually if all heys have been put in right place by manully traversinf left/ right pointers self.assertEqual(tree.getRoot().getKey(), 10) self.assertEqual(tree.getRoot().getLeft().getKey(), 5) self.assertEqual(tree.getRoot().getLeft().getLeft().getKey(), 2) self.assertEqual(tree.getRoot().getLeft().getRight().getKey(), 7) self.assertEqual(tree.getRoot().getRight().getKey(),15) self.assertEqual(tree.getRoot().getRight().getLeft().getKey(),12) self.assertEqual(tree.getRoot().getRight().getRight().getKey(),20)
def testPutRight(self): #check the put to the right by making a linear tree to the right tree = BST() tree.put(1,"a") tree.put(2,"b") tree.put(3,"c") tree.put(4,"d") tree.put(5,"e") tree.put(6,"f") tree.put(7,"g") #check manually if all heys have been put in right place by manully traversinf left pointers self.assertEqual(tree.getRoot().getValue(), "a") self.assertEqual(tree.getRoot().getRight().getValue(), "b") self.assertEqual(tree.getRoot().getRight().getRight().getValue(), "c") self.assertEqual(tree.getRoot().getRight().getRight().getRight().getValue(), "d") self.assertEqual(tree.getRoot().getRight().getRight().getRight().getRight().getValue(), "e") self.assertEqual(tree.getRoot().getRight().getRight().getRight().getRight().getRight().getValue(), "f") self.assertEqual(tree.getRoot().getRight().getRight().getRight().getRight().getRight().getRight().getValue(), "g") #check if inserting a key again will update the associated value tree.put(2,"z") self.assertEqual(tree.getRoot().getRight().getValue(), "z") self.assertEqual(tree.getRoot().getRight().getRight().getValue(), "c")
def testPutLeft(self): #check the put to the left by making a linear tree to the left tree = BST() tree.put(7,"a") tree.put(6,"b") tree.put(5,"c") tree.put(4,"d") tree.put(3,"e") tree.put(2,"f") tree.put(1,"g") #check manually if all heys have been put in right place by manully traversinf left pointers self.assertEqual(tree.getRoot().getValue(), "a") self.assertEqual(tree.getRoot().getLeft().getValue(), "b") self.assertEqual(tree.getRoot().getLeft().getLeft().getValue(), "c") self.assertEqual(tree.getRoot().getLeft().getLeft().getLeft().getValue(), "d") self.assertEqual(tree.getRoot().getLeft().getLeft().getLeft().getLeft().getValue(), "e") self.assertEqual(tree.getRoot().getLeft().getLeft().getLeft().getLeft().getLeft().getValue(), "f") self.assertEqual(tree.getRoot().getLeft().getLeft().getLeft().getLeft().getLeft().getLeft().getValue(), "g")
def testPutRoot(self): #check if adding one node to the tree will in fact have the root pointer pointed to it tree = BST() tree.put(11,"Ashbourne") self.assertEqual(tree.getRoot().getKey(), 11)