|
| 1 | +// Optimal Binary Search Tree Algorithm in Rust |
| 2 | +// Time Complexity: O(n^3) with prefix sum optimization |
| 3 | +// Space Complexity: O(n^2) for the dp table and prefix sum array |
| 4 | + |
| 5 | +/// Constructs an Optimal Binary Search Tree from a list of key frequencies. |
| 6 | +/// The goal is to minimize the expected search cost given key access frequencies. |
| 7 | +/// |
| 8 | +/// # Arguments |
| 9 | +/// * `freq` - A slice of integers representing the frequency of key access |
| 10 | +/// |
| 11 | +/// # Returns |
| 12 | +/// * An integer representing the minimum cost of the optimal BST |
| 13 | +pub fn optimal_search_tree(freq: &[i32]) -> i32 { |
| 14 | + let n = freq.len(); |
| 15 | + if n == 0 { |
| 16 | + return 0; |
| 17 | + } |
| 18 | + |
| 19 | + // dp[i][j] stores the cost of optimal BST that can be formed from keys[i..=j] |
| 20 | + let mut dp = vec![vec![0; n]; n]; |
| 21 | + |
| 22 | + // prefix_sum[i] stores sum of freq[0..i] |
| 23 | + let mut prefix_sum = vec![0; n + 1]; |
| 24 | + for i in 0..n { |
| 25 | + prefix_sum[i + 1] = prefix_sum[i] + freq[i]; |
| 26 | + } |
| 27 | + |
| 28 | + // Base case: Trees with only one key |
| 29 | + for i in 0..n { |
| 30 | + dp[i][i] = freq[i]; |
| 31 | + } |
| 32 | + |
| 33 | + // Build chains of increasing length l (from 2 to n) |
| 34 | + for l in 2..=n { |
| 35 | + for i in 0..=n - l { |
| 36 | + let j = i + l - 1; |
| 37 | + dp[i][j] = i32::MAX; |
| 38 | + |
| 39 | + // Compute the total frequency sum in the range [i..=j] using prefix sum |
| 40 | + let fsum = prefix_sum[j + 1] - prefix_sum[i]; |
| 41 | + |
| 42 | + // Try making each key in freq[i..=j] the root of the tree |
| 43 | + for r in i..=j { |
| 44 | + // Cost of left subtree |
| 45 | + let left = if r > i { dp[i][r - 1] } else { 0 }; |
| 46 | + // Cost of right subtree |
| 47 | + let right = if r < j { dp[r + 1][j] } else { 0 }; |
| 48 | + |
| 49 | + // Total cost = left + right + sum of frequencies (fsum) |
| 50 | + let cost = left + right + fsum; |
| 51 | + |
| 52 | + // Choose the minimum among all possible roots |
| 53 | + if cost < dp[i][j] { |
| 54 | + dp[i][j] = cost; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Minimum cost of the optimal BST storing all keys |
| 61 | + dp[0][n - 1] |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use super::*; |
| 67 | + |
| 68 | + // Macro to generate multiple test cases for the optimal_search_tree function |
| 69 | + macro_rules! optimal_bst_tests { |
| 70 | + ($($name:ident: $input:expr => $expected:expr,)*) => { |
| 71 | + $( |
| 72 | + #[test] |
| 73 | + fn $name() { |
| 74 | + let freq = $input; |
| 75 | + assert_eq!(optimal_search_tree(freq), $expected); |
| 76 | + } |
| 77 | + )* |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + optimal_bst_tests! { |
| 82 | + // Common test cases |
| 83 | + test_case_1: &[34, 10, 8, 50] => 180, |
| 84 | + test_case_2: &[10, 12] => 32, |
| 85 | + test_case_3: &[10, 12, 20] => 72, |
| 86 | + test_case_4: &[25, 10, 20] => 95, |
| 87 | + test_case_5: &[4, 2, 6, 3] => 26, |
| 88 | + |
| 89 | + // Edge test cases |
| 90 | + test_case_single: &[42] => 42, |
| 91 | + test_case_empty: &[] => 0, |
| 92 | + } |
| 93 | +} |
0 commit comments