How to print the tree from an optimal binary search tree root table
up vote
-1
down vote
favorite
I already have a root table
Now I have to print out this tree according to the root table.
But it seems the way I printed it is wrong.
My logic is:
Each node saves its own level and according to each node has how many nodes before it.
If a node has 3 nodes before it then print("ttt");
import java.util.*;
public class Main {
static double minAvg;
public static void main(String args) {
Scanner sc = new Scanner(System.in);
System.out.println("type in frequency from the keyboard, use , to split.。");
String input = sc.nextLine();
String inputArray = input.split(",");
double inputFrequency= new double[inputArray.length];
for(int i = 0;i < inputFrequency.length;i++) {
inputFrequency[i] = Float.parseFloat(inputArray[i]);
}
int n = inputFrequency.length ;
//Root table
int R = new int[n+1][n+1];
double A = optst(n,inputFrequency,R);
showAtable(A);
System.out.println();
System.out.println();
showRtable(R);
System.out.println();
System.out.println();
new BinarySearchTree().showRTableTree(R);
}
//algorithm, find min cost
static double optst(int n, double p,int R){
int i,j,diagonal;
double A = new double[n+1][n+1];
for (i=1; i<=n; i++) {
A[i-1][i-1] = 0;
A[i-1][i] = p[i-1];
R[i-1][i] = i;
R[i-1][i-1] = 0;
}
for(diagonal=1; diagonal<=n-1; diagonal++) {
for(i=1; i<=n-diagonal; i++) {
j = i + diagonal;
double minimum = Double.MAX_VALUE;
double sum = 0;
int minr = 0;
for(int k=i; k<=j; k++) {
if (A[i-1][k-1]+A[k][j] < minimum) {
minimum = A[i-1][k-1]+A[k][j];
minr = k;
}
sum += p[k-1];
}
A[i-1][j] = minimum + sum;
R[i-1][j] = minr;
}
}
return A;
}
static void showAtable(double a) {
for(int i=0;i<a.length;i++)
{
for(int z=0;z<a.length;z++) {
System.out.printf("%.3f"+"t",a[i][z]);
}
System.out.println();
}
}
static void showRtable(int a) {
for(int i=0; i<a.length; i++) {
for(int j=0; j<a[0].length;j++) {
System.out.print(String.valueOf(a[i][j]) + "tt");
}
System.out.println();
}
}
}
class BinarySearchTree {
public void showRTableTree(int array) {
Node node = new Node();
build(array, node, 1, array.length-1, 0, -1);
node.showTree(node);
}
Node build(int array, Node node, int leftpivot, int rightpivot, int level, int i) {
if (leftpivot == rightpivot) {//Check if it is a leaf
Node sub = new Node();
sub.node = array[leftpivot][rightpivot];
sub.level = level;
i++;
sub.index = i;
return sub;
}
int subRoot = array[leftpivot][rightpivot];//sub Root
node.node = array[leftpivot][rightpivot];
node.level = level;
level ++;
int new_leftpivot = subRoot-1;
int new_rightpivot = subRoot+1;
//find left sub tree
Node newTree = new Node();
if (new_leftpivot>=leftpivot) {
//將傳的左子樹放入次樹的左子樹
node.leftNode = build(array, newTree, leftpivot, new_leftpivot, level, i);
i = node.leftNode.index;
}
i++;//has several nodes before it.
node.index = i;
//find right sub tree
if (new_rightpivot<=rightpivot) {
node.rightNode = build(array, newTree, new_rightpivot, rightpivot, level, i);
i = node.rightNode.index;
}
return node;
}
class Node {
Node leftNode ;
Node rightNode;
int node = 0;
int level = 0;
int index = 0;
int nowlevel = 0;
void showTree(Node node) {
if (nowlevel != node.level) {
System.out.println();
nowlevel = node.level;
}
//according to this node has how many nodes before it
for (int i = 0; i< node.index; i++) {
System.out.print("t");
}
System.out.print(node.node);
if (node.leftNode != null) {
showTree(node.leftNode);
}
if (node.rightNode != null) {
showTree(node.rightNode);
}
}
}
}
I don't know how to fix it.
or anyone has other approaches can print the tree according to this root table?
my result:
algorithm binary-search-tree
add a comment |
up vote
-1
down vote
favorite
I already have a root table
Now I have to print out this tree according to the root table.
But it seems the way I printed it is wrong.
My logic is:
Each node saves its own level and according to each node has how many nodes before it.
If a node has 3 nodes before it then print("ttt");
import java.util.*;
public class Main {
static double minAvg;
public static void main(String args) {
Scanner sc = new Scanner(System.in);
System.out.println("type in frequency from the keyboard, use , to split.。");
String input = sc.nextLine();
String inputArray = input.split(",");
double inputFrequency= new double[inputArray.length];
for(int i = 0;i < inputFrequency.length;i++) {
inputFrequency[i] = Float.parseFloat(inputArray[i]);
}
int n = inputFrequency.length ;
//Root table
int R = new int[n+1][n+1];
double A = optst(n,inputFrequency,R);
showAtable(A);
System.out.println();
System.out.println();
showRtable(R);
System.out.println();
System.out.println();
new BinarySearchTree().showRTableTree(R);
}
//algorithm, find min cost
static double optst(int n, double p,int R){
int i,j,diagonal;
double A = new double[n+1][n+1];
for (i=1; i<=n; i++) {
A[i-1][i-1] = 0;
A[i-1][i] = p[i-1];
R[i-1][i] = i;
R[i-1][i-1] = 0;
}
for(diagonal=1; diagonal<=n-1; diagonal++) {
for(i=1; i<=n-diagonal; i++) {
j = i + diagonal;
double minimum = Double.MAX_VALUE;
double sum = 0;
int minr = 0;
for(int k=i; k<=j; k++) {
if (A[i-1][k-1]+A[k][j] < minimum) {
minimum = A[i-1][k-1]+A[k][j];
minr = k;
}
sum += p[k-1];
}
A[i-1][j] = minimum + sum;
R[i-1][j] = minr;
}
}
return A;
}
static void showAtable(double a) {
for(int i=0;i<a.length;i++)
{
for(int z=0;z<a.length;z++) {
System.out.printf("%.3f"+"t",a[i][z]);
}
System.out.println();
}
}
static void showRtable(int a) {
for(int i=0; i<a.length; i++) {
for(int j=0; j<a[0].length;j++) {
System.out.print(String.valueOf(a[i][j]) + "tt");
}
System.out.println();
}
}
}
class BinarySearchTree {
public void showRTableTree(int array) {
Node node = new Node();
build(array, node, 1, array.length-1, 0, -1);
node.showTree(node);
}
Node build(int array, Node node, int leftpivot, int rightpivot, int level, int i) {
if (leftpivot == rightpivot) {//Check if it is a leaf
Node sub = new Node();
sub.node = array[leftpivot][rightpivot];
sub.level = level;
i++;
sub.index = i;
return sub;
}
int subRoot = array[leftpivot][rightpivot];//sub Root
node.node = array[leftpivot][rightpivot];
node.level = level;
level ++;
int new_leftpivot = subRoot-1;
int new_rightpivot = subRoot+1;
//find left sub tree
Node newTree = new Node();
if (new_leftpivot>=leftpivot) {
//將傳的左子樹放入次樹的左子樹
node.leftNode = build(array, newTree, leftpivot, new_leftpivot, level, i);
i = node.leftNode.index;
}
i++;//has several nodes before it.
node.index = i;
//find right sub tree
if (new_rightpivot<=rightpivot) {
node.rightNode = build(array, newTree, new_rightpivot, rightpivot, level, i);
i = node.rightNode.index;
}
return node;
}
class Node {
Node leftNode ;
Node rightNode;
int node = 0;
int level = 0;
int index = 0;
int nowlevel = 0;
void showTree(Node node) {
if (nowlevel != node.level) {
System.out.println();
nowlevel = node.level;
}
//according to this node has how many nodes before it
for (int i = 0; i< node.index; i++) {
System.out.print("t");
}
System.out.print(node.node);
if (node.leftNode != null) {
showTree(node.leftNode);
}
if (node.rightNode != null) {
showTree(node.rightNode);
}
}
}
}
I don't know how to fix it.
or anyone has other approaches can print the tree according to this root table?
my result:
algorithm binary-search-tree
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Nov 19 at 20:54
Sorry,I don’t understand. What regulation do I violate? Please let me know
– Shawn Plus
Nov 20 at 1:41
Please post here you code (make it as minimal as you can) as an addition to your github link
– David Winder
Nov 20 at 16:50
(1) Post text in the question, not images or off-site links. (2) Follow the requirements for a Minimal, complete, verifiable example. (3) Reduce the search space with some basic debugging.
– Prune
Nov 20 at 16:54
But that image is my execution result, it can help to figure out what problem is.
– Shawn Plus
Nov 20 at 20:29
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I already have a root table
Now I have to print out this tree according to the root table.
But it seems the way I printed it is wrong.
My logic is:
Each node saves its own level and according to each node has how many nodes before it.
If a node has 3 nodes before it then print("ttt");
import java.util.*;
public class Main {
static double minAvg;
public static void main(String args) {
Scanner sc = new Scanner(System.in);
System.out.println("type in frequency from the keyboard, use , to split.。");
String input = sc.nextLine();
String inputArray = input.split(",");
double inputFrequency= new double[inputArray.length];
for(int i = 0;i < inputFrequency.length;i++) {
inputFrequency[i] = Float.parseFloat(inputArray[i]);
}
int n = inputFrequency.length ;
//Root table
int R = new int[n+1][n+1];
double A = optst(n,inputFrequency,R);
showAtable(A);
System.out.println();
System.out.println();
showRtable(R);
System.out.println();
System.out.println();
new BinarySearchTree().showRTableTree(R);
}
//algorithm, find min cost
static double optst(int n, double p,int R){
int i,j,diagonal;
double A = new double[n+1][n+1];
for (i=1; i<=n; i++) {
A[i-1][i-1] = 0;
A[i-1][i] = p[i-1];
R[i-1][i] = i;
R[i-1][i-1] = 0;
}
for(diagonal=1; diagonal<=n-1; diagonal++) {
for(i=1; i<=n-diagonal; i++) {
j = i + diagonal;
double minimum = Double.MAX_VALUE;
double sum = 0;
int minr = 0;
for(int k=i; k<=j; k++) {
if (A[i-1][k-1]+A[k][j] < minimum) {
minimum = A[i-1][k-1]+A[k][j];
minr = k;
}
sum += p[k-1];
}
A[i-1][j] = minimum + sum;
R[i-1][j] = minr;
}
}
return A;
}
static void showAtable(double a) {
for(int i=0;i<a.length;i++)
{
for(int z=0;z<a.length;z++) {
System.out.printf("%.3f"+"t",a[i][z]);
}
System.out.println();
}
}
static void showRtable(int a) {
for(int i=0; i<a.length; i++) {
for(int j=0; j<a[0].length;j++) {
System.out.print(String.valueOf(a[i][j]) + "tt");
}
System.out.println();
}
}
}
class BinarySearchTree {
public void showRTableTree(int array) {
Node node = new Node();
build(array, node, 1, array.length-1, 0, -1);
node.showTree(node);
}
Node build(int array, Node node, int leftpivot, int rightpivot, int level, int i) {
if (leftpivot == rightpivot) {//Check if it is a leaf
Node sub = new Node();
sub.node = array[leftpivot][rightpivot];
sub.level = level;
i++;
sub.index = i;
return sub;
}
int subRoot = array[leftpivot][rightpivot];//sub Root
node.node = array[leftpivot][rightpivot];
node.level = level;
level ++;
int new_leftpivot = subRoot-1;
int new_rightpivot = subRoot+1;
//find left sub tree
Node newTree = new Node();
if (new_leftpivot>=leftpivot) {
//將傳的左子樹放入次樹的左子樹
node.leftNode = build(array, newTree, leftpivot, new_leftpivot, level, i);
i = node.leftNode.index;
}
i++;//has several nodes before it.
node.index = i;
//find right sub tree
if (new_rightpivot<=rightpivot) {
node.rightNode = build(array, newTree, new_rightpivot, rightpivot, level, i);
i = node.rightNode.index;
}
return node;
}
class Node {
Node leftNode ;
Node rightNode;
int node = 0;
int level = 0;
int index = 0;
int nowlevel = 0;
void showTree(Node node) {
if (nowlevel != node.level) {
System.out.println();
nowlevel = node.level;
}
//according to this node has how many nodes before it
for (int i = 0; i< node.index; i++) {
System.out.print("t");
}
System.out.print(node.node);
if (node.leftNode != null) {
showTree(node.leftNode);
}
if (node.rightNode != null) {
showTree(node.rightNode);
}
}
}
}
I don't know how to fix it.
or anyone has other approaches can print the tree according to this root table?
my result:
algorithm binary-search-tree
I already have a root table
Now I have to print out this tree according to the root table.
But it seems the way I printed it is wrong.
My logic is:
Each node saves its own level and according to each node has how many nodes before it.
If a node has 3 nodes before it then print("ttt");
import java.util.*;
public class Main {
static double minAvg;
public static void main(String args) {
Scanner sc = new Scanner(System.in);
System.out.println("type in frequency from the keyboard, use , to split.。");
String input = sc.nextLine();
String inputArray = input.split(",");
double inputFrequency= new double[inputArray.length];
for(int i = 0;i < inputFrequency.length;i++) {
inputFrequency[i] = Float.parseFloat(inputArray[i]);
}
int n = inputFrequency.length ;
//Root table
int R = new int[n+1][n+1];
double A = optst(n,inputFrequency,R);
showAtable(A);
System.out.println();
System.out.println();
showRtable(R);
System.out.println();
System.out.println();
new BinarySearchTree().showRTableTree(R);
}
//algorithm, find min cost
static double optst(int n, double p,int R){
int i,j,diagonal;
double A = new double[n+1][n+1];
for (i=1; i<=n; i++) {
A[i-1][i-1] = 0;
A[i-1][i] = p[i-1];
R[i-1][i] = i;
R[i-1][i-1] = 0;
}
for(diagonal=1; diagonal<=n-1; diagonal++) {
for(i=1; i<=n-diagonal; i++) {
j = i + diagonal;
double minimum = Double.MAX_VALUE;
double sum = 0;
int minr = 0;
for(int k=i; k<=j; k++) {
if (A[i-1][k-1]+A[k][j] < minimum) {
minimum = A[i-1][k-1]+A[k][j];
minr = k;
}
sum += p[k-1];
}
A[i-1][j] = minimum + sum;
R[i-1][j] = minr;
}
}
return A;
}
static void showAtable(double a) {
for(int i=0;i<a.length;i++)
{
for(int z=0;z<a.length;z++) {
System.out.printf("%.3f"+"t",a[i][z]);
}
System.out.println();
}
}
static void showRtable(int a) {
for(int i=0; i<a.length; i++) {
for(int j=0; j<a[0].length;j++) {
System.out.print(String.valueOf(a[i][j]) + "tt");
}
System.out.println();
}
}
}
class BinarySearchTree {
public void showRTableTree(int array) {
Node node = new Node();
build(array, node, 1, array.length-1, 0, -1);
node.showTree(node);
}
Node build(int array, Node node, int leftpivot, int rightpivot, int level, int i) {
if (leftpivot == rightpivot) {//Check if it is a leaf
Node sub = new Node();
sub.node = array[leftpivot][rightpivot];
sub.level = level;
i++;
sub.index = i;
return sub;
}
int subRoot = array[leftpivot][rightpivot];//sub Root
node.node = array[leftpivot][rightpivot];
node.level = level;
level ++;
int new_leftpivot = subRoot-1;
int new_rightpivot = subRoot+1;
//find left sub tree
Node newTree = new Node();
if (new_leftpivot>=leftpivot) {
//將傳的左子樹放入次樹的左子樹
node.leftNode = build(array, newTree, leftpivot, new_leftpivot, level, i);
i = node.leftNode.index;
}
i++;//has several nodes before it.
node.index = i;
//find right sub tree
if (new_rightpivot<=rightpivot) {
node.rightNode = build(array, newTree, new_rightpivot, rightpivot, level, i);
i = node.rightNode.index;
}
return node;
}
class Node {
Node leftNode ;
Node rightNode;
int node = 0;
int level = 0;
int index = 0;
int nowlevel = 0;
void showTree(Node node) {
if (nowlevel != node.level) {
System.out.println();
nowlevel = node.level;
}
//according to this node has how many nodes before it
for (int i = 0; i< node.index; i++) {
System.out.print("t");
}
System.out.print(node.node);
if (node.leftNode != null) {
showTree(node.leftNode);
}
if (node.rightNode != null) {
showTree(node.rightNode);
}
}
}
}
I don't know how to fix it.
or anyone has other approaches can print the tree according to this root table?
my result:
algorithm binary-search-tree
algorithm binary-search-tree
edited Nov 20 at 20:24
asked Nov 19 at 20:11
Shawn Plus
359
359
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Nov 19 at 20:54
Sorry,I don’t understand. What regulation do I violate? Please let me know
– Shawn Plus
Nov 20 at 1:41
Please post here you code (make it as minimal as you can) as an addition to your github link
– David Winder
Nov 20 at 16:50
(1) Post text in the question, not images or off-site links. (2) Follow the requirements for a Minimal, complete, verifiable example. (3) Reduce the search space with some basic debugging.
– Prune
Nov 20 at 16:54
But that image is my execution result, it can help to figure out what problem is.
– Shawn Plus
Nov 20 at 20:29
add a comment |
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Nov 19 at 20:54
Sorry,I don’t understand. What regulation do I violate? Please let me know
– Shawn Plus
Nov 20 at 1:41
Please post here you code (make it as minimal as you can) as an addition to your github link
– David Winder
Nov 20 at 16:50
(1) Post text in the question, not images or off-site links. (2) Follow the requirements for a Minimal, complete, verifiable example. (3) Reduce the search space with some basic debugging.
– Prune
Nov 20 at 16:54
But that image is my execution result, it can help to figure out what problem is.
– Shawn Plus
Nov 20 at 20:29
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Nov 19 at 20:54
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Nov 19 at 20:54
Sorry,I don’t understand. What regulation do I violate? Please let me know
– Shawn Plus
Nov 20 at 1:41
Sorry,I don’t understand. What regulation do I violate? Please let me know
– Shawn Plus
Nov 20 at 1:41
Please post here you code (make it as minimal as you can) as an addition to your github link
– David Winder
Nov 20 at 16:50
Please post here you code (make it as minimal as you can) as an addition to your github link
– David Winder
Nov 20 at 16:50
(1) Post text in the question, not images or off-site links. (2) Follow the requirements for a Minimal, complete, verifiable example. (3) Reduce the search space with some basic debugging.
– Prune
Nov 20 at 16:54
(1) Post text in the question, not images or off-site links. (2) Follow the requirements for a Minimal, complete, verifiable example. (3) Reduce the search space with some basic debugging.
– Prune
Nov 20 at 16:54
But that image is my execution result, it can help to figure out what problem is.
– Shawn Plus
Nov 20 at 20:29
But that image is my execution result, it can help to figure out what problem is.
– Shawn Plus
Nov 20 at 20:29
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381946%2fhow-to-print-the-tree-from-an-optimal-binary-search-tree-root-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here.
– Prune
Nov 19 at 20:54
Sorry,I don’t understand. What regulation do I violate? Please let me know
– Shawn Plus
Nov 20 at 1:41
Please post here you code (make it as minimal as you can) as an addition to your github link
– David Winder
Nov 20 at 16:50
(1) Post text in the question, not images or off-site links. (2) Follow the requirements for a Minimal, complete, verifiable example. (3) Reduce the search space with some basic debugging.
– Prune
Nov 20 at 16:54
But that image is my execution result, it can help to figure out what problem is.
– Shawn Plus
Nov 20 at 20:29