level order traversal line by line

Given a Tree and we need to print the level order traversal of the tree in slight different manner. Here level order traversal need to be printed line by line

The output of program for above example is

A
B C
D E F G
H I

The Code is -
import java.util.*;
class Node
{
    int data;
    Node left, right;
 
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
    public Node(){
        data =0;
        left  = right = null;
    }
}

// Binary tree Class
class BTree
{
 
    static Node root;

    void levelOrder(Node node)
	   {
    	Queue a = new LinkedList();
    	if(node == null)
    	return;
    	if(node != null)
    	a.add(node);
    	a.add(null); //Null is added to denote that this level ends here.
    	while(!a.isEmpty()){
        	Node temp = a.remove();
        	// If temp is null, that means, one level is over , print a new line.
        	if(temp == null ){
            	System.out.println();
            	//Add Null only if the size of queue is not zero, otherwise there would be infinite //null added.
            	if(a.size()!=0)
            	a.add(null);
            	continue;
        	}
        	if(temp.left != null)
        	a.add(temp.left);
        	if(temp.right != null)
        	a.add(temp.right);
      	 
       	 
        	System.out.print(temp.data+" ");
    	}
	}

 public static void main(String[] args)
    {
        BTree tree = new BTree();
        tree.root = new Node(1);
        tree.root.left = new Node(5);
        tree.root.right = new Node(4);
        tree.root.left.left = new Node(9);
        tree.root.left.right = new Node(7);
        tree.root.right.left = new Node(10);
        tree.root.right.right = new Node(13);
        System.out.println("Level Order traversal of Binary Tree is ");
        tree.levelOrder(root);
    }
}
    
    

There is a second way to print line by line traversal, in which we do not need to add null. It’s more efficient and there are less chance of error.

The Code is -
import java.util.*;
class Node
{
    int data;
    Node left, right;
 
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
    public Node(){
        data =0;
        left  = right = null;
    }
}

// Binary tree Class
class BTree
{
 
    static Node root;
    void levelOrder(Node node)
	{
    	if(root == null)
            return;         
        // Create an empty queue for level order traversal
        Queue queue =new LinkedList();         
        queue.add(root);      
         
        while(true)
        {
             
            int count = queue.size();
            if(count == 0)
                break;
             
             while(count > 0)
            {
                Node temp = queue.poll();
                System.out.print(temp.data + " ");
               if(temp.left != null)
                    queue.add(temp.left);
                if(temp.right != null)
                    queue.add(temp.right);
                count--;
            }
            System.out.println();
        }
    }
   
    
    


 public static void main(String[] args)
    {
        BTree tree = new BTree();
        tree.root = new Node(1);
        tree.root.left = new Node(5);
        tree.root.right = new Node(4);
        tree.root.left.left = new Node(9);
        tree.root.left.right = new Node(7);
        tree.root.right.left = new Node(10);
        tree.root.right.right = new Node(13);
        System.out.println("Level Order traversal of Binary Tree is ");
        tree.levelOrder(root);
    }
}