CodexBloom - Programming Q&A Platform

Difficulty Implementing an Optimal Binary Search Tree Algorithm in Java - Suboptimal Tree Structure

👀 Views: 16 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-03
algorithm java binary-search-tree Java

I need help solving I'm upgrading from an older version and I'm currently trying to implement an Optimal Binary Search Tree (OBST) algorithm in Java, but I'm facing an issue where the tree structure doesn't seem optimal. My current implementation is based on dynamic programming, where I calculate the cost for each subtree, but the final tree doesn't reflect the minimum cost. Here's a simplified version of my implementation: ```java public class OptimalBST { private int[][] cost; private int[][] root; private int[] freq; private int n; public OptimalBST(int[] freq) { this.freq = freq; this.n = freq.length; cost = new int[n][n]; root = new int[n][n]; buildOptimalBST(); } private void buildOptimalBST() { for (int i = 0; i < n; i++) { cost[i][i] = freq[i]; root[i][i] = i; } for (int len = 2; len <= n; len++) { for (int i = 0; i <= n - len; i++) { int j = i + len - 1; cost[i][j] = Integer.MAX_VALUE; for (int r = i; r <= j; r++) { int c = (r > i ? cost[i][r - 1] : 0) + (r < j ? cost[r + 1][j] : 0) + sum(freq, i, j); if (c < cost[i][j]) { cost[i][j] = c; root[i][j] = r; } } } } } private int sum(int[] freq, int i, int j) { int total = 0; for (int k = i; k <= j; k++) { total += freq[k]; } return total; } public void printOptimalTree() { printTree(0, n - 1); } private void printTree(int i, int j) { if (i > j) return; int r = root[i][j]; System.out.println("Node: " + r + " (Frequency: " + freq[r] + ")"); printTree(i, r - 1); printTree(r + 1, j); } } ``` I've tested the implementation with various frequency distributions, but the resulting tree does not seem to minimize the overall search cost. For instance, with frequencies `{10, 30, 50}` for keys `{A, B, C}`, I expected the algorithm to yield a structure that prioritizes the most frequently accessed keys, but the output has B as the root, which is not optimal. I'm getting the following output for the tree structure: ``` Node: 1 (Frequency: 30) Node: 0 (Frequency: 10) Node: 2 (Frequency: 50) ``` This structure suggests that the frequency of B is dominating, but with the frequency of C being higher, it should ideally be the root. I've double-checked the cost calculations, and they seem accurate, but I'm wondering if there's a specific detail or edge case I'm missing in the dynamic programming approach. Any insights or suggestions would be greatly appreciated! I'd really appreciate any guidance on this. For reference, this is a production REST API.