Beispiel #1
0
    def test_solve(self):
        """Test solve

        Args:
            self: TestConvertSortedArrayToBST

        Returns:
            None

        Raises:
            None
        """
        # Given
        input_list = [range(1, 10)]
        covert_to_bst_problem = ConvertSortedArrayToBST(input_list)

        # When
        node = covert_to_bst_problem.solve()

        # in order traversal returns the binary search tree in sorted order
        in_order_result = []
        BinaryTreeNode.in_order(node, in_order_result)

        # Then
        self.assertEqual(in_order_result, input_list)
    def test_in_order_traversal(self):
        """Test for in_order

        Args:
            self: TestBinaryTree

        Returns:
            None

        Raises:
            None
        """
        # Given
        root = BinaryTreeNode(3)
        root.left = BinaryTreeNode(2)
        root.right = BinaryTreeNode(5)
        root.right.left = BinaryTreeNode(4)
        root.right.right = BinaryTreeNode(7)
        root.right.right.right = BinaryTreeNode(8)
        root.right.right.left = BinaryTreeNode(6)

        # When
        result = []
        BinaryTreeNode.in_order(root, result)

        # Then
        self.assertEqual(result, [2, 3, 4, 5, 6, 7, 8])
    def test_solve(self):
        """Test solve

        Args:
            self: TestBinaryTreeUpsideDown

        Returns:
            None

        Raises:
            None
        """
        # Given
        root = BinaryTreeNode(1)
        root.left = BinaryTreeNode(2)

        root.left.left = BinaryTreeNode(4)
        root.left.right = BinaryTreeNode(5)
        root.right = BinaryTreeNode(3)

        upside_down_problem = BinaryTreeUpsideDown(root)

        # When
        node = upside_down_problem.solve()

        # Then
        elements_list = []
        BinaryTreeNode.in_order(node, elements_list)
        self.assertEqual(elements_list, [5, 4, 3, 2, 1])