# print space

• March 30th, 2010, 12:04 PM
brainTuner
print space
Hi, would you mind helping me in solving this problem?
Because I stuck now. The problem is to print a space in a binary tree (not binary search tree)
my current program can only output without appropriate spacing
so if input is : 4 7 8 9 2 0 3 1 5
the output will become
4
7 8
9 2 0 3
1 5

What the expected output is
Code :

``` 4 7 8 9 2 0 3 1 5```

actually the "/" space is not really compulsory, the problem is to print spaces

Here is my code contains 2 classes: TreeNode class and BinaryTree class

Code :

```import java.io.*; public class TreeNode { public Object da; public TreeNode left; public TreeNode right; public TreeNode(Object newItem) { da = newItem; left = null; right = null; } public TreeNode(Object newItem, TreeNode leftNode, TreeNode rightNode) { da = newItem; left = leftNode; right = rightNode; }   public void setItem(Object newItem) { da = newItem; } public Object getItem() { return da; } public void setLeft(TreeNode leftNode) { left = leftNode; } public TreeNode getLeft() { return left; } public void setRight(TreeNode rightNode) { right = rightNode; } public TreeNode getRight() { return right; } public int left (int i) { return 2*i; } public int right (int i) { return 2*i + 1; } public int parent (int i) { return (i-1)/2; } }```

Code :

```  import java.util.*; class BinaryTree {   public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Enter the values to insert into the binary tree: "); String[] values = (input.nextLine()).split("\\s");   TreeNode root = null; Queue queue = new LinkedList(); TreeNode parent = null; for (int index = 0; index < values.length; index++) { if (root == null) { root = new TreeNode(values[index]); queue.add(root); } else { TreeNode node = new TreeNode(values[index]); if (index % 2 == 1) { parent = (TreeNode) queue.remove(); parent.setLeft(node); }   else { parent.setRight(node); } queue.add(node); } } queue = new LinkedList(); queue.add(root); int maxNodes = 1; int countNodes = 0; while (queue.size() > 0 ) { TreeNode node = (TreeNode) queue.remove();   System.out.print(node.getItem().toString() + " "); countNodes++; if (countNodes == maxNodes) { System.out.println(); maxNodes = maxNodes * 2; countNodes = 0; } TreeNode leftChild = node.getLeft(); if (leftChild != null) queue.add(leftChild); TreeNode rightChild = node.getRight(); if (rightChild != null) queue.add(rightChild); } System.out.println(); } }```

It can’t print like the real binary tree and I do not know how to make it accordingly.
Any suggestions?
Thanks a lot. =)
• March 30th, 2010, 09:46 PM
wolfgar
Re: print space
could u explain what it should look like?
• March 31st, 2010, 06:23 AM
brainTuner
Re: print space
I have already edited my question above, hope you have better understanding about my problem. Thanks -)
• March 31st, 2010, 08:50 AM
Freaky Chris
Re: print space
Here is something to look at,
Code :

``` 1 = (2^(n-c))-1 2 2 = (2^(n-c))-1, (2^(n-(c-1)))-1, 3 3 3 3 = (2^(n-c))-1, (2^(n-(c-1)))-1,... 4 4 4 4 4 4 4 4 = (2^(n-c))-1, (2^(n-(c-1)))-1,... 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 = (2^(n-c))-1, (2^(n-(c-1)))-1,...```
What this is showing you is that the first space on each line is determined by the equation
(2^(n-c))-1

Where n = number of layers, demo has 5 layers.
c = current layer, i.e 1-5 from the demo.

and any spaces following that, so the space after each character is of length
(2^(n-(c-1)))-1

Hope all that makes sense.

The demo doesn't line up right, because of the text used on this forum lol!

Regards,
Chris
• March 31st, 2010, 10:38 AM
brainTuner
Re: print space
hmm, the algorithm you gave was quite helping
But how about if the 'c' are random integers (i.e. they are not only from 1-5)?

Thank you
• March 31st, 2010, 11:38 AM
Freaky Chris
Re: print space
the c is the current level, so it will work for any size tree that follow the format you gave
• March 31st, 2010, 11:53 AM
brainTuner
Re: print space
any idea of printing by using Queue?
I am using levl ordr trversal to print..
since this seems to print by using Array, doesn't it?
• April 1st, 2010, 06:09 PM
wolfgar
Re: print space
start from the bottom and work your way up

i dont remember what i made but once i needed to do that
and did so useing a reverse loop ( one the de-increments )
and for each layer add X number of spaces for each layer up it is

Code :

```String[] layers = the layers 0 being the top ; String[] newLayers = set to the number of layers in the layers array; for ( int i = layers.size(); i >= 0 ; i-- ){ for ( int j = layers.size() - i; j > 0; j ++){ newLayers[i] += padding; } newLayers[i] += layers[i]; }```

that is just for the edges , but the internal padding could be done similar