def test_given_elements_then_traverse_preorder_should_return_elements_in_preorder_with_parents_first(
        input_elements, expected_output):
    object_under_test = RedBlackTree()

    for ele in input_elements:
        object_under_test.insert(value=ele)

    assert expected_output == object_under_test.traverse_preorder()
def test_search_on_a_tree_containing_given_value_should_return_node_with_the_value(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(8)

    result = object_under_test.search(8)

    assert result.value == 8
def test_inserting_root_and_two_children_then_right_child_should_be_red():
    object_under_test = RedBlackTree()
    object_under_test.insert(15)
    object_under_test.insert(8)
    object_under_test.insert(33)

    assert object_under_test.get_root().right.is_red is True
def test_inserting_root_and_two_children_then_right_child_value_should_be_highest(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(15)
    object_under_test.insert(8)
    object_under_test.insert(33)

    assert object_under_test.get_root().right.value == 33
示例#5
0
 def test_insert_two_nodes_to_the_right(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(10))
     rbt.rbt_insert(RedBlackTree.Node(30))
     rbt.rbt_insert(RedBlackTree.Node(40))
     assert rbt.root.key == 20
     assert rbt.root.is_black()
     assert rbt.root.left.key == 10
     assert rbt.root.left.is_black()
     assert rbt.root.right.key == 30
     assert rbt.root.right.is_black()
     assert rbt.root.right.right.key == 40
     assert rbt.root.right.right.is_red()
示例#6
0
 def test_force_left_rotate(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(30))
     rbt.rbt_insert(RedBlackTree.Node(40))
     assert rbt.root.key == 30
     assert rbt.root.is_black()
     assert rbt.root.left.key == 20
     assert rbt.root.left.is_red()
     assert rbt.root.right.key == 40
     assert rbt.root.right.is_red()
示例#7
0
 def test_force_double_rotate_left_then_right(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(10))
     rbt.rbt_insert(RedBlackTree.Node(15))
     assert rbt.root.key == 15
     assert rbt.root.is_black()
     assert rbt.root.left.key == 10
     assert rbt.root.left.is_red()
     assert rbt.root.right.key == 20
     assert rbt.root.right.is_red()
示例#8
0
 def test_force_right_rotate(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(10))
     rbt.rbt_insert(RedBlackTree.Node(5))
     assert rbt.root.key == 10
     assert rbt.root.is_black()
     assert rbt.root.left.key == 5
     assert rbt.root.left.is_red()
     assert rbt.root.right.key == 20
     assert rbt.root.right.is_red()
示例#9
0
 def four_node_factory(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(30))
     rbt.rbt_insert(RedBlackTree.Node(40))
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(10))
     assert rbt.root.key == 30
     assert rbt.root.is_black()
     assert rbt.root.left.key == 20
     assert rbt.root.left.is_black()
     assert rbt.root.right.key == 40
     assert rbt.root.right.is_black()
     assert rbt.root.left.left.key == 10
     assert rbt.root.left.left.is_red()
     return rbt
示例#10
0
 def test_can_change_from_red_to_black_and_back(self):
     node = RedBlackTree.Node(20)
     assert node.is_red()
     node.color = BLACK
     assert node.is_black()
     node.color = RED
     assert node.is_red()
def test_inserting_three_nodes_in_descending_line_then_root_should_be_black():
    object_under_test = RedBlackTree()
    object_under_test.insert(3)
    object_under_test.insert(2)
    object_under_test.insert(1)

    assert object_under_test.get_root().is_red is False
def test_deleting_only_node_should_leave_the_tree_empty():
    object_under_test = RedBlackTree()
    object_under_test.insert(22)

    object_under_test.delete(22)

    assert not object_under_test.traverse_preorder()
def test_inserting_three_nodes_in_ascending_line_then_right_child_value_should_be_highest(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(1)
    object_under_test.insert(2)
    object_under_test.insert(3)

    assert object_under_test.get_root().right.value == 3
def test_inserting_three_nodes_in_descending_line_then_left_child_value_should_be_lowest(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(3)
    object_under_test.insert(2)
    object_under_test.insert(1)

    assert object_under_test.get_root().left.value == 1
def test_inserting_three_nodes_in_descending_line_then_right_child_should_be_red(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(3)
    object_under_test.insert(2)
    object_under_test.insert(1)

    assert object_under_test.get_root().right.is_red is True
def test_inserting_root_and_two_children_then_root_value_should_be_in_the_middle(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(2)
    object_under_test.insert(1)
    object_under_test.insert(3)

    assert object_under_test.get_root().value == 2
def test_inserting_three_nodes_in_ascending_line_then_root_value_should_be_in_the_middle(
):
    object_under_test = RedBlackTree()
    object_under_test.insert(1)
    object_under_test.insert(2)
    object_under_test.insert(3)

    assert object_under_test.get_root().value == 2
示例#18
0
 def test_insert_a_second_node_to_right(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(30))
     assert rbt.root.key == 20
     assert rbt.root.is_black()
     assert rbt.root.right.key == 30
     assert rbt.root.right.is_red()
示例#19
0
 def test_insert_a_second_node_to_left(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(10))
     assert rbt.root.key == 20
     assert rbt.root.is_black()
     assert rbt.root.left.key == 10
     assert rbt.root.left.is_red()
def test_deleting_nonexistent_value_should_raise():
    object_under_test = RedBlackTree()
    object_under_test.insert(44)

    nonexistent_value = 22

    with pytest.raises(ValueNotFoundInTreeError) as err:
        object_under_test.delete(nonexistent_value)

    assert f"{nonexistent_value}" in str(err)
def test_inserting_duplicated_value_should_raise_an_error():
    object_under_test = RedBlackTree()

    duplicated_elem = 8
    object_under_test.insert(duplicated_elem)

    with pytest.raises(DuplicatedValueInTreeError) as err:
        object_under_test.insert(duplicated_elem)

    assert f"{duplicated_elem}" in str(err)
def test_search_on_empty_tree_returns_none():
    object_under_test = RedBlackTree()

    assert object_under_test.search(8) is None
示例#23
0
 def test_initialization(self):
     rbt = RedBlackTree()
     assert isinstance(rbt, RedBlackTree)
     assert isinstance(rbt, BinarySearchTree)
     assert isinstance(rbt, BinaryTree)
示例#24
0
 def test_initialization(self):
     node = RedBlackTree.Node(20)
     assert isinstance(node, RedBlackTree.Node)
     assert node.is_red()
示例#25
0
    def test_right_left_case(self):
        rbt = RedBlackTree()
        rbt.rbt_insert(RedBlackTree.Node(30))
        rbt.rbt_insert(RedBlackTree.Node(20))
        rbt.rbt_insert(RedBlackTree.Node(40))
        rbt.rbt_insert(RedBlackTree.Node(35))
        assert rbt.root.key == 30
        assert rbt.root.is_black()
        assert rbt.root.left.key == 20
        assert rbt.root.left.is_black()
        assert rbt.root.right.key == 40
        assert rbt.root.right.is_black()
        assert rbt.root.right.left.key == 35
        assert rbt.root.right.left.is_red()

        rbt.rbt_delete(rbt.root.left)
        assert rbt.root.key == 35
        # assert rbt.root.is_black()
        assert rbt.root.left.key == 30
        # assert rbt.root.left.is_black()
        assert rbt.root.right.key == 40
示例#26
0
 def test_insert_root_into_red_black_tree(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     assert rbt.root.key == 20
     assert rbt.root.is_black()
def test_given_empty_tree_then_is_empty_should_return_true():
    assert RedBlackTree().is_empty()
def test_given_one_node_then_is_empty_should_return_false():
    object_under_test = RedBlackTree()
    object_under_test.insert(15)

    assert not object_under_test.is_empty()