|
| 1 | +# Optimal Binary Search Tree |
| 2 | + |
| 3 | +## English |
| 4 | + |
| 5 | +The optimal binary search tree problem is a classic dynamic programming problem that aims to minimize the expected search cost based on access frequencies. It has applications in compiler design and database indexing. |
| 6 | + |
| 7 | +This challenge involves implementing a dynamic programming algorithm to construct an optimal binary search tree minimizing the average search cost. |
| 8 | + |
| 9 | +### Relevant Code Snippet |
| 10 | + |
| 11 | +```javascript |
| 12 | +class OptimalBST { |
| 13 | + constructor(keys, freq) { |
| 14 | + this.keys = keys; |
| 15 | + this.freq = freq; |
| 16 | + this.n = keys.length; |
| 17 | + this.dp = Array.from({ length: this.n }, () => new Array(this.n).fill(0)); |
| 18 | + } |
| 19 | + |
| 20 | + optimalCost() { |
| 21 | + const prefixSum = new Array(this.n + 1).fill(0); |
| 22 | + for (let i = 0; i < this.n; i++) { |
| 23 | + prefixSum[i + 1] = prefixSum[i] + this.freq[i]; |
| 24 | + } |
| 25 | + |
| 26 | + const getSum = (i, j) => prefixSum[j + 1] - prefixSum[i]; |
| 27 | + |
| 28 | + const costForRoot = (i, j, r) => { |
| 29 | + const left = r > i ? this.dp[i][r - 1] : 0; |
| 30 | + const right = r < j ? this.dp[r + 1][j] : 0; |
| 31 | + return left + right + getSum(i, j); |
| 32 | + }; |
| 33 | + |
| 34 | + for (let i = 0; i < this.n; i++) { |
| 35 | + this.dp[i][i] = this.freq[i]; |
| 36 | + } |
| 37 | + |
| 38 | + for (let length = 2; length <= this.n; length++) { |
| 39 | + for (let i = 0; i <= this.n - length; i++) { |
| 40 | + const j = i + length - 1; |
| 41 | + let minCost = Infinity; |
| 42 | + for (let r = i; r <= j; r++) { |
| 43 | + const cost = costForRoot(i, j, r); |
| 44 | + if (cost < minCost) { |
| 45 | + minCost = cost; |
| 46 | + } |
| 47 | + } |
| 48 | + this.dp[i][j] = minCost; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return this.dp[0][this.n - 1]; |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### History |
| 58 | + |
| 59 | +The optimal binary search tree problem has been widely studied in computer science and has practical applications in compiler design and database indexing. |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Español |
| 64 | + |
| 65 | +Árbol Binario de Búsqueda Óptimo |
| 66 | + |
| 67 | +El problema del árbol binario de búsqueda óptimo es un problema clásico de programación dinámica que busca minimizar el costo esperado de búsqueda basado en las frecuencias de acceso. Tiene aplicaciones en diseño de compiladores e indexación de bases de datos. |
| 68 | + |
| 69 | +Este reto consiste en implementar un algoritmo de programación dinámica para construir un árbol binario de búsqueda óptimo que minimice el costo promedio de búsqueda. |
| 70 | + |
| 71 | +### Fragmento de Código Relevante |
| 72 | + |
| 73 | +```javascript |
| 74 | +class OptimalBST { |
| 75 | + constructor(keys, freq) { |
| 76 | + this.keys = keys; |
| 77 | + this.freq = freq; |
| 78 | + this.n = keys.length; |
| 79 | + this.dp = Array.from({ length: this.n }, () => new Array(this.n).fill(0)); |
| 80 | + } |
| 81 | + |
| 82 | + optimalCost() { |
| 83 | + const prefixSum = new Array(this.n + 1).fill(0); |
| 84 | + for (let i = 0; i < this.n; i++) { |
| 85 | + prefixSum[i + 1] = prefixSum[i] + this.freq[i]; |
| 86 | + } |
| 87 | + |
| 88 | + const getSum = (i, j) => prefixSum[j + 1] - prefixSum[i]; |
| 89 | + |
| 90 | + const costForRoot = (i, j, r) => { |
| 91 | + const left = r > i ? this.dp[i][r - 1] : 0; |
| 92 | + const right = r < j ? this.dp[r + 1][j] : 0; |
| 93 | + return left + right + getSum(i, j); |
| 94 | + }; |
| 95 | + |
| 96 | + for (let i = 0; i < this.n; i++) { |
| 97 | + this.dp[i][i] = this.freq[i]; |
| 98 | + } |
| 99 | + |
| 100 | + for (let length = 2; length <= this.n; length++) { |
| 101 | + for (let i = 0; i <= this.n - length; i++) { |
| 102 | + const j = i + length - 1; |
| 103 | + let minCost = Infinity; |
| 104 | + for (let r = i; r <= j; r++) { |
| 105 | + const cost = costForRoot(i, j, r); |
| 106 | + if (cost < minCost) { |
| 107 | + minCost = cost; |
| 108 | + } |
| 109 | + } |
| 110 | + this.dp[i][j] = minCost; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return this.dp[0][this.n - 1]; |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Historia |
| 120 | + |
| 121 | +El problema del árbol binario de búsqueda óptimo ha sido ampliamente estudiado en ciencias de la computación y tiene aplicaciones prácticas en diseño de compiladores e indexación de bases de datos. |
0 commit comments