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:
enter image description here










share|improve this question
























  • 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















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:
enter image description here










share|improve this question
























  • 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













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:
enter image description here










share|improve this question















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:
enter image description here







algorithm binary-search-tree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen