动态规划之最优二叉搜索树的结构 C++实现

动态规划之最优二叉搜索树的结构 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值