def test_solve_invalid(self):
        """Test solve (invalid)

        Args:
            self: TestCheckBinaryTreeIsFull

        Returns:
            None

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

        root2 = BinaryTreeNode(5)
        root2.left = BinaryTreeNode(4)
        root2.right = BinaryTreeNode(7)
        root2.right.right = BinaryTreeNode(8)

        sub_tree_problem = CheckBinaryTreeIsSubtree(root1, root2)

        # Then
        self.assertFalse(sub_tree_problem.solve())
コード例 #2
0
    def test_solve(self):
        """Test solve

        Args:
            self: TestBottomViewOfBinaryTree

        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)

        bottom_view_problem = BottomViewOfBinaryTree(root)

        # When
        result = bottom_view_problem.solve()

        # Then
        self.assertEqual(result, [2, 4, 6, 7, 8])
コード例 #3
0
    def test_post_order_traversal(self):
        """Test for post_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.post_order(root, result)

        # Then
        self.assertEqual(result, [2, 4, 6, 8, 7, 5, 3])
コード例 #4
0
    def test_construct(self):
        """Test for construct

        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)

        # Then
        self.assertEqual(root.data, 3)
        self.assertEqual(root.left.data, 2)
        self.assertEqual(root.right.data, 5)
        self.assertEqual(root.right.left.data, 4)
        self.assertEqual(root.right.right.data, 7)
        self.assertEqual(root.right.right.right.data, 8)
        self.assertEqual(root.right.right.left.data, 6)
    def test_solve_in_valid(self):
        """Test solve (in valid)

        Args:
            self: TestValidateBinarySearchTreeBruteForce

        Returns:
            None

        Raises:
            None
        """
        # Given
        root = BinaryTreeNode(3)
        root.left = BinaryTreeNode(2)
        root.right = BinaryTreeNode(5)
        root.right.left = BinaryTreeNode(1)  # should be greater than 3
        root.right.right = BinaryTreeNode(7)

        validate_binary_search_tree_problem = ValidateBinarySearchTreeBruteForce(
            root)

        # When
        is_valid = validate_binary_search_tree_problem.solve()

        # Then
        self.assertFalse(is_valid)
    def test_solve(self):
        """Test solve

        Args:
            self: TestValidateBinarySearchTreeBruteForce

        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)

        validate_binary_search_tree_problem = ValidateBinarySearchTreeBruteForce(
            root)

        # When
        is_valid = validate_binary_search_tree_problem.solve()

        # Then
        self.assertTrue(is_valid)
    def test_solve(self):
        """Test solve

        Args:
            self: TestMinDepthOfBinaryTreeDepthFirst

        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)

        min_depth_problem = MinDepthOfBinaryTreeDepthFirst(root)

        # When
        depth = min_depth_problem.solve()

        # Then
        self.assertEqual(depth, 2)
    def test_solve_invalid(self):
        """Test solve (invalid)

        Args:
            self: TestCheckBinaryTreeIsFull

        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)

        full_tree_problem = CheckBinaryTreeIsFull(root)

        # Then
        self.assertFalse(full_tree_problem.solve())
    def test_solve(self):
        """Test solve

        Args:
            self: TestFindDistanceBetweenTwoNodes

        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)

        distance_problem = FindDistanceBetweenTwoNodes(root, 2, 6)

        # When
        result = distance_problem.solve()

        # Then
        self.assertEqual(result, 4)
    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])
コード例 #11
0
    def test_solve_not_balanced(self):
        """Test solve (Not Balanced)

        Args:
            self: TestBalancedBinaryTreeRecursion

        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)

        balanced_tree_problem = BalancedBinaryTreeRecursion(root)

        # When
        is_balanced = balanced_tree_problem.solve()

        # Then
        self.assertFalse(is_balanced)
コード例 #12
0
    def test_solve(self):
        """Test solve

        Args:
            self: TestBinaryTreeMaxSumPath

        Returns:
            None

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

        root.left.left = BinaryTreeNode(1)
        root.right = BinaryTreeNode(5)
        root.right.left = BinaryTreeNode(4)
        root.right.right = BinaryTreeNode(7)

        max_sum_path_problem = BinaryTreeMaxSumPath(root)

        # When
        max_sum = max_sum_path_problem.solve()

        # Then
        self.assertEqual(max_sum, 18)
    def test_solve(self):
        """Test solve

        Args:
            self: TestBalancedBinaryTreeBruteForce

        Returns:
            None

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

        root.left.left = BinaryTreeNode(1)
        root.right = BinaryTreeNode(5)
        root.right.left = BinaryTreeNode(4)
        root.right.right = BinaryTreeNode(7)

        balanced_tree_problem = BalancedBinaryTreeBruteForce(root)

        # When
        is_balanced = balanced_tree_problem.solve()

        # Then
        self.assertTrue(is_balanced)
コード例 #14
0
    def test_solve(self):
        """Test solve

        Args:
            self: TestFindLowestCommonAncestor

        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)

        lca_problem = FindLowestCommonAncestor(root, 2, 6)

        # When
        result = lca_problem.solve()

        # Then
        self.assertEqual(result.data, 3)
コード例 #15
0
    def sorted_array_to_bst(self, number_list, start, end):
        """Convert sorted array to Binary Search Tree

        Args:
            number_list: input list
            start: start index
            end: end index

        Returns:
            BinaryTreeNode

        Raises:
            None
        """
        if start > end:
            return None

        middle = int((start + end) / 2)

        node = BinaryTreeNode(number_list[middle])
        node.left = self.sorted_array_to_bst(number_list, start, middle - 1)
        node.right = self.sorted_array_to_bst(number_list, middle + 1, end)

        return node