import time # Importing a binary search Tree: # because with a binary search we can allocate the element in a dynamic way and also we have # methods like contains that help us to compare one elelemnt with other from binary_search import BinarySearchTree # Initialize the binary tree with something! bst = BinarySearchTree("Bucaramanga") start_time = time.time() f = open('names_1.txt', 'r') names_1 = f.read().split("\n") # List containing 10000 names f.close() f = open('names_2.txt', 'r') names_2 = f.read().split("\n") # List containing 10000 names f.close() duplicates = [] # Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements # The first thing to do is add all the element of the list names_1 to the binary searche tree # because we nedd a base to compare all the elements of the list name_2 for name in names_1: bst.insert(name) for name2 in names_2: if bst.contains(name2): duplicates.append(name2)
#https://www.cs.usfca.edu/~galles/visualization/BST.html from binary_search import BinarySearchTree tree = BinarySearchTree() tree.add(8) tree.add(10) tree.add(3) tree.add(14) tree.add(13) tree.add(1) tree.add(6) tree.add(4) tree.add(7) print('Order') tree.show_in_order(tree.root) print('Pos-order') tree.show_pos_order(tree.root) print('pre -order') tree.show_pre_order(tree.root) #print(tree.search(tree.root, 10).value) #print(tree.search(tree.root, 10).is_left) #print(tree.search(tree.root, 10).parent.value) """ En orden izquierda - raiz - derecha # 1, 3, 4, 6, 7, 8, 10, 13, 14 """
# Replace the nested for loops below with your improvements # for name_1 in names_1: # for name_2 in names_2: # if name_1 == name_2: # duplicates.append(name_1) # --------------------------oops... not supposed to use a dictionary...------- # ref_dict = {} # for name in names_1: # ref_dict[name] = name # for name in names_2: # if ref_dict.get(name): # duplicates.append(name) # --------------------------- finished before realizing dictionary use is prohibited ----------- binary_reference = BinarySearchTree(names_1[0]) for i in range(1, len(names_1)): binary_reference.insert(names_1[i]) for name in names_2: if binary_reference.contains(name): duplicates.append(name) end_time = time.time() print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") print(f"runtime: {end_time - start_time} seconds") # ---------- Stretch Goal ----------- # Python has built-in tools that allow for a very efficient approach to this problem # What's the best time you can accomplish? Thare are no restrictions on techniques or data # structures, but you may not import any additional libraries that you did not write yourself.
from binary_search import BinarySearchTree bst = BinarySearchTree() bst.add(10, "A value") bst.add(20, "B value") bst.add(5, "A value") bst.add(12, "D value") bst.add(30, "E value") bst.add(2, "G value") bst.add(35, "F value") print("The inorder traversal of the tree is") print(bst.inorder_walk()) print("The preorder traversal of the tree is") print(bst.preorder_walk()) print("The postorder traversal of the tree is") print(bst.postorder_walk()) print(bst.smallest(), "is the smallest") print(bst.largest(), "is the largest") x = int(input("enter the no. u want to search? ")) bst.search(x) del_value = int(input("enter the key u want to delte.")) bst.delete(del_value) print("after deletion the search tree becomes.. ", bst.inorder_walk())
import time import re from binary_search import BinarySearchTree start_time = time.time() f = open('names/names_1.txt', 'r') names_1 = f.read().split("\n") # List containing 10000 names f.close() f = open('names/names_2.txt', 'r') names_2 = f.read().split("\n") # List containing 10000 names f.close() duplicates = [] search_tree = BinarySearchTree() for name in names_1: search_tree.insert(name) for name in names_2: found = search_tree.lookup(name) if found: duplicates.append(found) end_time = time.time() print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") print(f"runtime: {end_time - start_time} seconds")
def test_bst(self): bst = BinarySearchTree() bst.addNode(20, "Computer") self.assertEqual(bst.size(), 1) bst.addNode(10, "laptop") self.assertEqual(bst.size(), 2) bst.addNode(15, "PDA") self.assertEqual(bst.size(), 3) bst.addNode(40, "android") self.assertEqual(bst.size(), 4) self.assertListEqual(bst.inOrderTraverse(), [10, 15, 20, 40]) self.assertListEqual(bst.preOrderTraverse(), [20, 10, 15, 40]) self.assertListEqual(bst.postOrderTraverse(), [10, 15, 40, 20])
def test_bestTest(self): bst=BinarySearchTree() #Test 'add' and 'size' bst.add(10,"A value") self.assertEqual(bst.size(),1) bst.add(5,"A value") self.assertEqual(bst.size(),2) bst.add(30,"A value") self.assertEqual(bst.size(),3) #Test 'inorder_walk' self.assertListEqual(bst.inorder_walk(),[5,10,30]) bst.add(15,"value") self.assertListEqual(bst.inorder_walk(),[5,10,15,30]) #Test 'preorder_walk' self.assertListEqual(bst.preorder_walk(),[10,5,30,15]) #Test 'postorder_walk' self.assertListEqual(bst.postorder_walk(),[5,15,30,10]) #test 'smallest' self.assertEqual(bst.smallest(),5) #test 'largest' self.assertEqual(bst.largest(),30) #test 'search' #the output itself gives the test. since the value is not returned, this test case cannot be applied #also delete implements search #test 'delete' bst.delete(5) self.assertListEqual(bst.inorder_walk(),[10,15,30])
import time from binary_search import BinarySearchTree """ original runtime complexity on my laptop: runtime: 13.456114292144775 seconds """ # Initialize the binary tree: bst = BinarySearchTree("dupes") # Looks like we need a value inside () here start_time = time.time() f = open('names_1.txt', 'r') names_1 = f.read().split("\n") # List containing 10000 names f.close() f = open('names_2.txt', 'r') names_2 = f.read().split("\n") # List containing 10000 names f.close() """ 0:29:00 mark from Thursday's lecture: https://youtu.be/_2gJLjhquXE In BST, "contains" returns TRUE if the tree contains the value. So it will run """ duplicates = [] # Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements """ Since we're dealing with dupes, let's set names_1 as our base case We add elements of list names_1 to the BST Then compare all elements of the list to name_2
from binary_search import BinarySearchTree bst = BinarySearchTree() bst.insert(4) bst.insert(7) bst.insert(6) bst.insert(3) bst.insert(5) bst.insert(2) print('The binary search tree is:') bst.print_tree() print('The maximum value in the tree is:') print(bst.max().val) print('The minimum value in the tree is:') print(bst.min().val) print('The successor of 3 is:') print(bst.successor(bst.search(3)).val) print('The successor of 4 is:') print(bst.successor(bst.search(4)).val) bst.delete(bst.search(4)) print('The successor of 3 after deleting 4 is:') print(bst.successor(bst.search(3)).val)