fork download
  1. import java.io.*;
  2. import java.util.*;
  3. class Optimal
  4. {
  5. public int p[];
  6. public int q[];
  7. public int a[];
  8. public int w[][];
  9. public int c[][];
  10. public int r[][];
  11. public int n;
  12. int front,rear,queue[];
  13. public Optimal(int SIZE)
  14. {
  15. p=new int[SIZE]; q= new int[SIZE]; a=new int[SIZE];
  16. w=new int[SIZE][SIZE]; c=new int[SIZE][SIZE]; r=new int[SIZE][SIZE];
  17. queue=new int[SIZE]; front=rear=-1;
  18. }
  19. /* This function returns a value in the range r[i][j-1] to r[i+1][j] SO that the cost c[i][k-1] + c[k][j] is minimum */
  20. public int Min_Value(int i, int j)
  21. {
  22. int m,k=0;
  23. int minimum = 32000;
  24. for (m=r[i][j-1] ; m<=r[i+1][j] ; m++)
  25. {
  26. if ((c[i][m-1]+c[m][j]) < minimum)
  27. {
  28. minimum = c[i][m-1] + c[m][j]; k = m;
  29. }
  30. }
  31. return k;
  32. }
  33. /* This function builds the table from all the given probabilities It basically computes C,r,W values */
  34. public void OBST()
  35. {
  36. int i, j, k, l, m;
  37.  
  38. for (i=0 ; i<n ; i++)
  39.  
  40. {
  41. // Initialize
  42. w[i][i] = q[i];
  43. r[i][i] = c[i][i] = 0;
  44. // Optimal trees with one node
  45. w[i][i+1] = q[i] + q[i+1] + p[i+1]; r[i][i+1] = i+1;
  46. c[i][i+1] = q[i] + q[i+1] + p[i+1];
  47. }
  48. w[n][n] = q[n];
  49. r[n][n] = c[n][n] = 0;
  50. // Find optimal trees with m nodes
  51. for (m=2 ; m<=n ; m++)
  52. {
  53. for (i=0 ; i<=n-m ; i++)
  54. {
  55. j = i+m;
  56. w[i][j] = w[i][j-1] + p[j] + q[j]; k = Min_Value(i,j);
  57. c[i][j] = w[i][j] + c[i][k-1] + c[k][j]; r[i][j] = k;
  58. }
  59. }
  60. }
  61. /*This function builds the tree from the tables made by the OBST function */
  62. public void build_tree()
  63. {
  64. int i, j, k;
  65. System.out.print("The Optimal Binary Search Tree For The Given Nodes Is \n");
  66. System.out.print("\n The Root of this OBST is :: "+r[0][n]);
  67. System.out.print("\n The Cost Of this OBST is :: "+c[0][n]);
  68. System.out.print("\n\n\tNODE\tLEFT CHILD\tRIGHT CHILD");
  69. System.out.println("\n "); queue[++rear] = 0;
  70. queue[++rear] = n; while(front != rear)
  71. {
  72. i = queue[++front]; j = queue[++front]; k = r[i][j];
  73. System.out.print("\n "+k); if (r[i][k-1] != 0)
  74. {
  75. System.out.print(" "+r[i][k-1]); queue[++rear] = i; queue[++rear] = k-1;
  76. }
  77. else System.out.print(" -");
  78. if(r[k][j] != 0)
  79. {
  80. System.out.print(" "+r[k][j]); queue[++rear] = k; queue[++rear] = j;
  81. }
  82. else
  83. System.out.print(" -");
  84. }
  85. System.out.println("\n");
  86. }
  87. }
  88. /* This is the main function */
  89. class OBSTDemo
  90. {
  91. public static void main (String[] args ) throws IOException,NullPointerException
  92. {
  93. Optimal obj=new Optimal(10); int i;
  94. System.out.print("\n Optimal Binary Search Tree \n");
  95. System.out.print("\n Enter the number of nodes");
  96. obj.n=getInt();
  97. System.out.print("\n Enter the data as \n");
  98. for (i=1;i<=obj.n;i++)
  99. {
  100. System.out.print("\n a["+i+"]"); obj.a[i]=getInt();
  101. }
  102. for (i=1 ; i<=obj.n ; i++)
  103. {
  104. System.out.println("p["+i+"]"); obj.p[i]=getInt();
  105. }
  106. for (i=0 ; i<=obj.n ; i++)
  107. {
  108. System.out.print("q["+i+"]"); obj.q[i]=getInt();
  109. } obj.OBST();
  110. obj.build_tree();
  111. }
  112. public static String getString() throws IOException
  113. {
  114. InputStreamReader input = new InputStreamReader(System.in);
  115. BufferedReader b = new BufferedReader(input);
  116. String str = b.readLine();//reading the string from console
  117. return str;
  118. }
  119. public static char getChar() throws IOException
  120. {
  121. String str = getString();
  122. return str.charAt(0);//reading first char of console string
  123. }
  124. public static int getInt() throws IOException
  125. {
  126. String str = getString();
  127. return Integer.parseInt(str);//converting console string to numeric value
  128. }
Success #stdin #stdout 0.03s 25628KB
stdin
Standard input is empty
stdout
import java.io.*;  
import java.util.*; 
class Optimal
{
public int p[]; 
public int q[]; 
public int a[]; 
public int w[][];
public int c[][];
public int r[][]; 
public int n;
int front,rear,queue[]; 
public Optimal(int SIZE)
{
p=new int[SIZE]; q= new int[SIZE]; a=new int[SIZE];
w=new int[SIZE][SIZE]; c=new int[SIZE][SIZE]; r=new int[SIZE][SIZE];
queue=new int[SIZE]; front=rear=-1;
}
/* This function returns a value in the range r[i][j-1] to r[i+1][j] SO that the cost c[i][k-1] + c[k][j] is minimum */
public int Min_Value(int i, int j)
{
int m,k=0;
int minimum = 32000;
for (m=r[i][j-1] ; m<=r[i+1][j] ; m++)
{
if ((c[i][m-1]+c[m][j]) < minimum)
{
minimum = c[i][m-1] + c[m][j]; k = m;
}
}
return k;
}
/* This function builds the table from all the given probabilities It basically computes C,r,W values */ 
public void OBST()
{
int i, j, k, l, m;

for (i=0 ; i<n ; i++)

{
// Initialize 
w[i][i] = q[i];
r[i][i] = c[i][i] = 0;
// Optimal trees with one node 
w[i][i+1] = q[i] + q[i+1] + p[i+1]; r[i][i+1] = i+1;
c[i][i+1] = q[i] + q[i+1] + p[i+1];
}
w[n][n] = q[n];
r[n][n] = c[n][n] = 0;
// Find optimal trees with m nodes 
for (m=2 ; m<=n ; m++)
{
for (i=0 ; i<=n-m ; i++)
{
j = i+m;
w[i][j] = w[i][j-1] + p[j] + q[j]; k = Min_Value(i,j);
c[i][j] = w[i][j] + c[i][k-1] + c[k][j]; r[i][j] = k;
}
}
}
/*This function builds the tree from the tables made by the OBST function */ 
public void build_tree()
{
int i, j, k;
System.out.print("The Optimal Binary Search Tree For The Given Nodes Is \n");
System.out.print("\n The Root of this OBST is :: "+r[0][n]);
System.out.print("\n The Cost Of this OBST is :: "+c[0][n]);
System.out.print("\n\n\tNODE\tLEFT CHILD\tRIGHT CHILD");
System.out.println("\n "); queue[++rear] = 0;
queue[++rear] = n; while(front != rear)
{
i = queue[++front]; j = queue[++front]; k = r[i][j];
System.out.print("\n "+k); if (r[i][k-1] != 0)
{
System.out.print(" "+r[i][k-1]); queue[++rear] = i; queue[++rear] = k-1;
}
else System.out.print(" -");
if(r[k][j] != 0)
{
System.out.print(" "+r[k][j]); queue[++rear] = k; queue[++rear] = j;
}
else
System.out.print(" -");
}
System.out.println("\n");
}
}
/* This is the main function */ 
class OBSTDemo
{
public static void main (String[] args ) throws IOException,NullPointerException
{
Optimal obj=new Optimal(10); int i;
System.out.print("\n Optimal Binary Search Tree \n"); 
System.out.print("\n Enter the number of nodes");
obj.n=getInt();
System.out.print("\n Enter the data as \n");
for (i=1;i<=obj.n;i++)
{
System.out.print("\n a["+i+"]"); obj.a[i]=getInt();
}
for (i=1 ; i<=obj.n ; i++)
{
System.out.println("p["+i+"]"); obj.p[i]=getInt();
}
for (i=0 ; i<=obj.n ; i++)
{
System.out.print("q["+i+"]"); obj.q[i]=getInt();
} obj.OBST();
obj.build_tree();
}
public static String getString() throws IOException
{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(input);
String str = b.readLine();//reading the string from console
return str;
}
public static char getChar() throws IOException
{
String str = getString();
return str.charAt(0);//reading first char of console string
}
public static int getInt() throws IOException
{
String str = getString();
return Integer.parseInt(str);//converting console string to numeric value
}