Guest User

Untitled

a guest
Nov 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4. static double minAvg;
  5. public static void main(String[] args) {
  6.  
  7. Scanner sc = new Scanner(System.in);
  8. System.out.println("type in frequency from the keyboard, use , to split.。");
  9. String input = sc.nextLine();
  10. String[] inputArray = input.split(",");
  11. double[] inputFrequency= new double[inputArray.length];
  12.  
  13. for(int i = 0;i < inputFrequency.length;i++) {
  14. inputFrequency[i] = Float.parseFloat(inputArray[i]);
  15. }
  16.  
  17. int n = inputFrequency.length ;
  18. //Root table
  19. int [][]R = new int[n+1][n+1];
  20.  
  21. double A [][] = optst(n,inputFrequency,R);
  22.  
  23.  
  24. showAtable(A);
  25. System.out.println();
  26. System.out.println();
  27. showRtable(R);
  28. System.out.println();
  29. System.out.println();
  30.  
  31. new BinarySearchTree().showRTableTree(R);
  32.  
  33. }
  34.  
  35. //algorithm, find min cost
  36. static double[][] optst(int n, double p[],int R[][]){
  37. int i,j,diagonal;
  38.  
  39. double [][]A = new double[n+1][n+1];
  40.  
  41. for (i=1; i<=n; i++) {
  42. A[i-1][i-1] = 0;
  43. A[i-1][i] = p[i-1];
  44.  
  45. R[i-1][i] = i;
  46. R[i-1][i-1] = 0;
  47. }
  48.  
  49. for(diagonal=1; diagonal<=n-1; diagonal++) {
  50. for(i=1; i<=n-diagonal; i++) {
  51. j = i + diagonal;
  52. double minimum = Double.MAX_VALUE;
  53. double sum = 0;
  54. int minr = 0;
  55.  
  56. for(int k=i; k<=j; k++) {
  57. if (A[i-1][k-1]+A[k][j] < minimum) {
  58. minimum = A[i-1][k-1]+A[k][j];
  59. minr = k;
  60. }
  61. sum += p[k-1];
  62. }
  63. A[i-1][j] = minimum + sum;
  64. R[i-1][j] = minr;
  65. }
  66. }
  67.  
  68. return A;
  69. }
  70.  
  71.  
  72. static void showAtable(double a[][]) {
  73. for(int i=0;i<a.length;i++)
  74. {
  75. for(int z=0;z<a.length;z++) {
  76. System.out.printf("%.3f"+"\t",a[i][z]);
  77.  
  78. }
  79. System.out.println();
  80. }
  81.  
  82. }
  83.  
  84. static void showRtable(int a[][]) {
  85. for(int i=0; i<a.length; i++) {
  86. for(int j=0; j<a[0].length;j++) {
  87. System.out.print(String.valueOf(a[i][j]) + "\t\t");
  88. }
  89. System.out.println();
  90. }
  91. }
  92. }
  93.  
  94.  
  95. class BinarySearchTree {
  96.  
  97.  
  98. public void showRTableTree(int array[][]) {
  99.  
  100. Node node = new Node();
  101. build(array, node, 1, array.length-1, 0, -1);
  102. node.showTree(node);
  103. }
  104. Node build(int array[][], Node node, int leftpivot, int rightpivot, int level, int i) {
  105. if (leftpivot == rightpivot) {//Check if it is a leaf
  106. Node sub = new Node();
  107. sub.node = array[leftpivot][rightpivot];
  108. sub.level = level;
  109. i++;
  110. sub.index = i;
  111. return sub;
  112. }
  113.  
  114. int subRoot = array[leftpivot][rightpivot];//sub Root
  115. node.node = array[leftpivot][rightpivot];
  116. node.level = level;
  117. level ++;
  118.  
  119. int new_leftpivot = subRoot-1;
  120. int new_rightpivot = subRoot+1;
  121.  
  122.  
  123. //find left sub tree
  124. Node newTree = new Node();
  125. if (new_leftpivot>=leftpivot) {
  126. //將傳的左子樹放入次樹的左子樹
  127. node.leftNode = build(array, newTree, leftpivot, new_leftpivot, level, i);
  128. i = node.leftNode.index;
  129. }
  130.  
  131. i++;//has several nodes before it.
  132. node.index = i;
  133.  
  134. //find right sub tree
  135. if (new_rightpivot<=rightpivot) {
  136. node.rightNode = build(array, newTree, new_rightpivot, rightpivot, level, i);
  137. i = node.rightNode.index;
  138. }
  139.  
  140.  
  141. return node;
  142. }
  143.  
  144.  
  145. class Node {
  146. Node leftNode ;
  147. Node rightNode;
  148.  
  149. int node = 0;
  150. int level = 0;
  151. int index = 0;
  152.  
  153. int nowlevel = 0;
  154.  
  155.  
  156. void showTree(Node node) {
  157. if (nowlevel != node.level) {
  158. System.out.println();
  159. nowlevel = node.level;
  160. }
  161. //according to this node has how many nodes before it
  162. for (int i = 0; i< node.index; i++) {
  163. System.out.print("\t\t");
  164. }
  165. System.out.print(node.node);
  166.  
  167. if (node.leftNode != null) {
  168. showTree(node.leftNode);
  169. }
  170.  
  171. if (node.rightNode != null) {
  172. showTree(node.rightNode);
  173. }
  174. }
  175. }
  176.  
  177.  
  178. }
Add Comment
Please, Sign In to add comment