Beispiel #1
0
    def test_right_right_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))
        rbt.rbt_insert(RedBlackTree.Node(50))
        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()
        assert rbt.root.right.right.key == 50
        assert rbt.root.right.right.is_red()

        rbt.rbt_delete(rbt.root.left)
        assert rbt.root.key == 40
        assert rbt.root.is_black()
        assert rbt.root.left.key == 30
        assert rbt.root.left.is_black()
        assert rbt.root.left.right.key == 35
        assert rbt.root.left.right.is_red()
        assert rbt.root.right.key == 50
        assert rbt.root.right.is_black()
Beispiel #2
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()
Beispiel #3
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()
Beispiel #4
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()
Beispiel #5
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()
Beispiel #6
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()
Beispiel #7
0
 def test_insert_nodes_to_left_and_right(self):
     rbt = RedBlackTree()
     rbt.rbt_insert(RedBlackTree.Node(20))
     rbt.rbt_insert(RedBlackTree.Node(10))
     rbt.rbt_insert(RedBlackTree.Node(30))
     assert rbt.root.key == 20
     assert rbt.root.is_black()
     assert rbt.root.left.key == 10
     assert rbt.root.left.is_red()
     assert rbt.root.right.key == 30
     assert rbt.root.right.is_red()
Beispiel #8
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
Beispiel #9
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()
Beispiel #10
0
 def test_initialization(self):
     node = RedBlackTree.Node(20)
     assert isinstance(node, RedBlackTree.Node)
     assert node.is_red()
Beispiel #11
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()