Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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
```             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

```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;
}
}```

```
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;
TreeNode parent = null;
for (int index = 0; index < values.length; index++)
{
if (root == null)
{
root = new TreeNode(values[index]);
}
else
{
TreeNode node = new TreeNode(values[index]);
if (index % 2 == 1) {
parent = (TreeNode) queue.remove();
parent.setLeft(node);
}

else
{
parent.setRight(node);
}
}
}
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)
TreeNode rightChild = node.getRight();
if (rightChild != null)
}
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. =)

2. ## Re: print space

could u explain what it should look like?

3. ## Re: print space

I have already edited my question above, hope you have better understanding about my problem. Thanks -)

4. ## Re: print space

Here is something to look at,
```               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

5. ## 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

6. ## Re: print space

the c is the current level, so it will work for any size tree that follow the format you gave

7. ## 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?

8. ## 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

```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 ++){