假设关键字的总数为n,用c[i,j]表示第i个关键字到第j个关键字的最优二叉查找树的代价,我们的目标是求c[0,n-1]。要求c[i,j],首先要从第i个关键字到第j个关键字中选一个出来作为根结点,选出根结点后,最优二叉搜索树的代价为左子树的代价加上右子树的代价,由于每选出一个根结点,每个关键字的搜索长度都增加1,因此得出递推式,即当

 package org.xiu68.ch06.ex6;

 public class Ex6_20 {
//动态规划,最优二叉搜索树
public static void main(String[] args) {
// TODO Auto-generated method stub
double[] w=new double[]{0.05,0.4,0.08,0.04,0.1,0.1,0.23};
String[] words=new String[]{"begin","do","else","end","if","then","while"};
int[][] roots=new int[w.length][w.length]; //i到j之间的二叉搜索树的根结点
optimalBST(w,roots); for(int i=0;i<roots.length;i++){
for(int j=0;j<roots[i].length;j++){
System.out.print(roots[i][j]+"\t");
}
System.out.println();
} printRoot(words, roots,0,w.length-1);
/*
最少平均比较次数为:2.18
0 1 1 1 1 1 1
0 1 1 1 1 1 4
0 0 2 2 4 4 6
0 0 0 3 4 4 6
0 0 0 0 4 4 6
0 0 0 0 0 5 6
0 0 0 0 0 0 6
begin和while的根结点为 do
begin和begin的根结点为 begin
else和while的根结点为 while
else和then的根结点为 if
else和end的根结点为 else
end和end的根结点为 end
then和then的根结点为 then
*/
} //roots中存放i到j之间的二叉搜索树的根结点
public static void optimalBST(double[] w,int[][] roots){
double[][] c=new double[w.length][w.length]; //i到j之间的二叉搜索树的最小代价 for(int i=0;i<w.length;i++){
c[i][i]=w[i]; //树只有自身,则代价为自身的频率
roots[i][i]=i; //(i到j的树的根结点)自身作为根结点
} for(int s=2;s<=w.length;s++){ //s个单词作为子问题(子问题的规模)
for(int i=0;i<=w.length-s;i++){ //s个单词的第一个单词
int j=i+s-1; //s个单词的最后一个单词 double min=Double.MAX_VALUE;
for(int k=i;k<=j;k++){ //寻找使平均查找次数最少的根结点
double cLeft=0; //以k作为根结点的左子树的代价
double cRight=0; //以k作为根结点的右子树的代价
if(k>i)
cLeft=c[i][k-1];
if(k<j)
cRight=c[k+1][j];
if(cLeft+cRight<min){
min=cLeft+cRight;
roots[i][j]=k;
}
}//
double sum=0;
for(int t=i;t<=j;t++){
sum+=w[t];
}
c[i][j]=min+sum;
}//
}//
System.out.println("最少平均比较次数为:"+c[0][w.length-1]);
} //打印最优根
public static void printRoot(String[] words,int[][] roots,int i,int j){
if(i<=j){
int k=roots[i][j];
System.out.println(words[i]+"和"+words[j]+"的根结点为 "+words[k]);
printRoot(words,roots,i,k-1);
printRoot(words,roots,k+1,j);
}
}
}

Ex 6_20 最优二叉搜索树..._第六次作业的更多相关文章

  1. OBST(最优二叉搜索树)

    简述一下问题:假设有一颗词典二叉树,我们从中查找需要的单词,使用红黑树或平衡树这样的数据结构总是可以在O(lgN)时间内进行查找,但单词的出现频率是不同的,我们给每个单词加上一个搜索概率,然后通过这些 ...

  2. OBST(Optimal Binary Tree最优二叉搜索树)

    二叉搜索树 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的 ...

  3. 算法导论( FFT & 自动机 & 最优二叉搜索树 !!!)

    原图链接:(!!!)

  4. 基本数据结构 —— 二叉搜索树(C++实现)

    目录 什么是二叉搜索树 二叉搜索树如何储存数值 二叉搜索树的操作 插入一个数值 查询是否包含某个数值 删除某个数值 测试代码 参考资料 什么是二叉搜索树 二叉搜索树(英语:Binary Search ...

  5. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  6. 二叉查找树 _ 二叉排序树 _ 二叉搜索树_C++

    一.数据结构背景+代码变量介绍 二叉查找树,又名二叉排序树,亦名二叉搜索树 它满足以下定义: 1.任意节点的子树又是一颗二叉查找树,且左子树的每个节点均小于该节点,右子树的每个节点均大于该节点. 2. ...

  7. 数据结构学习笔记_树(二叉搜索树,B-树,B+树,B*树)

    一.查找二叉树(二叉搜索树BST) 1.查找二叉树的性质 1).所有非叶子结点至多拥有两个儿子(Left和Right): 2).所有结点存储一个关键字: 3).非叶子结点的左指针指向小于其关键字的子树 ...

  8. [Swift]LeetCode95. 不同的二叉搜索树 II | Unique Binary Search Trees II

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  9. [Swift]LeetCode96. 不同的二叉搜索树 | Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

随机推荐

  1. 前端基础之html(一)

    https://www.cnblogs.com/haiyan123/p/7516060.html 一.初始html 1.web服务本质 import socket sock=socket.socket ...

  2. genetic model

    如果CC表示野生基型,CA因表示杂合型突变基因型,AA表示纯合型突变基因型.Recessive Model(隐性模型 ):AA VS (CA+CC);Dominant Model(显性模型):(CA+ ...

  3. SQL Server 2012/2016/2017 新增函数

    /************************************************************** SQL Server 2012 新增的函数 ************** ...

  4. ubuntu14安装node0.12.7

    1. 官网下载linux系统二进制文件, 链接如下: https://nodejs.org/ 2. 解压到/opt/目录 3. 设置node环境变量 export NODE_HOME=/opt/nod ...

  5. springmvc 中@Controller和@RestController的区别

    @Controller和@RestController的区别? 官方文档:@RestController is a stereotype annotation that combines @Respo ...

  6. nginx: [warn] duplicate MIME type "text/html"错误

    检查配置文件时提示:nginx: [warn] duplicate MIME type "text/html" in /home/web/nginx/inc/gzip.conf:9 ...

  7. Random类(随机数)

    前言:总是忘记怎么用.上网一查,都是些有的没的...... 最简单却最常用的方法:Random.Next方法 首先,为Random类实例化一个对象: Random n=new Random(); Ne ...

  8. JAVA记录-java代码优化策略

    java代码优化策略 1.生成对象时,合理分配空间和大小:new ArrayList(100); 2.优化for循环: Vector vect = new Vector(1000); For(int ...

  9. js鼠标移入移出效果【原】

    <HTML> <HEAD> <!-- meta 解释 : http://www.haorooms.com/post/html_meta_ds --> <met ...

  10. CentOS6.x下,tomcat - web项目部署

    1. 安装tomcat tomcat安装方法:http://www.cnblogs.com/vurtne-lu/p/6478440.html 2. 配置tomcat 修改server.xml文件 &l ...