Пример #1
0
    def test_04_delete(self):
        tree = self.tree

        tree.delete(12)
        self.assertEqual(tree.to_list(),
            (6, (4, (1, (0, None, None), (3, None, None)), None), (7, None, (9, None, None))))
        tree.traverse(self.check)

        tree.delete(7)
        self.assertEqual(tree.to_list(),
            (6, (4, (1, (0, None, None), (3, None, None)), None), (9, None, None)))
        tree.traverse(self.check)

        self.assertRaises(KeyError, tree.delete, 7)

        tree.delete(1)
        self.assertEqual(tree.to_list(),
            (6, (4, (0, None, (3, None, None)), None), (9, None, None)))
        tree.traverse(self.check)

        tree.delete(6)
        self.assertEqual(tree.to_list(),
            (4, (0, None, (3, None, None)), (9, None, None)))
        tree.traverse(self.check)

        tree.delete(9)
        self.assertEqual(tree.to_list(),
            (4, (0, None, (3, None, None)), None))
        tree.traverse(self.check)

        tree = Node.from_list_raw((150, (130, None, None), (170, None, (190, None, (210, None, None)))))
        tree.delete(170)
        self.assertEqual(tree.to_list(),
            (150, (130, None, None), (190, None, (210, None, None))))
        tree.traverse(self.check)

        tree.delete(210)
        self.assertEqual(tree.to_list(),
            (150, (130, None, None), (190, None, None)))
        tree.traverse(self.check)

        tree.delete(150)
        self.assertEqual(tree.to_list(),
            (130, None, (190, None, None)))
        tree.traverse(self.check)

        tree.delete(130)
        self.assertEqual(tree.to_list(),
            (190, None, None))
        tree.traverse(self.check)

        self.assertRaises(RuntimeError, tree.delete, 190)
Пример #2
0
    def test_06_rotate(self):
        tree = Node.from_list([100, 50, 40, 60, 150, 170, 190, 200])

        tree.search(40).rotate_cw()
        self.assertEqual(tree.to_list(),
            (100, (40, None, (50, None, (60, None, None))),
             (150, None, (170, None, (190, None, (200, None, None)))))
        )
        tree.traverse(self.check)

        tree.search(170).rotate_ccw()
        self.assertEqual(tree.to_list(),
            (100, (40, None, (50, None, (60, None, None))),
             (170, (150, None, None), (190, None, (200, None, None))))
        )
        tree.traverse(self.check)

        t1 = Node.from_list_raw((32, (23, None, None), (48, None, (59, None, None))))
        t1.right.rotate_ccw()
        t1.traverse(self.check)
Пример #3
0
 def setUp(self):
     self.tree = Node.from_list_raw(self.LIST)