コード例 #1
0
class Test4AVLDoubleRightLeft(unittest.TestCase):

    def setUp(self):
        A = BSTNode(1, "A")
        B = BSTNode(3, "B")
        C = BSTNode(2, "C")

        B.left = C
        A.right = B

        #    A
        #     \
        #      B
        #     /
        #    C

        self.tree = AVL()
        self.nodes = (A, B, C)


    def test1_double_right_left_rotation_returns_new_node(self):
        """Calling double_right_left_rotation for a node returns the new node which takes the place of the old node. (1p)"""
        A, _, C = self.nodes
        self.assertIs(
            self.tree.double_right_left_rotation(A),
            C,
            "Rotating a node {0} with a RL rotation should return the node which takes its place."
            .format(A)
        )


    def test2_double_right_left_rotation(self):
        """Rotating a node A with a double, right-left rotation moves the right child of A as the new right child of the old left child of the old right child of A. (1p)"""
        A, B, C = self.nodes

        self.tree.double_right_left_rotation(A)

        self.assertIs(
            C.left,
            A,
            "The left child of {0} should be {1}."
            .format(C.left, A)
        )
        self.assertIs(
            C.right,
            B,
            "The right child of {0} should be {1}."
            .format(C.right, B)
        )


    def test3_double_right_left_rotation_updates_height(self):
        """After rotating a node A with a double, right-left rotation, the heights of the nodes are correct. (1p)"""
        A, B, C = self.nodes

        self.tree.double_right_left_rotation(A)

        expected_node_heights = [
            (A, 0),
            (B, 0),
            (C, 1),
        ]

        for node, expected_height in expected_node_heights:
            node_height = node.height()
            self.assertEqual(
                node_height,
                expected_height,
                "The height of {0} should be {1}, not {2}."
                .format(node, expected_height, node_height)
            )