예제 #1
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)
예제 #2
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)