动态规划之最优二叉搜索树的结构 C++实现
2016-11-08 17:50
513 查看
动态规划之最优二叉搜索树的结构 C++实现
原理
根据每个元素出现的频率,对应一个概率值,计算其整个二叉树的期望搜索代价,确定最优二叉树的结构。源代码
#include <iostream> #include <utility> #include <vector> using namespace std; //Probability. vector<double> temp_dblP = { 0.0,0.15,0.10,0.05,0.10,0.20 }, temp_dblQ = { 0.05,0.10,0.05,0.05,0.05,0.10 }; //Just Memoized of Optimal_BST pair<vector<vector<double>>, vector<vector<int>>> Optimal_BST(const vector<double> &temp_dblP, const vector<double> &temp_dblQ, const size_t &temp_n) { vector<vector<double>> temp_VecE, temp_VecW; vector<vector<int>> temp_VecRoot; temp_VecE.resize(temp_n + 2); temp_VecW.resize(temp_n + 2); temp_VecRoot.resize(temp_n + 1); for(auto &i : temp_VecE) { i.resize(temp_n + 1); } for(auto &i : temp_VecW) { i.resize(temp_n + 1); } for(auto &i : temp_VecRoot) { i.resize(temp_n + 1); } for(auto i = 1; i <= temp_n + 1; ++i) { temp_VecE[i][i - 1] = temp_dblQ[i - 1]; temp_VecW[i][i - 1] = temp_dblQ[i - 1]; } for(auto l = 1; l <= temp_n; ++l) { for (auto i = 1; i <= temp_n - l + 1; ++i) { auto j = i + l - 1; temp_VecE[i][j] = DBL_MAX; temp_VecW[i][j] = temp_VecW[i][j - 1] + temp_dblP[j] + temp_dblQ[j]; for (auto r = i; r <= j; ++r) { auto temp_t = temp_VecE[i][r - 1] + temp_VecE[r + 1][j] + temp_VecW[i][j]; if(temp_t < temp_VecE[i][j]) { temp_VecE[i][j] = temp_t; temp_VecRoot[i][j] = r; } } } } return make_pair(temp_VecE, temp_VecRoot); } void Print_BST(vector<vector<int>> const &temp_VecRoot, size_t const &temp_i, size_t const &temp_j, size_t const &temp_r) { auto RootChild = 0; if(temp_i < temp_dblP.size() && temp_j < temp_dblP.size()) { RootChild = temp_VecRoot[temp_i][temp_j]; } if (RootChild == temp_VecRoot[1][temp_dblP.size() - 1]) { cout << "k" << RootChild << " is root" << endl; Print_BST(temp_VecRoot, temp_i, RootChild - 1, RootChild); Print_BST(temp_VecRoot, RootChild + 1, temp_j, RootChild); return; } if (temp_j < temp_i - 1) { return; } if(temp_j == temp_i - 1) { if(temp_j < temp_r) { cout << "d" << temp_j << " is " << "k" << temp_r << "'s left child" << endl; } else { cout << "d" << temp_j << " is " << "k" << temp_r << "'s right child" << endl; } return; } if (RootChild < temp_r) { cout << "k" << RootChild << " is " << "k" << temp_r << "'s left child" << endl; } else { cout << "k" << RootChild << " is " << "k" << temp_r << "'s right child" << endl; } Print_BST(temp_VecRoot, temp_i, RootChild - 1, RootChild); Print_BST(temp_VecRoot, RootChild + 1, temp_j, RootChild); } int main() { auto temp_pair = Optimal_BST(temp_dblP, temp_dblQ, temp_dblP.size() - 1); for(auto &i : temp_pair.first) { for(auto &j : i) { cout << j << " "; } cout << endl; } cout << endl; for (auto &i : temp_pair.second) { for (auto &j : i) { cout << j << " "; } cout << endl; } cout << endl; Print_BST(temp_pair.second, 1, temp_dblP.size() - 1, 0); return 0; }
相关文章推荐
- 【动态规划】最优二叉搜索树C++实现
- 二叉搜索树 -【动态规划】最优C++实现
- C++数据结构——二叉搜索树(实现自定义迭代器)
- 最优二叉查找树的期望搜索代价(动态规划)C++实现
- 完全二叉搜索树结构的C++实现
- 最优二叉查找树的期望搜索代价(动态规划)C++实现
- 使用C++实现二叉搜索树的数据结构
- C++ 二叉搜索树(BST)的实现方法
- c++实现文件传输之一:框架结构和界面实现
- c++中map容器简单功能实现测试附带结构体指针测试
- ACM最长单调递增子序列问题(动态规划)o(n*n)C++实现
- 使用动态分配的链式结构实现的二叉搜索树
- 用C++实现插件体系结构
- 数据结构(C++实现)——变长顺序表的实现
- 数据结构之C++实现图(Graph)(无主函数)
- 数据结构(13)线性表之C++实现一元多项式相减
- C++ 二叉搜索树(查找树)的实现
- 数据结构与算法——不相交集类的C++实现
- 二叉搜索树的c++实现
- 数据结构与算法之枚举(穷举)法 C++实现