Skip to main content
Code Review

Return to Question

added 173 characters in body
Source Link
DAme
  • 189
  • 1
  • 6

To complete the code, here is a class with main to test my code:

public class PreattyPrintTree {
 public static void main(String[] args) {
 BlackRedBST bst = new BlackRedBST();
 
 // this is a sample, but a method to add the nodes should be write
 bst.root = new NodeRB(1);
 
 bst.root.left = new NodeRB(2);
 bst.root.left.level = 2;
 bst.root.left.p=bst.root;
 bst.root.right = new NodeRB(3);
 bst.root.right.p = bst.root;
 bst.root.right.level = 2;
 
 bst.printTree(bst.root, "\t", true);
 }
}

An output sample, with three nodes and print of leaf enabled:

 |-+*
 |+3
 | |-+*
 +1
 | |-+*
 |+2
 |-+*

How can the printTree method be improved?

How can the printTree method be improved?

To complete the code, here is a class with main to test my code:

public class PreattyPrintTree {
 public static void main(String[] args) {
 BlackRedBST bst = new BlackRedBST();
 
 // this is a sample, but a method to add the nodes should be write
 bst.root = new NodeRB(1);
 
 bst.root.left = new NodeRB(2);
 bst.root.left.level = 2;
 bst.root.left.p=bst.root;
 bst.root.right = new NodeRB(3);
 bst.root.right.p = bst.root;
 bst.root.right.level = 2;
 
 bst.printTree(bst.root, "\t", true);
 }
}

An output sample, with three nodes and print of leaf enabled:

 |-+*
 |+3
 | |-+*
 +1
 | |-+*
 |+2
 |-+*

How can the printTree method be improved?

Tweeted twitter.com/StackCodeReview/status/1043152999449862144
edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

How could be improvedcan the printTree method be improved?

How could be improved the printTree method?

How can the printTree method be improved?

Post Reopened by ferada, Sᴀᴍ Onᴇᴌᴀ , Stephen Rauch, Graipher, mdfst13
added 765 characters in body
Source Link
DAme
  • 189
  • 1
  • 6
class BlackRedBST{
 public static final NodeRB NILL = null;
 public static final boolean RED = true;
 public static final boolean BLACK = false;
 
 NodeRB root;
 
 public BlackRedBST () {
 root = NILL;
 }
 
 public void printTree(NodeRB tree, String indent, boolean print_leaf) {
 if(tree == NILL) {
 System.out.print((print_leaf?indent+" |-+*\n":""));
 } else { 
 indent += " ";
 if(tree.p == NILL || tree.p == null) {
 printTree(tree.right, indent + " ", print_leaf);
 System.out.print(indent + "+" + tree.value + "\n");
 printTree(tree.left, indent + " ", print_leaf);
 }else if(tree.p.right==tree) {
 printTree(tree.right, indent + " ", print_leaf);
 System.out.print(indent + "|+" + tree.value + "\n");
 printTree(tree.left, indent + "|", print_leaf);
 }
 else { 
 printTree(tree.right, indent + "|", print_leaf);
 System.out.print(indent + "|+" + tree.value + "\n");
 printTree(tree.left, indent + " ", print_leaf);
 
 }
 }
 
 }
}

To be clear in the following the NodeRB class:

class NodeRB{
 
 int value;
 NodeRB p;
 NodeRB left;
 NodeRB right;
 boolean color;
 int level;
 
 public NodeRB(int v) {
 value = v;
 color = BlackRedBST.RED;
 left = BlackRedBST.NILL;
 right = BlackRedBST.NILL;
 level = 1;
 }
}

How could this be improved the printTree method?

public void printTree(NodeRB tree, String indent, boolean print_leaf) {
 if(tree == NILL) {
 System.out.print((print_leaf?indent+" |-+*\n":""));
 } else { 
 indent += " ";
 if(tree.p == NILL || tree.p == null) {
 printTree(tree.right, indent + " ", print_leaf);
 System.out.print(indent + "+" + tree.value + "\n");
 printTree(tree.left, indent + " ", print_leaf);
 }else if(tree.p.right==tree) {
 printTree(tree.right, indent + " ", print_leaf);
 System.out.print(indent + "|+" + tree.value + "\n");
 printTree(tree.left, indent + "|", print_leaf);
 }
 else { 
 printTree(tree.right, indent + "|", print_leaf);
 System.out.print(indent + "|+" + tree.value + "\n");
 printTree(tree.left, indent + " ", print_leaf);
 
 }
 }
 
}

How could this be improved?

class BlackRedBST{
 public static final NodeRB NILL = null;
 public static final boolean RED = true;
 public static final boolean BLACK = false;
 
 NodeRB root;
 
 public BlackRedBST () {
 root = NILL;
 }
 
 public void printTree(NodeRB tree, String indent, boolean print_leaf) {
 if(tree == NILL) {
 System.out.print((print_leaf?indent+" |-+*\n":""));
 } else { 
 indent += " ";
 if(tree.p == NILL || tree.p == null) {
 printTree(tree.right, indent + " ", print_leaf);
 System.out.print(indent + "+" + tree.value + "\n");
 printTree(tree.left, indent + " ", print_leaf);
 }else if(tree.p.right==tree) {
 printTree(tree.right, indent + " ", print_leaf);
 System.out.print(indent + "|+" + tree.value + "\n");
 printTree(tree.left, indent + "|", print_leaf);
 }
 else { 
 printTree(tree.right, indent + "|", print_leaf);
 System.out.print(indent + "|+" + tree.value + "\n");
 printTree(tree.left, indent + " ", print_leaf);
 
 }
 }
 
 }
}

To be clear in the following the NodeRB class:

class NodeRB{
 
 int value;
 NodeRB p;
 NodeRB left;
 NodeRB right;
 boolean color;
 int level;
 
 public NodeRB(int v) {
 value = v;
 color = BlackRedBST.RED;
 left = BlackRedBST.NILL;
 right = BlackRedBST.NILL;
 level = 1;
 }
}

How could be improved the printTree method?

Post Closed as "Not suitable for this site" by Toby Speight, yuri, Graipher, 200_success, Mast
deleted 12 characters in body; edited title
Source Link
ferada
  • 11.4k
  • 25
  • 65
Loading
Source Link
DAme
  • 189
  • 1
  • 6
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /