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

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

        B.right = C
        A.left = B

        #    A
        #   /
        #  B
        #   \
        #    C

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

    def test1_double_left_right_rotation_returns_new_node(self):
        """Calling double_left_right_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_left_right_rotation(A),
            C,
            "Rotating a node {0} with a LR rotation should return the node which takes its place."
            .format(A)
        )


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

        A_new = self.tree.double_left_right_rotation(A)

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

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

        self.tree.double_left_right_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)
            )