def test_is_valid_red_black_tree_1(self): keys = [5, 3, 2, 1, 4, 1, 2, 6, 7, 1, 5, 4, 3, 2, 1] tree = RedBlackTree() for key in keys: tree.add(key) self.assertTrue(tree.is_red_black_tree(), "The Red Black Tree properties were violated.")
def test_insert(): bst = BinarySearchTree() rbt = RedBlackTree() bst.insert(5) rbt.insert(5) assert bst.find(5) and rbt.find(5)
def test_postorder(): bst = BinarySearchTree() rbt = RedBlackTree() data = gen_random_u_array(10) for d in data: bst.insert(d) rbt.insert(d) assert np.array_equal(bst.postorder(), rbt.postorder()) is False
def test_maximum(): bst = BinarySearchTree() rbt = RedBlackTree() data = gen_random_u_array(10) for d in data: bst.insert(d) rbt.insert(d) assert bst.maximum().key is rbt.maximum().key
def test_tree_height(): bst = BinarySearchTree() rbt = RedBlackTree() data = gen_random_u_array() for d in data: bst.insert(d) rbt.insert(d) assert bst.tree_height() is not rbt.tree_height()
def test_insertion_rn(): data = gen_random_u_array() sorted_data = SortedList(data) rbt = RedBlackTree() for d in data: rbt.insert(d) assert np.array_equal(rbt.inorder(), sorted_data)
def test_add_and_contains(self): tree = RedBlackTree() keys = [ 1, 2, 3, 4, 5, 20, 15, 14, 13, 12, 11, 6, 7, 8, 9, 19, 18, 25, 23, 24, 22, 21, 64, 66, 67, 69, 37, 39, 70, 73, 40, 75, 42, 76, 46, 47, 49, 53, 58, 62 ] for key in keys: tree.add(key) self.assertTrue(key in tree, "Key {} should be in the tree".format(key))
def test_is_valid_red_black_tree_2(self): keys = [ 1, 2, 3, 4, 5, 20, 15, 14, 13, 12, 11, 6, 7, 8, 9, 19, 18, 25, 23, 24, 22, 21, 64, 66, 67, 69, 37, 39, 70, 73, 40, 75, 42, 76, 46, 47, 49, 53, 58, 62 ] tree = RedBlackTree() for key in keys: tree.add(key) self.assertTrue(tree.is_red_black_tree(), "The Red Black Tree properties were violated.")
def test_valid_depth(self): tree = RedBlackTree() keys = list(range(10000)) shuffle(keys) for length, key in enumerate(keys, 1): tree.add(key) # Too expensive to check every time. if length < 100 or length % 1000 == 0: depth = tree.max_depth() lower_bound = log(length, 2) upper_bound = (2 * lower_bound) + 1 self.assertTrue( lower_bound <= depth <= upper_bound, "The tree depth should be between {} and {}, but it is {}." .format(lower_bound, upper_bound, depth))
def test_insert_fixup(): rbt = RedBlackTree() rbt.insert(5) rbt.insert(6) rbt.insert(2) assert rbt.root.color is Black and rbt.root.left.color \ is Red and rbt.root.right.color is Red and rbt.nil.color is Black
def test_remove(self): tree = RedBlackTree() keys = [ 1, 2, 3, 4, 5, 20, 15, 14, 13, 12, 11, 6, 7, 8, 9, 19, 18, 25, 23, 24, 22, 21, 64, 66, 67, 69, 37, 39, 70, 73, 40, 75, 42, 76, 46, 47, 49, 53, 58, 62 ] for key in keys: tree.add(key) for key in keys: tree.remove(key) self.assertFalse( key in tree, "Key {} should no longer be in the tree.".format(key))
def test_remove_on_not_present(self): tree = RedBlackTree() keys = [ 1, 2, 3, 4, 5, 20, 15, 14, 13, 12, 11, 6, 7, 8, 9, 19, 18, 25, 23, 24, 22, 21, 64, 66, 67, 69, 37, 39, 70, 73, 40, 75, 42, 76, 46, 47, 49, 53, 58, 62 ] for key in keys: tree.add(key) non_existant_keys = [10, 0, 16, 17, 28] for key in non_existant_keys: with self.assertRaises( KeyError, msg= "Remove should raise a key error if the node is not present in the tree," ): tree.remove(key)
def test_length(self): tree = RedBlackTree() keys = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] expected_length = 0 for key in keys: tree.add(key) expected_length += 1 self.assertEqual( len(tree), expected_length, "The tree should be length {}, but got length {}.".format( expected_length, len(tree))) for key in keys: tree.remove(key) expected_length -= 1 self.assertEqual( len(tree), expected_length, "The tree should be length {}, but got length {}.".format( expected_length, len(tree)))
def test_length_on_duplicates(self): tree = RedBlackTree() # There are 7 unique keys (1, 2, 3, 4, 5, 6, 7) keys = [5, 3, 2, 1, 4, 1, 2, 6, 7, 1, 5, 4, 3, 2, 1] keys_in_tree = set() for key in keys: tree.add(key) keys_in_tree.add(key) self.assertEqual( len(tree), len(keys_in_tree), "The tree should be length {}, but got length {}.".format( len(keys_in_tree), len(tree))) for key in keys: try: tree.remove(key) except: pass if key in keys_in_tree: keys_in_tree.remove(key) self.assertEqual( len(tree), len(keys_in_tree), "The tree should be length {}, but got length {}.".format( len(keys_in_tree), len(tree)))
def test_length_on_empty_tree(self): tree = RedBlackTree() self.assertTrue( len(tree) == 0, "An empty tree should have a length of 0.")
def test_find(): bst = BinarySearchTree() rbt = RedBlackTree() assert bst.find(5) is None and rbt.find(5) is None