def test_create_niveau(self): content = [["Plaats", "School", "Niveau", "Leerling", "Score", "Label"], ["Apeldoorn", "HAN", "HBO", "563631", "15", "test1"], ["Arnhem", "HAN", "HBO", "563631", "15", "test1"]] niveau = Level() binary_search_tree = BinarySearchTree() binary_search_tree.insert(content[1][0], 0) binary_search_tree.insert(content[2][0], 1) expected_niveau = "Plaats" niveau.create_level(content, content[0][0]) self.assertEqual(expected_niveau, niveau.name) self.assertEqual(binary_search_tree.find('Apeldoorn').key, niveau.variables.find('Apeldoorn').key) self.assertEqual(binary_search_tree.find('Arnhem').key, niveau.variables.find('Arnhem').key)
class TestBinarySearchTree(unittest.TestCase): def setUp(self): self.my_bst = BinarySearchTree() self.tree_values = [25, 50, 12, 35, 5, 60, 17, 1] for value in self.tree_values: self.my_bst.insert(value) def test_exceptions(self): self.assertRaises(AttributeError, self.my_bst.insert, 50) self.assertRaises(AttributeError, self.my_bst.insert, 12) self.assertRaises(AttributeError, self.my_bst.insert, 35) self.assertRaises(AttributeError, self.my_bst.insert, 1) def test_tree_operations(self): self.assertEquals(self.my_bst.find(13), False) self.assertEquals(self.my_bst.find(27), False) for val in self.tree_values: self.assertTrue(self.my_bst.find(val)) self.assertIsNone(self.my_bst.delete(12))
def test_find(self): key1 = 2 key2 = 1 key3 = 0 value1 = 1 value2 = 2 binary_search_tree = BinarySearchTree() binary_search_tree.insert(key1, value2) binary_search_tree.insert(key2, value1) binary_search_tree.insert(key3, value1) result = binary_search_tree.find(key1) self.assert_found(key1, result)
def benchmarking(): eBookEntry.__hash__ = monkey_hash # eBookEntry.set_sort_type(eBookEntry.Sort.AUTHOR) eBookEntry.set_sort_type(eBookEntry.Sort.ID) my_books = eBookEntryReader("catalog-short4.txt") my_hash_table = HashQP() bst = BinarySearchTree() avl = AVLTree() random_ids = random.choices([book.ID for book in my_books], k=10000) qp_res = [] avl_res = [] bst_res = [] for book in my_books: my_hash_table.insert(book) bst.insert(book) avl.insert(book) qp_time1 = time.time() for i in random_ids: # print(my_hash_table.find(i)) book = my_hash_table.find(i) qp_res.append(book) qp_time2 = time.time() print('QP time: ', qp_time2 - qp_time1) avl_time1 = time.time() for i in random_ids: # print(avl.find(i)) book = avl.find(i) avl_res.append(book) avl_time2 = time.time() print('AVL time: ', avl_time2 - avl_time1) bst_time1 = time.time() for i in random_ids: # print(bst.find(i)) book = bst.find(i) bst_res.append(book) # check if all items are same bst_time2 = time.time() print('BST time: ', bst_time2 - bst_time1) # test to see if all the finds were the same print(bst_res == avl_res) print(bst_res == qp_res)
class SymbolTable: def __init__(self): self.__binarySearchTree = BinarySearchTree() def insert(self, value): return self.__binarySearchTree.insert(value) def find(self, value): return self.__binarySearchTree.find(value) def __str__(self): s = 'Symbol Table:\n' for i in range(len(self.__binarySearchTree.nodes)): if self.__binarySearchTree.nodes[i] is not None: s += str(i + 1) + ' ' + self.__binarySearchTree.nodes[i] + '\n' return s
def main(): #open the list of names file and read the student info then put it into a list inputfile = str(input("Please enter a list of names file ")) inputfilehandle = open(inputfile, 'r') inputfileread = inputfilehandle.readlines() # BST = BinarySearchTree() BST = BinarySearchTree() #loops through and adds Student record to BinarySearchTree #create a start time for the creater starttime = time.time() #find out how many records your inserting into insertrecordcounter = 0 for line in inputfileread: ssline = line.strip(" ").split('\t') #changes the variable to an int sname = int(ssline[0]) BST.insert(Student(sname)) insertrecordcounter += 1 inputfilehandle.close() endtime = time.time() print(BST) #create a start time for the creater # asks for a second file, and reads it inputfile2 = str(input("Please enter a list of names file ")) sfilehandle = open(inputfile2, 'r') Idsearch = sfilehandle.readlines() #create a start time for the creater starttime2 = time.time() findrecordcounter = 0 # search for an ID match in the search file and BinarySearchTree for ID in range(len(Idsearch)): findrecordcounter += 1 num = Idsearch[ID] # change it to an integar IDcomparison = int(num) studentID = Student(IDcomparison) #finds the cross-over IDchecker = BST.find(studentID) #checks to see if the ID was found or not and prints it if IDchecker is not None: print("Found student record " + str(IDchecker)) else: print("Student ID " + str(IDcomparison) + " not found") #create a start time for the creater sfilehandle.close() endtime2 = time.time() print("Time to insert " + str(insertrecordcounter) + " records into an AVL Tree: " + format(endtime - starttime, "6.4f") + " seconds") print("Time to find " + str(findrecordcounter) + " records into an AVL Tree: " + format(endtime2 - starttime2, "6.4f") + " seconds")
def testTreeFind(self): bst = BinarySearchTree() self.assertFalse(bst.find(5)) bst.insert(5) self.assertTrue(bst.find(5))
from BinarySearchTree import BinarySearchTree tree = BinarySearchTree() tree.add(4) tree.add(2) tree.add(1) tree.add(3) tree.add(8) tree.add(6) tree.add(5) tree.add(7) tree.in_order_travers() print("search result = ", tree.find(6)) print("remove result = ", tree.remove(6)) tree.in_order_travers()
def test_find_non_empty_tree(self): BST = BinarySearchTree(51) self.assertNotEqual(BST.find(51), None)
def test_find_empty_tree(self): BST = BinarySearchTree() self.assertEqual(BST.find(11), None)