I have written a program to print a binary tree level by level on different lines, like the tree would actually look if drawn on paper.
I have done a BFS traversal and then I proceed to print the tree assuming it is complete binary tree:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BinaryTreeLevelWise {
/**
* This class represents the individual nodes of the binary tree
* Each node has a left, right pointer of type Node
* and Value to hold the value
* @author Aneesh
*
*/
class Node {
Node left;
Node right;
int value;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node value=" + value + "";
}
}
/**
* Driver function to test the code
* @param args
*/
public static void main(String[] args) {
new BinaryTreeLevelWise().run();
}
/**
* This function inserts an element into the binary tree
* @param node
* @param value
*/
public void insert(Node node, int value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Inserted " + value + " to left of "
+ node.value);
node.left = new Node(value);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of "
+ node.value);
node.right = new Node(value);
}
}
}
/**
* Builds the tree and executes some functions
*/
public void run() {
// build the simple tree from chapter 11.
Node root = new Node(5);
System.out.println("Binary Tree Example");
System.out.println("Building tree with root value " + root.value);
insert(root, 1);
insert(root, 8);
insert(root,-2);
insert(root, 6);
insert(root, 3);
insert(root, 9);
insert(root,-3);
insert(root,-1);
/*System.out.println("Traversing tree in order");
printInOrder(root);
System.out.println("Traversing tree front-to-back from location 7");
printFrontToBack(root, 7);*/
System.out.println("*************\nPrinting the tree levelWise");
printLevelWise(root);
}
/**
* This functions uses a list of nodes and prints them level wise assuming a complete
* binary tree.
* Every level will have have 2^L elements where L is the level, root is level L=0
* @param Root of the tree of type {@link Node}
*/
public void printLevelWise(Node root) {
// TODO Auto-generated method stub
Queue<Node> nodes= new LinkedList<>();
List<Node> listOfNodes = new ArrayList<Node>();
//add root to the list
traverseLevels(root, listOfNodes,nodes);
//printing in a straight line
//System.out.println("nodes are "+listOfNodes);
// printing level wise
int count = 0,level=0;
while (count < listOfNodes.size()){
int printLen= (int) Math.pow(2, level++);
for (int i=count; i < printLen -1 && i < listOfNodes.size();++i){
System.out.print(listOfNodes.get(i).value+" ");
}
System.out.println();
count = printLen-1;
}
}
/**
* This function traverses the tree and puts all the nodes level wise into a list
* @param root
* @param listOfNodes
* @param nodes
*/
private void traverseLevels(Node root, List<Node> listOfNodes, Queue<Node> nodes) {
// TODO Auto-generated method stub
if (root!=null){
nodes.add(root);
listOfNodes.add(root);
while(!nodes.isEmpty()){
//standard BFS
root= nodes.poll();
if (root.left!=null) {
listOfNodes.add(root.left);
nodes.add(root.left);
}
if (root.right!=null) {
listOfNodes.add(root.right);
nodes.add(root.right);
}
}
}
}
}
Output:
5 1 8 -2 3 6 9 -3 -1
Is this the correct method? Are there any more efficient methods?