def build_tree(elements): root = BinarySearchTreeNode(elements[0]) for element_index in range(1, len(elements)): root.add_child(elements[element_index]) return root
def test_print_traversals(self): # WARNING: Tests are for Print() # Debug calls to Print() in functions will cause failure stdout_ = sys.stdout # Keep previous value sys.stdout = io.StringIO() self.bst = BinarySearchTreeNode(1) self.bst.insert(8) self.bst.insert(5) self.bst.insert(7) self.bst.insert(6) self.bst.insert(3) self.bst.insert(4) self.bst.insert(2) self.bst.in_order_print(self.bst) output = sys.stdout.getvalue() self.assertEqual(output, "1\n2\n3\n4\n5\n6\n7\n8\n") sys.stdout = io.StringIO() self.bst.bft_print(self.bst) output = sys.stdout.getvalue() self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or output == "1\n8\n5\n7\n3\n6\n4\n2\n") sys.stdout = io.StringIO() self.bst.dft_print(self.bst) output = sys.stdout.getvalue() self.assertTrue(output == "1\n8\n5\n7\n6\n3\n4\n2\n" or output == "1\n8\n5\n3\n2\n4\n7\n6\n") sys.stdout = io.StringIO() self.bst.pre_order_dft(self.bst) output = sys.stdout.getvalue() self.assertEqual(output, "1\n8\n5\n3\n2\n4\n7\n6\n") sys.stdout = io.StringIO() self.bst.post_order_dft(self.bst) output = sys.stdout.getvalue() self.assertEqual(output, "2\n4\n3\n6\n7\n5\n8\n1\n") sys.stdout = stdout_ # Restore stdout
def setUp(self): size = 10 self.random_list = random.sample(range(0, size), size) # self.random_list = [5, 3, 6, 1, 2, 4, 9, 0, 7, 8] print "random list generated: " + str(self.random_list) self.tree = BinarySearchTreeNode(None, None, None, None) for key in self.random_list: self.tree.add(key, "data-"+str(key)) print 'height of tree is: %d' % self.tree.height() print 'leaf count of tree is: %d' % self.tree.leaf_count() self.existed_key = random.sample(self.random_list, 1)[0] # self.existed_key = 5 print "existed_key: " + str(self.existed_key) self.non_existed_key = size + 1
def setUp(self): self.bst = BinarySearchTreeNode(5)
class BinarySearchTreeTests(unittest.TestCase): def setUp(self): self.bst = BinarySearchTreeNode(5) def test_insert(self): self.bst.insert(2) self.bst.insert(3) self.bst.insert(7) self.bst.insert(6) self.assertEqual(self.bst.left.right.value, 3) self.assertEqual(self.bst.right.left.value, 6) def test_contains(self): self.bst.insert(2) self.bst.insert(3) self.bst.insert(7) self.assertTrue(self.bst.contains(7)) self.assertFalse(self.bst.contains(8)) def test_get_max(self): self.assertEqual(self.bst.get_max(), 5) self.bst.insert(30) self.assertEqual(self.bst.get_max(), 30) self.bst.insert(300) self.bst.insert(3) self.assertEqual(self.bst.get_max(), 300) def test_for_each(self): arr = [] cb = lambda x: arr.append(x) v1 = random.randint(1, 101) v2 = random.randint(1, 101) v3 = random.randint(1, 101) v4 = random.randint(1, 101) v5 = random.randint(1, 101) self.bst.insert(v1) self.bst.insert(v2) self.bst.insert(v3) self.bst.insert(v4) self.bst.insert(v5) self.bst.for_each(cb) self.assertTrue(5 in arr) self.assertTrue(v1 in arr) self.assertTrue(v2 in arr) self.assertTrue(v3 in arr) self.assertTrue(v4 in arr) self.assertTrue(v5 in arr)
class TestBinarySearchTree(unittest.TestCase): def setUp(self): size = 10 self.random_list = random.sample(range(0, size), size) # self.random_list = [5, 3, 6, 1, 2, 4, 9, 0, 7, 8] print "random list generated: " + str(self.random_list) self.tree = BinarySearchTreeNode(None, None, None, None) for key in self.random_list: self.tree.add(key, "data-"+str(key)) print 'height of tree is: %d' % self.tree.height() print 'leaf count of tree is: %d' % self.tree.leaf_count() self.existed_key = random.sample(self.random_list, 1)[0] # self.existed_key = 5 print "existed_key: " + str(self.existed_key) self.non_existed_key = size + 1 def tearDown(self): pass def test_min(self): min_value = min(self.random_list) self.assertEqual(min_value, (self.tree.min()).key) def test_max(self): max_value = max(self.random_list) self.assertEqual(max_value, (self.tree.max()).key) def test_search(self): existed_node = self.tree.search(self.existed_key) self.assertEqual(self.existed_key, existed_node.key) non_existed_node = self.tree.search(self.non_existed_key) self.assertEqual(non_existed_node, None) def test_delete(self): print '\nbefore delete,pre order', self.tree.pre_order() print '\nbefore delete,in order', self.tree.in_order() self.tree.delete(self.existed_key) research_deleted_node = self.tree.search(self.existed_key) self.assertEqual(research_deleted_node, None) print '\nafter delete, pre order', self.tree.pre_order() print '\nafter delete, in order', self.tree.in_order()
class BinarySearchTreeNodeTests(unittest.TestCase): def setUp(self): self.bst = BinarySearchTreeNode(5) def test_insert(self): self.bst.insert(2) self.bst.insert(3) self.bst.insert(7) self.bst.insert(6) self.assertEqual(self.bst.left.right.value, 3) self.assertEqual(self.bst.right.left.value, 6) def test_handle_dupe_insert(self): self.bst2 = BinarySearchTreeNode(1) self.bst2.insert(1) self.assertEqual(self.bst2.right.value, 1) def test_contains(self): self.bst.insert(2) self.bst.insert(3) self.bst.insert(7) self.assertTrue(self.bst.contains(7)) self.assertFalse(self.bst.contains(8)) def test_get_max(self): self.assertEqual(self.bst.get_max(), 5) self.bst.insert(30) self.assertEqual(self.bst.get_max(), 30) self.bst.insert(300) self.bst.insert(3) self.assertEqual(self.bst.get_max(), 300) def test_for_each(self): arr = [] def cb(x): return arr.append(x) v1 = random.randint(1, 101) v2 = random.randint(1, 101) v3 = random.randint(1, 101) v4 = random.randint(1, 101) v5 = random.randint(1, 101) self.bst.insert(v1) self.bst.insert(v2) self.bst.insert(v3) self.bst.insert(v4) self.bst.insert(v5) self.bst.for_each(cb) self.assertTrue(5 in arr) self.assertTrue(v1 in arr) self.assertTrue(v2 in arr) self.assertTrue(v3 in arr) self.assertTrue(v4 in arr) self.assertTrue(v5 in arr) def test_print_traversals(self): # WARNING: Tests are for Print() # Debug calls to Print() in functions will cause failure stdout_ = sys.stdout # Keep previous value sys.stdout = io.StringIO() self.bst = BinarySearchTreeNode(1) self.bst.insert(8) self.bst.insert(5) self.bst.insert(7) self.bst.insert(6) self.bst.insert(3) self.bst.insert(4) self.bst.insert(2) self.bst.in_order_print(self.bst) output = sys.stdout.getvalue() self.assertEqual(output, "1\n2\n3\n4\n5\n6\n7\n8\n") sys.stdout = io.StringIO() self.bst.bft_print(self.bst) output = sys.stdout.getvalue() self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or output == "1\n8\n5\n7\n3\n6\n4\n2\n") sys.stdout = io.StringIO() self.bst.dft_print(self.bst) output = sys.stdout.getvalue() self.assertTrue(output == "1\n8\n5\n7\n6\n3\n4\n2\n" or output == "1\n8\n5\n3\n2\n4\n7\n6\n") sys.stdout = io.StringIO() self.bst.pre_order_dft(self.bst) output = sys.stdout.getvalue() self.assertEqual(output, "1\n8\n5\n3\n2\n4\n7\n6\n") sys.stdout = io.StringIO() self.bst.post_order_dft(self.bst) output = sys.stdout.getvalue() self.assertEqual(output, "2\n4\n3\n6\n7\n5\n8\n1\n") sys.stdout = stdout_ # Restore stdout
def test_handle_dupe_insert(self): self.bst2 = BinarySearchTreeNode(1) self.bst2.insert(1) self.assertEqual(self.bst2.right.value, 1)
duplicates = get_dupes(merge_sort(names_1), merge_sort(names_2)) end_time = time.time() print( "---- Merge Sort ----", f"{len(duplicates)} duplicates:", f"{', '.join(duplicates)}", f"runtime: {end_time - start_time} seconds", sep='\n\n' ) ## best solution from binary_search_tree import BinarySearchTreeNode start_time = time.time() BST = BinarySearchTreeNode(names_1[0]) for name in names_1[1:]: BST.insert(name) duplicates = [] for name in names_2: if BST.contains(name): duplicates.append(name) end_time = time.time() print( "---- Merge Sort ----", f"{len(duplicates)} duplicates:", f"{', '.join(duplicates)}", f"runtime: {end_time - start_time} seconds",