예제 #1
0
 def test_delete_right_node_noChildren(self):
     tree = RBtree()
     tree.insert(15)
     tree.insert(20)
     tree.insert(25)
     node = tree.root.right
     tree.delete_right_node_noChildren(node)
     self.assertIsNone(tree.root.right.key)
예제 #2
0
 def test_delete_right_node_onlyLeftChildren(self):
     tree = RBtree()
     tree.insert(20)
     tree.insert(15)
     tree.insert(25)
     tree.insert(23)
     node = tree.root.right
     tree.delete_right_node_onlyLeftChildren(node)
     self.assertEqual(tree.root.right.key, 23)
예제 #3
0
 def test_delete_left_node_onlyLeftChildren(self):
     tree = RBtree()
     tree.insert(20)
     tree.insert(15)
     tree.insert(25)
     tree.insert(14)
     node = tree.root.left
     tree.delete_left_node_onlyLeftChildren(node)
     self.assertEqual(tree.root.left.key, 14)
예제 #4
0
    def test_Insert(self):
        tree = RBtree()
        tree.insert(20)
        tree.insert(15)
        tree.insert(25)
        tree.insert(10)

        self.assertFalse(tree.root.red)
        self.assertFalse(tree.search(15).red)
        self.assertFalse(tree.search(25).red)
        tree.insert(17)
        tree.insert(8)
        self.assertTrue(tree.search(15).red)
        self.assertFalse(tree.search(10).red)
        self.assertFalse(tree.search(17).red)
        self.assertTrue(tree.search(8).red)
        tree.insert(9)
        self.assertTrue(tree.search(10).red)
        self.assertTrue(tree.search(8).red)
        self.assertEqual(tree.search(9).left.key, 8)
예제 #5
0
    def test_Prev(self):
        tree = RBtree()
        tree.insert(20)
        tree.insert(15)
        tree.insert(25)
        node1 = tree.root.left
        tree.insert(10)
        node2 = node1.left

        self.assertIsNone(tree.FindPrev(25))
        self.assertEqual(tree.FindPrev(20), node1)
        self.assertEqual(tree.FindPrev(15), node2)
예제 #6
0
    def test_Next(self):
        tree = RBtree()
        tree.insert(20)
        tree.insert(15)
        tree.insert(25)
        node1 = tree.root.right
        tree.insert(10)
        tree.insert(30)
        node2 = node1.right

        self.assertIsNone(tree.FindNext(15))
        self.assertEqual(tree.FindNext(20), node1)
        self.assertEqual(tree.FindNext(25), node2)
예제 #7
0
 def test_Delete(self):
     tree = RBtree()
     tree.insert(20)
     tree.insert(15)
     tree.insert(25)
     tree.insert(23)
     tree.insert(27)
     self.assertFalse(tree.root.red, False)
     self.assertEqual(tree.root.right.key, 25)
     self.assertEqual(tree.root.right.left.key, 23)
     self.assertTrue(tree.root.right.left.red, True)
     tree.deleteNode(25)
     self.assertEqual(tree.root.key, 20)
     self.assertEqual(tree.root.right.key, 27)
     self.assertFalse(tree.root.right.red, False)
     self.assertIsNone(tree.root.right.right.key, None)
     self.assertEqual(tree.root.right.left.key, 23)
     self.assertTrue(tree.root.right.left.red, True)
예제 #8
0
    def test_find_node(self):
        rb_tree = RBtree()
        rb_tree.insert(2)
        node_2 = rb_tree.root
        rb_tree.insert(1)
        node_1 = rb_tree.root.left
        rb_tree.insert(4)
        node_4 = rb_tree.root.right
        rb_tree.insert(5)
        node_5 = node_4.right
        rb_tree.insert(9)
        node_9 = node_5.right
        rb_tree.insert(3)
        node_3 = node_4.left
        rb_tree.insert(6)
        node_6 = node_9.left
        rb_tree.insert(7)
        node_7 = node_5.right
        rb_tree.insert(15)
        node_15 = node_9.right

        self.assertEqual(rb_tree.search(5), node_5)
        self.assertEqual(rb_tree.search(2), node_2)
        self.assertEqual(rb_tree.search(1), node_1)
        self.assertEqual(rb_tree.search(4), node_4)
        self.assertEqual(rb_tree.search(3), node_3)
        self.assertEqual(rb_tree.search(7), node_7)
        self.assertEqual(rb_tree.search(6), node_6)
        self.assertEqual(rb_tree.search(9), node_9)
        self.assertEqual(rb_tree.search(15), node_15)
예제 #9
0
 def test_insert_root(self):
     rb_tree = RBtree()
     rb_tree.insert(15)
     self.assertEqual(rb_tree.root.key, 15)
     self.assertEqual(rb_tree.root.key, 15)