最优二叉搜索树
以排序顺序给定一组整数,另一个数组频次计数。我们的任务是用这些数据创建一个二叉搜索树来查找所有搜索的最低成本。
创建辅助数组 cost[n, n] 来求解和存储子问题的解。成本矩阵会保留按自下而上的方式解决问题的输入。
输入和输出
Input:
The key values as node and the frequency.
Keys = {10, 12, 20}
Frequency = {34, 8, 50}
Output:
The minimum cost is 142.
These are possible BST from the given values.
For case 1, the cost is: (34*1) + (8*2) + (50*3) = 200
For case 2, the cost is: (8*1) + (34*2) + (50*2) = 176.
Similarly for case 5, the cost is: (50*1) + (34 * 2) + (8 * 3) = 142 (Minimum)算法
optCostBst(keys, freq, n)
输入:插入到 BST 的键,每个键的频率,键的数量。
输出:计算出最优 BST 的最小成本。
Begin define cost matrix of size n x n for i in range 0 to n-1, do cost[i, i] := freq[i] done for length in range 2 to n, do for i in range 0 to (n-length+1), do j := i + length – 1 cost[i, j] := ∞ for r in range i to j, done if r > i, then c := cost[i, r-1] else c := 0 if r < j, then c := c + cost[r+1, j] c := c + sum of frequency from i to j if c < cost[i, j], then cost[i, j] := c done done done return cost[0, n-1] End
示例
#include <iostream>
using namespace std;
int sum(int freq[], int low, int high) { //sum of frequency from low to high range
int sum = 0;
for (int k = low; k <=high; k++)
sum += freq[k];
return sum;
}
int minCostBST(int keys[], int freq[], int n) {
int cost[n][n];
for (int i = 0; i < n; i++) //when only one key, move along diagonal elements
cost[i][i] = freq[i];
for (int length=2; length<=n; length++) {
for (int i=0; i<=n-length+1; i++) { //from 0th row to n-length+1 row as i
int j = i+length-1;
cost[i][j] = INT_MAX; //initially store to infinity
for (int r=i; r<=j; r++) {
//find cost when r is root of subtree
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;
}
}
}
return cost[0][n-1];
}
int main() {
int keys[] = {10, 12, 20};
int freq[] = {34, 8, 50};
int n = 3;
cout << "Cost of Optimal BST is: "<< minCostBST(keys, freq, n);
}输出
Cost of Optimal BST is: 142
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP