def test_inserts_value_to_the_right_if_greater_second_level(self): bt = BinarySearchTree() bt.insert(10) bt.insert(11) bt.insert(12) self.assertEqual(bt.root.right.right.value, 12)
def test_inserts_value_to_the_right_and_then_left(self): bt = BinarySearchTree() bt.insert(10) bt.insert(20) bt.insert(15) self.assertEqual(bt.root.right.left.value, 15)
def test_delete_node(self): values = [7, 3, 23, 1, 5, 9, 52, 2, 4, 36] tr = BinarySearchTree() for v in values: tr.insert(v) tr.populate_parent() expected_in_order_tree_after_7_delete = \ [1, 2, 3, 4, 5, 9, 23, 36, 52] tr.delete_data(7) self.assertEquals(tr.getInOrderPath(), expected_in_order_tree_after_7_delete) self.assertRaises(Exception, tr.delete_data, 6) expected_in_order = \ [1, 3, 4, 5, 9, 23, 36, 52] tr.delete_data(2) self.assertEquals(tr.getInOrderPath(), expected_in_order) expected_in_order = \ [1, 4, 5, 9, 23, 36, 52] tr.delete_data(3) self.assertEquals(tr.getInOrderPath(), expected_in_order) expected_in_order = \ [1, 4, 5, 9, 23, 36] tr.delete_data(52) self.assertEquals(tr.getInOrderPath(), expected_in_order) expected_in_order = \ [1, 5, 9, 23, 36] tr.delete_data(4) self.assertEquals(tr.getInOrderPath(), expected_in_order)
def test_inserts_value_to_the_left_if_smaller_second_level(self): bt = BinarySearchTree() bt.insert(10) bt.insert(2) bt.insert(1) self.assertEqual(bt.root.left.left.value, 1)
def test_size(self): bstn = BinarySearchTreeNode(10) bst = BinarySearchTree(bstn) bst.insert(12) bst.insert(8) bst.insert(7) self.assertEqual(size(bst.root), 4)
def test_insert(self): bstn = BinarySearchTreeNode(10) bst = BinarySearchTree(bstn) bst.insert(12) bst.insert(8) self.assertFalse(bst.is_in_tree(9)) self.assertTrue(bst.is_in_tree(12)) self.assertTrue(bst.is_in_tree(8))
def test_throws_when_adding_an_already_existing_value_with_multiple_values_tree( self): bt = BinarySearchTree() bt.insert(10) bt.insert(11) bt.insert(21) bt.insert(301) bt.insert(1) with self.assertRaises(ValueError): bt.insert(10)
def test_insert_with_7_items(self): # Create a complete binary search tree of 7 items in level-order items = [4, 2, 6, 1, 3, 5, 7] tree = BinarySearchTree() for item in items: tree.insert(item) assert tree.root.data == 4 assert tree.root.left.data == 2 assert tree.root.right.data == 6 assert tree.root.left.left.data == 1 assert tree.root.left.right.data == 3 assert tree.root.right.left.data == 5 assert tree.root.right.right.data == 7
class TestBinarySearchTree(unittest.TestCase): def setUp(self): self.empty_bst = BinarySearchTree() self.single_node_bst = BinarySearchTree() self.single_node_bst.insert(-1) self.small_bst = BinarySearchTree() self.small_bst.insert(43) self.small_bst.insert(43) self.small_bst.insert(-2) self.small_bst.insert(102) self.toy_bst = get_toy_binary_search_tree() def test_maxValue(self): self.assertRaises(TypeError, self.empty_bst.maxValue) self.assertEqual(-1, self.single_node_bst.maxValue()) self.assertEqual(102, self.small_bst.maxValue()) self.assertEqual(102, self.toy_bst.maxValue()) def test_minValue(self): self.assertRaises(TypeError, self.empty_bst.minValue) self.assertEqual(-1, self.single_node_bst.minValue()) self.assertEqual(-2, self.small_bst.minValue()) self.assertEqual(-1, self.toy_bst.minValue()) def test_delete_node(self): values = [7, 3, 23, 1, 5, 9, 52, 2, 4, 36] tr = BinarySearchTree() for v in values: tr.insert(v) tr.populate_parent() expected_in_order_tree_after_7_delete = \ [1, 2, 3, 4, 5, 9, 23, 36, 52] tr.delete_data(7) self.assertEquals(tr.getInOrderPath(), expected_in_order_tree_after_7_delete) self.assertRaises(Exception, tr.delete_data, 6) expected_in_order = \ [1, 3, 4, 5, 9, 23, 36, 52] tr.delete_data(2) self.assertEquals(tr.getInOrderPath(), expected_in_order) expected_in_order = \ [1, 4, 5, 9, 23, 36, 52] tr.delete_data(3) self.assertEquals(tr.getInOrderPath(), expected_in_order) expected_in_order = \ [1, 4, 5, 9, 23, 36] tr.delete_data(52) self.assertEquals(tr.getInOrderPath(), expected_in_order) expected_in_order = \ [1, 5, 9, 23, 36] tr.delete_data(4) self.assertEquals(tr.getInOrderPath(), expected_in_order)
def test_is_bst(self): bstn = BinarySearchTreeNode(10) bst = BinarySearchTree(bstn) bst.insert(12) bst.insert(8) bst.insert(7) self.assertEqual(is_bst(bst.root, None, None), True) bstn = BinarySearchTreeNode(10) bstn.left = BinarySearchTreeNode(10) bstn.left.left = BinarySearchTreeNode(11) bstn.right = BinarySearchTreeNode(12) self.assertEqual(is_bst(bstn, None, None), False) bstn = BinarySearchTreeNode(20) bstn.left = BinarySearchTreeNode(10) bstn.left.right = BinarySearchTreeNode(25) bstn.right = BinarySearchTreeNode(30) self.assertEqual(is_bst(bstn, None, None), False)
def test_size(self): tree = BinarySearchTree() assert tree.size == 0 tree.insert('B') assert tree.size == 1 tree.insert('A') assert tree.size == 2 tree.insert('C') assert tree.size == 3
def test_insert_with_3_items(self): # Create a complete binary search tree of 3 items in level-order tree = BinarySearchTree() tree.insert(2) assert tree.root.data == 2 assert tree.root.left is None assert tree.root.right is None tree.insert(1) assert tree.root.data == 2 assert tree.root.left.data == 1 assert tree.root.right is None tree.insert(3) assert tree.root.data == 2 assert tree.root.left.data == 1 assert tree.root.right.data == 3
from binary_tree import BinarySearchTree import random def pretty_print(node, level): # step 1 (base case) if node.val is None: return # step 2 if node.left is not None: to_print = "\t" * level + str(node.val) print(to_print) pretty_print(node.left, level + 1) elif node.right is not None: to_print = "\t" * level + str(node.val) print(to_print) pretty_print(node.right, level + 1) tree = BinarySearchTree() listing = [] for _ in range(10): elem = random.randint(0, 10) listing.append(elem) tree.insert(elem) print(listing) pretty_print(tree.root, 0)
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 bst = BinarySearchTree("") for name_2 in names_2: bst.insert(name_2) for name_1 in names_1: if bst.contains(name_1): duplicates.append(name_1) 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.
def test_throws_when_adding_an_already_existing_value(self): bt = BinarySearchTree() bt.insert(10) with self.assertRaises(ValueError): bt.insert(10)
import time from binary_tree import BinarySearchTree 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 binary_tree = BinarySearchTree(names_1[0]) for name in names_1[1:]: binary_tree.insert(name) for name in names_2: if binary_tree.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.
import time from binary_tree import BinarySearchTree 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 = [] tree = BinarySearchTree('tree') for name_1 in names_1: tree.insert(name_1) for name_2 in names_2: if tree.contains(name_2): duplicates.append(name_2) 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_inserts_first_node_as_root_node(self): bt = BinarySearchTree() bt.insert(10) self.assertIs(bt.root.value, 10)
def test_does_not_insert_subsequent_node_as_root_node(self): bt = BinarySearchTree() bt.insert(10) bt.insert(20) self.assertEqual(bt.root.value, 10)