I am having trouble with writing this section of my code into a tree format. I want it to output as
x
x x
x x
but it is outputting as
x
x
x
....
How would I be able to add indentations and spaces in my code? And in the case of an empty node, enter an asterisk or any symbol?
public void insert(int value)
{
Node n = new Node(value);
if(root == null)
root = n;
else
{
Node parent = root;
while(parent != null)
{
if(value < parent.data)
{
if(parent.left == null)
{
parent.left = n;
return;
}
else
{
parent = parent.left;
}
}
else
{
if(parent.right == null)
{
parent.right = n;
return;
}
else
{
parent = parent.right;
}
}
}
}
}
private void inOrder(Node n)
{
if(n == null)
return;
inOrder(n.left);
System.out.println(n.data + " ");
inOrder(n.right);
}
public void printInorder()
{
inOrder(root);
}