|
| 1 | +package com.thealgorithms.dynamicprogramming; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +class OptimalBinarySearchTreeTest { |
| 13 | + |
| 14 | + @ParameterizedTest |
| 15 | + @MethodSource("validTestCases") |
| 16 | + void testFindOptimalCost(int[] keys, int[] frequencies, long expectedCost) { |
| 17 | + assertEquals(expectedCost, OptimalBinarySearchTree.findOptimalCost(keys, frequencies)); |
| 18 | + } |
| 19 | + |
| 20 | + private static Stream<Arguments> validTestCases() { |
| 21 | + return Stream.of(Arguments.of(new int[] {}, new int[] {}, 0L), Arguments.of(new int[] {15}, new int[] {9}, 9L), Arguments.of(new int[] {10, 12}, new int[] {34, 50}, 118L), Arguments.of(new int[] {20, 10, 30}, new int[] {50, 34, 8}, 134L), |
| 22 | + Arguments.of(new int[] {12, 10, 20, 42, 25, 37}, new int[] {8, 34, 50, 3, 40, 30}, 324L), Arguments.of(new int[] {1, 2, 3}, new int[] {0, 0, 0}, 0L)); |
| 23 | + } |
| 24 | + |
| 25 | + @ParameterizedTest |
| 26 | + @MethodSource("crossCheckTestCases") |
| 27 | + void testFindOptimalCostAgainstBruteForce(int[] keys, int[] frequencies) { |
| 28 | + assertEquals(bruteForceOptimalCost(keys, frequencies), OptimalBinarySearchTree.findOptimalCost(keys, frequencies)); |
| 29 | + } |
| 30 | + |
| 31 | + private static Stream<Arguments> crossCheckTestCases() { |
| 32 | + return Stream.of(Arguments.of(new int[] {3, 1, 2}, new int[] {4, 2, 6}), Arguments.of(new int[] {5, 2, 8, 6}, new int[] {3, 7, 1, 4}), Arguments.of(new int[] {9, 4, 11, 2}, new int[] {1, 8, 2, 5})); |
| 33 | + } |
| 34 | + |
| 35 | + @ParameterizedTest |
| 36 | + @MethodSource("invalidTestCases") |
| 37 | + void testFindOptimalCostInvalidInput(int[] keys, int[] frequencies) { |
| 38 | + assertThrows(IllegalArgumentException.class, () -> OptimalBinarySearchTree.findOptimalCost(keys, frequencies)); |
| 39 | + } |
| 40 | + |
| 41 | + private static Stream<Arguments> invalidTestCases() { |
| 42 | + return Stream.of(Arguments.of(null, new int[] {}), Arguments.of(new int[] {}, null), Arguments.of(new int[] {1, 2}, new int[] {3}), Arguments.of(new int[] {1, 1}, new int[] {2, 3}), Arguments.of(new int[] {1, 2}, new int[] {3, -1})); |
| 43 | + } |
| 44 | + |
| 45 | + private static long bruteForceOptimalCost(int[] keys, int[] frequencies) { |
| 46 | + int[][] sortedNodes = new int[keys.length][2]; |
| 47 | + for (int index = 0; index < keys.length; index++) { |
| 48 | + sortedNodes[index][0] = keys[index]; |
| 49 | + sortedNodes[index][1] = frequencies[index]; |
| 50 | + } |
| 51 | + Arrays.sort(sortedNodes, java.util.Comparator.comparingInt(node -> node[0])); |
| 52 | + |
| 53 | + int[] sortedFrequencies = new int[sortedNodes.length]; |
| 54 | + for (int index = 0; index < sortedNodes.length; index++) { |
| 55 | + sortedFrequencies[index] = sortedNodes[index][1]; |
| 56 | + } |
| 57 | + |
| 58 | + return bruteForceOptimalCost(sortedFrequencies, 0, sortedFrequencies.length - 1, 1); |
| 59 | + } |
| 60 | + |
| 61 | + private static long bruteForceOptimalCost(int[] frequencies, int start, int end, int depth) { |
| 62 | + if (start > end) { |
| 63 | + return 0L; |
| 64 | + } |
| 65 | + |
| 66 | + long minimumCost = Long.MAX_VALUE; |
| 67 | + for (int root = start; root <= end; root++) { |
| 68 | + long currentCost = (long) depth * frequencies[root] + bruteForceOptimalCost(frequencies, start, root - 1, depth + 1) + bruteForceOptimalCost(frequencies, root + 1, end, depth + 1); |
| 69 | + minimumCost = Math.min(minimumCost, currentCost); |
| 70 | + } |
| 71 | + return minimumCost; |
| 72 | + } |
| 73 | +} |
0 commit comments