算法导论 15章(1)最优二叉搜索树

#include <iostream>
#include <vector>
using namespace std;
class Result
{
public:
	Result(int n)
	{
		num = n+2;
		e = new double *[n+2];
		root = new int *[n+2];
		w = new double *[n+2];
		for(int i=0; i<=n+1; i++)
		{
			e[i] = new double [n+2];
			root[i] = new int [n+2];
			w[i] = new double [n+2];
		}
/****************test*******************
		for(i = 0; i<num; i++)
			for(int j=0; j<num; j++)
			{
				e[i][j] = i+j;
				root[i][j] =i+j;
			}
***************test********************/
	}
	void Print()
	{
		for(int i = 0; i<num; i++)
		{
			for(int j = 0; j<num; j++)
			{
				cout << e[i][j] << "  " ;
			}
			cout << endl;
		}
		cout << endl;
		for(i = 0; i<num; i++)
		{
			for(int j = 0; j<num; j++)
			{
				cout << root[i][j] << "  " ;
			}
			cout << endl;
		}	
	}
	void set_w(int i, int j, double num)
	{
		w[i][j] = num;
	}
	void set_e(int i, int j, double num)
	{
		e[i][j] = num;
	}
	void set_root(int i, int j, int num)
	{
		root[i][j] = num;
	}
	double get_w(int i, int j)
	{
		return w[i][j];
	}
	double get_e(int i, int j)
	{
		return e[i][j];
	}
	int get_root(int i, int j)
	{
		return root[i][j];
	}

private:
	double **w;   //保存概率
	double **e;   //保存BST的查找期望
	int **root;  //保存BST的节点信息
	int num;    //保存数组规模
};

Result OPTIMAL_BST(const vector<double> &p, const vector<double> &q, const int &n)
{
	//p中的有效元素只有n个,第0个元素是填充的
	//q中的有效元素有n+1个
	//这里的n指的是查找节点的个数

	Result res(n);
	for(int i = 1; i<=n+1; i++)
	{
		res.set_w(i, i-1, q[i-1]);
		res.set_e(i, i-1, q[i-1]);
	}
	for(int l = 1; l <= n; l++)
		for(int i = 1; i <= n-l+1; i++)
		{
			int j = i+l-1;
			res.set_e(i, j, 99999);
			res.set_w(i, j, (res.get_w(i, j-1))+p[j]+q[j]);   // w[i][j] = w[i][j-1]+p[j]+q[j]
			for(int r = i; r<=j; r++)
			{
				double tmp = res.get_e(i, r-1)+res.get_e(r+1, j)+res.get_w(i, j);
				if(tmp < res.get_e(i, j))
				{
					res.set_e(i, j, tmp);
					res.set_root(i, j, r);
				}
			}
		}
	return res;
}

//打印节点的函数
void Print(Result res, int i, int j, int flag)
{
	if(flag == 1)
	{
		cout << "k" <<res.get_root(i, j) << "是根节点" << endl; 
		flag = 0;
	}
	int r = res.get_root(i, j);

	//如果左子树是叶子
	if(i>r-1)
	{
		//当i比r-1大时,左子树是叶子了,即为d的节点,此时d的下标就是r-1
		cout << "d" << r-1 << "是" << "k" << r << "的左孩子" << endl;
	}
	//如果左子树不是叶子
	else
	{
		cout << "k" << res.get_root(i, r-1) << "是" << "k" << r << "的左孩子" << endl;
		Print(res, i, r-1, 0);
	}

	//如果右子树是叶子
	if(j<r+1)
	{
		cout << "d" << j << "是" << "k" << r << "的左孩子" << endl;
	}
	//如果左子树不是叶子
	else
	{
		cout << "k" << res.get_root(r+1, j) << "是" << "k" << r << "的左孩子" << endl;
		Print(res, r+1, j, 0);
	}
}

int main()
{
	int n;
//	cin >> n;
//	Result res(n);
	vector<double> v1;
	vector<double> v2;
	v1.push_back(0);
	v1.push_back(0.15);
	v1.push_back(0.10);
	v1.push_back(0.05);
	v1.push_back(0.10);
	v1.push_back(0.20);
	v2.push_back(0.05);
	v2.push_back(0.10);
	v2.push_back(0.05);
	v2.push_back(0.05);
	v2.push_back(0.05);
	v2.push_back(0.10);
	Result res(v1.size()-1);
	res = OPTIMAL_BST(v1, v2, v1.size()-1);
	cout << res.get_e(1, 5) << endl;
	Print(res, 1, 5, 1);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值