Beispiel #1
0
 def test_tree_insert_right_node(self):
     rb_tree = RBTree()
     rb_node = RBTreeNode(9)
     rb_node_right = RBTreeNode(10)
     sol = Solution()
     sol.RBInsert(rb_tree, rb_node)
     sol.RBInsert(rb_tree, rb_node_right)
     self.assertEqual(rb_tree.root.right.key, rb_node_right.key)
Beispiel #2
0
 def test_tree_insert_left_node(self):
     rb_tree = RBTree()
     rb_node = RBTreeNode(9)
     rb_node_left = RBTreeNode(6)
     sol = Solution()
     sol.RBInsert(rb_tree, rb_node)
     sol.RBInsert(rb_tree, rb_node_left)
     self.assertEqual(rb_tree.root.left.key, rb_node_left.key)
 def test_right_right_reconciliation_strategy_with_no_greatgrandparent(self):
     root = RBTreeNode(5)
     root.insert(6)
     root.insert(7)
     self.assertEqual(root.value, 5)
     self.assertEqual(root.color, RBTreeColor.Red)
     self.assertEqual(root.parent.left, root)
     self.assertEqual(root.parent.value, 6)
     self.assertEqual(root.parent.color, RBTreeColor.Black)
     self.assertEqual(root.parent.right.value, 7)
     self.assertEqual(root.parent.right.color, RBTreeColor.Red)
    def test_left_left_reconciliation_strategy_with_no_greatgrandparent(self):
        root = RBTreeNode(5)
        root.insert(4)
        root.insert(3)

        self.assertEqual(root.parent.value, 4)
        self.assertEqual(root.left, None)
        self.assertEqual(root.right, None)
        self.assertEqual(root.parent.right, root)
        self.assertEqual(root.parent.left.value, 3)
        self.assertEqual(root.parent.left.left, None)
        self.assertEqual(root.parent.left.right, None)
        self.assertEqual(root.parent.left.parent, root.parent)
    def test_insert_equal(self):
        root = RBTreeNode(5)
        value = 5

        root.insert(value)

        self.assertEqual(root.color, RBTreeColor.Black)
        self.assertNotEqual(root.right, None)
        self.assertEqual(root.right.parent, root)
        self.assertEqual(root.right.color, RBTreeColor.Red)
        self.assertEqual(root.right.value, value)
        self.assertEqual(root.right.left, None)
        self.assertEqual(root.right.right, None)
Beispiel #6
0
 def test_node_init(self):
     nil = RBTreeNode(15)
     self.assertEqual(nil.key, 15)
     self.assertIsNone(nil.left, None)
     self.assertIsNone(nil.right, None)
     self.assertIsNone(nil.parent, None)
     self.assertEqual(nil.color, 'black')
 def test_right_left_reconciliation_strategy_with_no_greatgrandparent(self):
     root = RBTreeNode(5)
     for value in [7,2,10,9]:
         root.insert(value)
     self.assertEqual(root.value, 5)
     self.assertEqual(root.color, RBTreeColor.Black)
     self.assertEqual(root.left.value, 2)
     self.assertEqual(root.left.color, RBTreeColor.Black)
     self.assertEqual(root.left.parent, root)
     self.assertEqual(root.right.value, 9)
     self.assertEqual(root.right.color, RBTreeColor.Black)
     self.assertEqual(root.right.parent, root)
     self.assertEqual(root.right.left.value, 7)
     self.assertEqual(root.right.left.color, RBTreeColor.Red)
     self.assertEqual(root.right.left.parent, root.right)
     self.assertEqual(root.right.right.value, 10)
     self.assertEqual(root.right.right.color, RBTreeColor.Red)
     self.assertEqual(root.right.right.parent, root.right)
Beispiel #8
0
    def test_tree_insert(self):
        rb_tree = RBTree()
        rb_node = RBTreeNode(9)
        rb_node_left = RBTreeNode(4)
        rb_node_right = RBTreeNode(20)
        sol = Solution()
        sol.RBInsert(rb_tree, rb_node)
        sol.RBInsert(rb_tree, rb_node_left)
        sol.RBInsert(rb_tree, rb_node_right)

        rb_node_insert = RBTreeNode(3)
        sol.RBInsert(rb_tree, rb_node_insert)
        self.assertEqual(rb_tree.root.left.left.key, rb_node_insert.key)
        self.assertEqual(rb_tree.root.left.left.color, 'red')

        rb_node_insert = RBTreeNode(5)
        sol.RBInsert(rb_tree, rb_node_insert)
        self.assertEqual(rb_tree.root.left.right.key, rb_node_insert.key)
        self.assertEqual(rb_tree.root.left.right.color, 'red')
    def test_root(self):
        value = 'value'

        root = RBTreeNode(value)

        self.assertEqual(root.color, RBTreeColor.Black)
        self.assertEqual(root.parent, None)
        self.assertEqual(root.left, None)
        self.assertEqual(root.right, None)
        self.assertEqual(root.value, value)
Beispiel #10
0
    def test_tree_delete(self):
        rb_tree = RBTree()
        sol = Solution()
        rb_node_insert = RBTreeNode(9)
        sol.RBInsert(rb_tree, rb_node_insert)
        rb_node_insert_del = RBTreeNode(4)
        sol.RBInsert(rb_tree, rb_node_insert_del)
        rb_node_insert = RBTreeNode(20)
        sol.RBInsert(rb_tree, rb_node_insert)
        rb_node_insert = RBTreeNode(3)
        sol.RBInsert(rb_tree, rb_node_insert)
        rb_node_insert = RBTreeNode(5)
        sol.RBInsert(rb_tree, rb_node_insert)

        sol.RBDelete(rb_tree, rb_node_insert_del)

        self.assertEqual(rb_tree.root.key, 9)
        self.assertEqual(rb_tree.root.color, 'black')
        self.assertEqual(rb_tree.root.left.key, 5)
        self.assertEqual(rb_tree.root.left.color, 'black')
        self.assertEqual(rb_tree.root.left.left.key, 3)
        self.assertEqual(rb_tree.root.left.left.color, 'red')
        self.assertEqual(rb_tree.root.right.key, 20)
        self.assertEqual(rb_tree.root.right.color, 'black')
    def test_recoloring(self):
        root = RBTreeNode(5)
        root.insert(2)
        root.insert(7)
        root.insert(1)

        self.assertEqual(root.color, RBTreeColor.Black)
        self.assertEqual(root.left.color, RBTreeColor.Black)
        self.assertEqual(root.right.color, RBTreeColor.Black)
        self.assertEqual(root.left.left.color, RBTreeColor.Red)
Beispiel #12
0
 def test_tree_insert_root(self):
     rb_tree = RBTree()
     rb_node = RBTreeNode(9)
     sol = Solution()
     sol.RBInsert(rb_tree, rb_node)
     self.assertEqual(rb_tree.root.key, rb_node.key)
Beispiel #13
0
 def test_tree_init(self):
     nil = RBTreeNode(-1)
     rb_tree = RBTree()
     self.assertEqual(rb_tree.root.key, nil.key)