@@ -0,0 +1,2240 @@
+/**%0A * %0A */%0Apackage datastructures.util;%0A%0Aimport java.util.LinkedList;%0Aimport java.util.Queue;%0A%0Aimport datastructures.tree.TreeNode;%0A%0A/**%0A * @author Sudharsanan Muralidharan%0A */%0Apublic class TreeUtil %7B%0A  %0A  @SuppressWarnings(%22rawtypes%22)%0A  public static String breadthFirstTraversal(TreeNode root) %7B%0A    Queue%3CTreeNode%3E queue = new LinkedList%3CTreeNode%3E();%0A    int depth = 0, numElementsAtDepth = 0;%0A    StringBuilder builder = new StringBuilder();%0A%0A    queue.add(root);%0A    builder.append(%22Level: %22).append(depth).append(%22 =%3E %5B %22);%0A    numElementsAtDepth = queue.size();%0A%0A    while (!queue.isEmpty()) %7B%0A      TreeNode node = queue.poll();%0A      numElementsAtDepth--;%0A      builder.append(node.data).append(%22 %22);%0A%0A      if (node.left != null) %7B%0A        queue.add(node.left);%0A      %7D%0A%0A      if (node.right != null) %7B%0A        queue.add(node.right);%0A      %7D%0A%0A      if (numElementsAtDepth == 0) %7B%0A        depth++;%0A        if (!queue.isEmpty()) %7B%0A          builder.append(%22%5D%22).append(%22%5Cn%22);%0A          builder.append(%22Level: %22).append(depth).append(%22 =%3E %5B %22);%0A          numElementsAtDepth = queue.size();%0A        %7D%0A      %7D%0A    %7D%0A    builder.append(%22%5D%22).append(%22%5Cn%5Cn%22);%0A    builder.append(%22Height of the Tree: %22 + depth);%0A%0A    return builder.toString();%0A  %7D%0A  %0A  @SuppressWarnings(%22rawtypes%22)%0A  public static String inOrder(TreeNode root) %7B%0A    StringBuilder builder = new StringBuilder();%0A    if (root != null) %7B%0A      builder.append(inOrder(root.left));%0A      builder.append(root.data).append(%22 %22);%0A      builder.append(inOrder(root.right));%0A    %7D%0A%0A    return builder.toString();%0A  %7D%0A%0A  @SuppressWarnings(%22rawtypes%22)%0A  public static String preOrder(TreeNode root) %7B%0A    StringBuilder builder = new StringBuilder();%0A    if (root != null) %7B%0A      builder.append(root.data).append(%22 %22);%0A      builder.append(preOrder(root.left));%0A      builder.append(preOrder(root.right));%0A    %7D%0A%0A    return builder.toString();%0A  %7D%0A%0A  @SuppressWarnings(%22rawtypes%22)%0A  public static String postOrder(TreeNode root) %7B%0A    StringBuilder builder = new StringBuilder();%0A    if (root != null) %7B%0A      builder.append(postOrder(root.left));%0A      builder.append(postOrder(root.right));%0A      builder.append(root.data).append(%22 %22);%0A    %7D%0A%0A    return builder.toString();%0A  %7D%0A%7D%0A

Move Tree common functions to TreeUtil.TODO: Do for all DS