コード例 #1
0
    def test_full(self):
        """Rotate a fully-populated tree.

        The transformation is as such::

            r         r
            └┐        └┐
             x         y
            ┌┴┐   →   ┌┴┐
            a y       x c
             ┌┴┐     ┌┴┐
             b c     a b
        """
        tree = IntervalTree()
        node_r = Node(Interval(1, 1))
        node_a = Node(Interval(2, 2))
        node_x = Node(Interval(3, 3))
        node_b = Node(Interval(4, 4))
        node_y = Node(Interval(5, 5))
        node_c = Node(Interval(6, 6))
        tree.insert(node_r)
        tree.insert(node_x)

        # force tree to take desired shape
        node_x.left = node_a
        node_x.right = node_y
        node_a.parent = node_x
        node_y.parent = node_x
        node_y.left = node_b
        node_y.right = node_c
        node_b.parent = node_y
        node_c.parent = node_y

        tree._rotate_left(node_x)  # pylint:disable=protected-access
        with self.subTest(comment='verify node_r'):
            self.assertIsNone(node_r.parent)
            self.assertIsNone(node_r.left)
            self.assertEqual(node_r.right, node_y)
        with self.subTest(comment='verify node_x'):
            self.assertEqual(node_x.parent, node_y)
            self.assertEqual(node_x.left, node_a)
            self.assertEqual(node_x.right, node_b)
        with self.subTest(comment='verify node_y'):
            self.assertEqual(node_y.parent, node_r)
            self.assertEqual(node_y.left, node_x)
            self.assertEqual(node_y.right, node_c)
        with self.subTest(comment='verify node_a'):
            self.assertEqual(node_a.parent, node_x)
            self.assertIsNone(node_a.left)
            self.assertIsNone(node_a.right)
        with self.subTest(comment='verify node_b'):
            self.assertEqual(node_b.parent, node_x)
            self.assertIsNone(node_b.left)
            self.assertIsNone(node_b.right)
        with self.subTest(comment='verify node_c'):
            self.assertEqual(node_c.parent, node_y)
            self.assertIsNone(node_c.left)
            self.assertIsNone(node_c.right)
コード例 #2
0
ファイル: tests.py プロジェクト: Ichimonji10/impedimenta
    def test_full(self):
        """Rotate a fully-populated tree.

        The transformation is as such::

            r         r
            └┐        └┐
             x         y
            ┌┴┐   →   ┌┴┐
            a y       x c
             ┌┴┐     ┌┴┐
             b c     a b
        """
        tree = IntervalTree()
        node_r = Node(Interval(1, 1))
        node_a = Node(Interval(2, 2))
        node_x = Node(Interval(3, 3))
        node_b = Node(Interval(4, 4))
        node_y = Node(Interval(5, 5))
        node_c = Node(Interval(6, 6))
        tree.insert(node_r)
        tree.insert(node_x)

        # force tree to take desired shape
        node_x.left = node_a
        node_x.right = node_y
        node_a.parent = node_x
        node_y.parent = node_x
        node_y.left = node_b
        node_y.right = node_c
        node_b.parent = node_y
        node_c.parent = node_y

        tree._rotate_left(node_x)  # pylint:disable=protected-access
        with self.subTest(comment='verify node_r'):
            self.assertIsNone(node_r.parent)
            self.assertIsNone(node_r.left)
            self.assertEqual(node_r.right, node_y)
        with self.subTest(comment='verify node_x'):
            self.assertEqual(node_x.parent, node_y)
            self.assertEqual(node_x.left, node_a)
            self.assertEqual(node_x.right, node_b)
        with self.subTest(comment='verify node_y'):
            self.assertEqual(node_y.parent, node_r)
            self.assertEqual(node_y.left, node_x)
            self.assertEqual(node_y.right, node_c)
        with self.subTest(comment='verify node_a'):
            self.assertEqual(node_a.parent, node_x)
            self.assertIsNone(node_a.left)
            self.assertIsNone(node_a.right)
        with self.subTest(comment='verify node_b'):
            self.assertEqual(node_b.parent, node_x)
            self.assertIsNone(node_b.left)
            self.assertIsNone(node_b.right)
        with self.subTest(comment='verify node_c'):
            self.assertEqual(node_c.parent, node_y)
            self.assertIsNone(node_c.left)
            self.assertIsNone(node_c.right)
コード例 #3
0
    def test_empty(self):
        """Rotate a tree consisting of just two nodes.

        The transformation is as such::

             x         y
             └┐   →   ┌┘
              y       x
        """
        tree = IntervalTree()
        node_x = Node(Interval(1, 1))  # current tree root
        node_y = Node(Interval(2, 2))  # future tree root
        tree.insert(node_x)
        tree.insert(node_y)
        tree._rotate_left(node_x)  # pylint:disable=protected-access
        with self.subTest(comment='verify node_x'):
            self.assertEqual(node_x.parent, node_y)
            self.assertIsNone(node_x.left)
            self.assertIsNone(node_x.right)
        with self.subTest(comment='verify node_y'):
            self.assertIsNone(node_y.parent)
            self.assertEqual(node_y.left, node_x)
            self.assertIsNone(node_y.right)
コード例 #4
0
ファイル: tests.py プロジェクト: Ichimonji10/impedimenta
    def test_empty(self):
        """Rotate a tree consisting of just two nodes.

        The transformation is as such::

             x         y
             └┐   →   ┌┘
              y       x
        """
        tree = IntervalTree()
        node_x = Node(Interval(1, 1))  # current tree root
        node_y = Node(Interval(2, 2))  # future tree root
        tree.insert(node_x)
        tree.insert(node_y)
        tree._rotate_left(node_x)  # pylint:disable=protected-access
        with self.subTest(comment='verify node_x'):
            self.assertEqual(node_x.parent, node_y)
            self.assertIsNone(node_x.left)
            self.assertIsNone(node_x.right)
        with self.subTest(comment='verify node_y'):
            self.assertIsNone(node_y.parent)
            self.assertEqual(node_y.left, node_x)
            self.assertIsNone(node_y.right)