算法之动态规划最优二叉树

来源:互联网 发布:华为p9plus隐藏软件 编辑:程序博客网 时间:2024/05/05 07:17
  1. package com.eshore.sweetop.dynamicprogramming;
  2. public class BestBinTree {
  3.     //关键字节点,p[0]为构造数据
  4. //  double[] p={0,0.15,0.10,0.05,0.10,0.20};
  5.     //虚拟节点
  6. //  double[] q={0.05,0.10,0.05,0.05,0.05,0.10};
  7.     //规模
  8. //  int n=5;
  9.     
  10.     double[] p={0,0.04,0.06,0.08,0.02,0.10,0.12,0.14};
  11.     double[] q={0.06,0.06,0.06,0.06,0.05,0.05,0.05,0.05};
  12.     static int n=7;
  13.     
  14.     double[][] e=new double[n+2][n+1];
  15.     double[][] w=new double[n+2][n+1];
  16.     int[][] root=new int[n+2][n+2];
  17.     
  18.     public void optimalBest(){
  19.         for (int i = 1; i < e.length; i++) {
  20.             e[i][i-1]=q[i-1];
  21.             w[i][i-1]=q[i-1];
  22.         }
  23.         
  24.         for (int l = 1; l < n+1; l++) {
  25.             for (int i = 1; i <= n-l+1; i++) {
  26.                 int j=i+l-1;
  27.                 e[i][j]=Double.MAX_VALUE;
  28.                 w[i][j]=w[i][j-1]+p[j]+q[j];
  29.                 for (int r = i; r <= j; r++) {
  30.                     double t=e[i][r-1]+e[r+1][j]+w[i][j];
  31.                     if(t<e[i][j]){
  32.                         e[i][j]=t;
  33.                         System.out.println(r);
  34.                         root[i][j]=r;
  35.                     }
  36.                 }
  37.             }   
  38.         }
  39. //      BestBinTree.display(w);
  40. //      BestBinTree.display(e); 
  41.         BestBinTree.display(root);
  42.         
  43.         BestBinTree.viewTree(root);
  44.     }
  45.     
  46.     public static void display(double[][] a){
  47.         for (int i = 1; i < a.length; i++) {
  48.             for (int j = 1; j < a[0].length; j++) {
  49.                 System.out.print(String.format("%.2f", a[i][j])+"/t");
  50.             }
  51.             System.out.println();
  52.         }
  53.     }
  54.     
  55.     public static void display(int[][] a){
  56.         for (int i = 1; i < a.length; i++) {
  57.             for (int j = 1; j < a[0].length; j++) {
  58.                 if(i<=j)
  59.                 System.out.print(a[i][j]+" ");
  60.             }
  61.             System.out.println();
  62.         }
  63.     }
  64.     
  65.     public static void viewTree(int[][] a){
  66.         System.out.println("根是k"+a[1][n]);
  67.         view(a,a[1][n],1,n);
  68. //      System.out.println(a[6][7]);
  69.     }
  70.     
  71.     public static void view(int[][] a,int root,int i,int j){
  72.         if(a[i][root-1]==0){
  73.             System.out.println("k"+root+"左孩子是d"+(root-1));
  74.         }else{
  75.             System.out.println("k"+root+"左孩子是k"+a[i][root-1]);  
  76.             view(a,a[i][root-1],i,root-1);
  77.         }
  78.         if(a[root+1][j]==0){
  79.             System.out.println("k"+root+"右孩子是d"+root);
  80.         }else{
  81.             System.out.println("k"+root+"右孩子是k"+a[root+1][j]);
  82.             view(a,a[root+1][j],root+1,j);
  83.         }
  84.     }
  85.     
  86.     
  87.     public static void main(String[] args) {
  88.         BestBinTree bbt=new BestBinTree();
  89.         bbt.optimalBest();
  90.     }
  91.     
  92. }
原创粉丝点击