0

It has a node class containing this code

class Node {

   String value;
   Node left;
   Node right;

   Node(String value) {

     this.value = value;
     this.left = null;
     this.right = null;
   }
}

The tree structure is already been made according to the node and having

Node node = new Node();

here node represents the root of the tree.
I need to draw or print a tree without Gui, Jframe. Just to show in the output panel. Example like this format of the tree
this tree like structure should be drawn according to node's left and right branches.
on call sketch(node); the tree will print
Can u guys help me to create the sketch class which will print the tree.

public static void sketch(Node root) {

}

Really appreciate your help.

  • yes tried couple ways but tree is not properly showing and space calculation problems – Piash Hassan May 19 '16 at 13:12
  • ok, so post what you have already... but you knwo that here is the answer: http://stackoverflow.com/questions/4965335/how-to-print-binary-tree-diagram – NeoP5 May 19 '16 at 13:14
  • trying the link you have given and letting u know thanks – Piash Hassan May 19 '16 at 13:17
  • thanks it works . thanks again for your time – Piash Hassan May 19 '16 at 13:24
  • Hey, if you copy your homework at least try to understand what is going on in there and how it works or you will fail later on!!! – NeoP5 May 19 '16 at 13:26
  • yes i will first gonna see what's going over there cause i will also need to face question of those parts – Piash Hassan May 19 '16 at 13:27
  • even then... forget the solution and do it on your own... learning by doing is the best way to learn.. especially learning programming.. practice is the only way to do! after a few hundred thousands lines of code you will know :D – NeoP5 May 19 '16 at 13:32

3 Answers3

0

To post a helpful answer ;) Your linked picture is unable to be display so i give it a try without knowing excatly what you want to archive!

Use recursive function that is called for every "sub-node". If you have to center your tree you can let your function return the max level of the node and append blanks in front of all lines above.

Hope this gives you some idea... NOT TESTED!

public static void sketch(Node node){
   printNode(node,0);
}

public static int printNode(Node node, int level){
  String line = "";
  String lineLeft = "";
  String lineRight = "";
  for (int i = 0; i++; i<level){
   line = line + "  ";
   lineLeft = lineLeft + "  ";
   lineRight = lineRight + "  ";
  }
  line = line + node.getValue();

  System.out.println(line);
  System.out.println(lineLeft);
  if(node.getLeft() != null){
   printNode(node.getLeft(), level+1); 
  }
 .... proceed here
}
NeoP5
  • 611
  • 7
  • 19
  • i have re added the picture – Piash Hassan May 19 '16 at 13:11
  • Please. Never concatenate `String` objects inside a loop. Use a `StringBuilder` or anything other which is not `immutable`. – SubOptimal May 19 '16 at 13:20
  • I know... really bad practice. Did this example from my mind directly and for a homework its nhmm.. ok. (even it would be better to teach best practice directly). => @SubOptimal you are fully right – NeoP5 May 19 '16 at 13:25
0

https://github.com/AharonSambol/PrettyPrintTreeJava

I know I'm late.. But I made this solution which works not only for simple trees but also for more complex ones (such as multi-lined strings)

Example output:

enter image description here

0

To print the tree you should do something like the BFS graph traversal. Pseudo code for this approach:


print(root):
  h := height(root)
  q := queue()
  q.add(root)

  for level := 0 to h:
     indent := 2^(h-level)-1
     space := 2^(h-level+1)-1
     
     levelNodes = q.clear()
     if (level > 0):                // print lines only for non-root levels
        printLines(levelNodes)      // ┌─┴─┐

     printLevelNodes(levelNodes)    // a   b

     for node in levelNodes:
        q.add(node.left)
        q.add(node.right)
  

In practice you would get something like:

TreeNode root = Tree.ints(TreeNode.class, "[3,9,20,null,null,15,7]");
Tree.print(root);
//           3
//       ┌───┴───┐
//       9      20
//             ┌─┴─┐
//            15   7

The complete implementation can be found in the class: https://github.com/stuparmihailo/algo2prepare/blob/main/src/main/java/com/algo2prepare/util/Tree.java

c3R1cGFy
  • 505
  • 4
  • 12