def test_can_search(self): root = None root = bt.insert(root, 11, 1) root = bt.insert(root, 5, 6) root = bt.insert(root, 42, 21) s = bt.search(root, 5) self.assertEqual(s.value, 6)
def test_insert(self): self.tree_root = None self.tree_root = binary_tree.insert(self.tree_root, 'Cindy', 1) self.tree_root = binary_tree.insert(self.tree_root, 'Fu', 2) self.tree_root = binary_tree.insert(self.tree_root, 'Computer', 5) self.tree_root = binary_tree.insert(self.tree_root, 'Science', 10) res = binary_tree.inorder(self.tree_root) self.assertEqual(res, [1, 5, 2, 10])
def test_binary_tree_multiple_right(self): root = None root = binary_tree.insert(root, key=10) root = binary_tree.insert(root, key=12) root = binary_tree.insert(root, key=11) root = binary_tree.insert(root, key=13) self.assertEqual(root.right.right.key, 13) self.assertEqual(root.right.left.key, 11)
def test_avl_search(self): """ This functions makes sure that the search function is correctly searching the AVL for a given key and returning its value """ self.n = 10 self.avl, self.keys = self.make_avl_tree(self.n) insert(self.avl.root, 50.5, "Successful search") self.assertEqual(search(self.avl.root, 50.5), "Successful search")
def test_binary_tree_search(self): ''' Unit test for search function of binary tree ''' tree = None tree = bt.insert(tree, 5, 'val1') tree = bt.insert(tree, 6, 'val2') tree = bt.insert(tree, 4, 'val3') self.assertEqual(bt.search(tree, 5), 'val1') self.assertEqual(bt.search(tree, 6), 'val2') self.assertEqual(bt.search(tree, 4), 'val3') self.assertEqual(bt.search(tree, 0), None)
def test_search(self): self.tree_root = None self.tree_root = binary_tree.insert(self.tree_root, 'Cindy', 1) self.tree_root = binary_tree.insert(self.tree_root, 'Fu', 2) self.tree_root = binary_tree.insert(self.tree_root, 'Computer', 5) self.tree_root = binary_tree.insert(self.tree_root, 'Science', 10) res1 = binary_tree.search(self.tree_root, 'Cindy') self.assertEqual(res1.value, 1) res2 = binary_tree.search(self.tree_root, 'Fu') self.assertEqual(res2.value, 2) res3 = binary_tree.search(self.tree_root, 'aldksfjlk') self.assertEqual(res3, None)
def make_random_tree(self, n): """ This helper function makes a random BST and returns its root and keys """ self.root = Node(random.randint(0, 100), random.randint(0, 100)) self.keys = [self.root.key] for i in range(n): key, value = random.randint(0, 100), random.randint(0, 100) if key not in self.keys: self.keys.append(key) insert(self.root, key, value) return self.root, self.keys
def test_correct_search(self): for i in range(100): root = None node_1_key = rdm.randint(1, 1000) node_1_value = rdm.randint(1, 1000) node_2_key = rdm.randint(1, 1000) node_2_value = rdm.randint(1, 1000) node_3_key = rdm.randint(1, 1000) node_3_value = rdm.randint(1, 1000) root = bt.insert(root, node_1_key, node_1_value) bt.insert(root, node_2_key, node_2_value) bt.insert(root, node_3_key, node_3_value) self.assertEqual(bt.search(root, node_1_key), node_1_value) self.assertEqual(bt.search(root, node_2_key), node_2_value) self.assertEqual(bt.search(root, node_3_key), node_3_value)
def test_binary_tree_search_noright_or_left_node(self): root = None root = binary_tree.insert(root, key=10, value=10) search = binary_tree.search(root, 11) self.assertEqual(search, None) search = binary_tree.search(root, 9) self.assertEqual(search, None)
def time_unsorted(arg, unsorted_data): if arg == 'hash': table = hash_tables.LinearProbe(100000, hash_functions.h_rolling) t0 = time.time() for i in range(len(unsorted_data)): table.add(unsorted_data[i][0], unsorted_data[i][1]) t1 = time.time() elapsed_unsorted_insert = t1 - t0 if arg == 'tree': root = None t0 = time.time() for i in range(len(unsorted_data)): bt.insert(root, int(unsorted_data[i][0]), unsorted_data[i][1]) t1 = time.time() elapsed_unsorted_insert = t1 - t0 return elapsed_unsorted_insert
def test_binary_tree_insert(self): ''' Unit test for insert function of binary tree ''' tree = None tree = bt.insert(tree, 5, 'val1') tree = bt.insert(tree, 6, 'val2') tree = bt.insert(tree, 4, 'val3') tree = bt.insert(tree, 5, 'val4') self.assertEqual(tree.key, 5) self.assertEqual(tree.right.key, 6) self.assertEqual(tree.left.key, 4) self.assertEqual(tree.right.left.key, 5) self.assertEqual(tree.value, 'val1') self.assertEqual(tree.right.value, 'val2') self.assertEqual(tree.left.value, 'val3') self.assertEqual(tree.right.left.value, 'val4')
def test_correct_insert(self): for i in range(1000): root = None node_1_key = rdm.randint(1, 1000) node_1_value = rdm.randint(1, 1000) node_2_key = rdm.randint(1, 1000) node_2_value = rdm.randint(1, 1000) node_3_key = rdm.randint(1, 1000) node_3_value = rdm.randint(1, 1000) root = bt.insert(root, node_1_key, node_1_value) bt.insert(root, node_2_key, node_2_value) bt.insert(root, node_3_key, node_3_value) keylist = [node_1_key, node_2_key, node_3_key] if len(keylist) > len(set(keylist)): continue if node_2_key > node_1_key: self.assertEqual(root.right.key, node_2_key) self.assertEqual(root.right.value, node_2_value) if node_3_key > node_1_key: if node_3_key > node_2_key: self.assertEqual(root.right.right.key, node_3_key) self.assertEqual(root.right.right.value, node_3_value) else: self.assertEqual(root.right.left.key, node_3_key) self.assertEqual(root.right.left.value, node_3_value) else: self.assertEqual(root.left.key, node_3_key) self.assertEqual(root.left.value, node_3_value) else: self.assertEqual(root.left.key, node_2_key) self.assertEqual(root.left.value, node_2_value) if node_3_key > node_1_key: self.assertEqual(root.right.key, node_3_key) self.assertEqual(root.right.value, node_3_value) else: if node_3_key > node_2_key: self.assertEqual(root.left.right.key, node_3_key) self.assertEqual(root.left.right.value, node_3_value) else: self.assertEqual(root.left.left.key, node_3_key) self.assertEqual(root.left.left.value, node_3_value)
def test_incorrect_insert(self): self.assertRaises(ValueError, lambda: bt.insert('hi', 5)) self.assertRaises(ValueError, lambda: bt.insert(7, 5)) self.assertRaises(ValueError, lambda: bt.insert(float(420.69), 5)) self.assertRaises(ValueError, lambda: bt.insert([], 5)) root = None self.assertRaises(ValueError, lambda: bt.insert(root, 'hi')) self.assertRaises(TypeError, lambda: bt.insert(root, []))
def time_insert(self): ''' Depending on the structure type, add keys and values from the input ''' insert_start = time.time() i = 0 # Initialize a hash table that 5 times the size of number of # insertion to speed up the hash for l in open(self.data): if i >= self.num: break else: i += 1 pair = l.rstrip().split(",") if self.struct == 'hash': self.temp_hash.add(pair[0], pair[1]) elif self.struct == 'tree': self.temp_tree = bt.insert(self.temp_tree, pair[0], pair[1]) elif self.struct == 'avl': self.temp_avl.insert(pair) insert_end = time.time() i_time = str(insert_end - insert_start) return i_time
def test_key_is_inserted_root_is_none(self): root = None root = bt.insert(root, 11, 1) self.assertEqual(root.value, 1)
def main(data_struct, data_set, num_points): """ This function performs our experiment for an AVL tree, BST, or hash table depending on user input. A figure of search and insertion times is produced. Parameters: - data_struct(str): The type of data structure - data_set(str): The type of data set. Random or sorted - num_points(int): The number of data points from the data set that we wish to include in analysis Returns: - None, but a .png file is saved """ key_values = get_data(data_set, num_points) n = list(range(num_points)) t_add = [] t_search = [] t_missing = [] if data_struct == "avl": for num in n: t0 = time.perf_counter() avl_tree = avl.AVL() for k, _ in key_values: avl_tree.insert(k) t1 = time.perf_counter() t_add.append(t1-t0) t0 = time.perf_counter() for k, _ in key_values[0:int(.1*num_points)]: avl_tree.find(str(k)) t1 = time.perf_counter() t_search.append(t1-t0) t0 = time.perf_counter() for k in range(num_points, int(num_points*1.1)): avl_tree.find(str(k)) t1 = time.perf_counter() t_missing.append(t1-t0) make_plot(t_add, t_search, t_missing, "AVL performance", "avl_test.png") elif data_struct == "hash": for num in n: t0 = time.perf_counter() ht = LinearProbe(5*num_points, h_rolling) for k, v in key_values: ht.add(str(k), v) t1 = time.perf_counter() t_add.append(t1-t0) t0 = time.perf_counter() for k, _ in key_values[0:int(.1*num_points)]: ht.search(str(k)) t1 = time.perf_counter() t_search.append(t1-t0) t0 = time.perf_counter() for k in range(num_points, int(num_points*1.1)): ht.search(str(k)) t1 = time.perf_counter() t_missing.append(t1-t0) make_plot(t_add, t_search, t_missing, "Hashing performance", "hash_test.png") elif data_struct == "tree": for num in n: t0 = time.perf_counter() root_key = key_values[0][0] root_val = key_values[0][1] root = Node(root_key, root_val) for k, v in key_values[1:]: insert(root, k, v) t1 = time.perf_counter() t_add.append(t1-t0) t0 = time.perf_counter() for k, _ in key_values[0:int(.1*num_points)]: search(root, k) t1 = time.perf_counter() t_search.append(t1-t0) t0 = time.perf_counter() for k in range(num_points, int(num_points*1.1)): search(root, str(k)) t1 = time.perf_counter() t_missing.append(t1-t0) make_plot(t_add, t_search, t_missing, "BST performance", "bst_test.png")
def test_none_insert_root(self): tree = bt.insert(None, 7, 4) self.assertEqual(tree.key, 7) self.assertEqual(tree.value, 4)
def insert_value(self): A = bt.Node() bt.insert(A, 1, 'one') self.assertEqual(A.key, 'one')
def test_binary_tree_search_first(self): root = None root = binary_tree.insert(root, key=1, value=10) search = binary_tree.search(root, 1) self.assertEqual((root.key, root.value), (1, search))
def test_none_insert_value(self): root = bt.Node(7, 4) root = bt.insert(root, 8, None) self.assertEqual(root.right.key, 8)
def test_none_insert_key(self): root = bt.Node(7, 4) self.assertRaises(ValueError, lambda: bt.insert(root, None, 7))
def test_binary_tree_insert_right(self): root = None root = binary_tree.insert(root, key=10) root = binary_tree.insert(root, key=11) self.assertEqual(root.right.key, 11)
def test_height(): root = insert(None, 15) assert height(root) == 1 insert(root, 10) assert height(root) == 2 insert(root, 25) assert height(root) == 2 insert(root, 6) assert height(root) == 3 insert(root, 14) insert(root, 20) insert(root, 60) assert height(root) == 3
def test_balanced(): root = insert(None, 15) insert(root, 10) insert(root, 25) insert(root, 6) insert(root, 14) insert(root, 20) insert(root, 60) print(treeToString(root)) assert checkBalancedBinaryTree(root)
def test_binary_tree_search_leftnode(self): root = None root = binary_tree.insert(root, key=10, value=10) root = binary_tree.insert(root, key=9, value=9) search = binary_tree.search(root, 9) self.assertEqual((root.left.key, root.left.value), (9, search))
def test_binary_tree_first_entry(self): r = binary_tree.insert(root=None, key="key", value=1) self.assertEqual((r.key, r.value), ("key", 1))
def test_search_cannot_find_value(self): root = None root = bt.insert(root, 102, 129) s = bt.search(root, 1000) self.assertEqual(s, 'key not found')
def test_binary_tree_search_rightnode(self): root = None root = binary_tree.insert(root, key=10, value=10) root = binary_tree.insert(root, key=11, value=11) search = binary_tree.search(root, 11) self.assertEqual((root.right.key, root.right.value), (11, search))
def setUpClass(cls): """ A function for initializing a binary tree for testing. """ cls.root = bt.Node(6, 'Root') bt.insert(cls.root, 7, 'A') bt.insert(cls.root, 8, 'B') bt.insert(cls.root, 9, 'C') bt.insert(cls.root, 3, 'D') bt.insert(cls.root, 2, 'E') bt.insert(cls.root, 4, 'F') bt.insert(cls.root, 5, 'G')
def test_binary_tree_insert_left(self): root = None root = binary_tree.insert(root, key=10) root = binary_tree.insert(root, key=9) self.assertEqual(root.left.key, 9)