Search code examples
javaalgorithmbinary-treedepth-first-searchtree-traversal

Is it possible to print each level of a binary tree on a separate line using depth-first traversal?


I am familiar with the use of a queue to print each level in a separate line in O(n) time. I want to know if there is any way to do this using pre-order, in-order or post-order traversal. I have following code but I have no idea what to do with depth parameter. Only idea I have is using an array of linked lists to store all nodes while traversing the tree, consuming O(n) extra space. Is there any better way to do this?

class Node {
 int key;
 Node left;
 Node right;

 Node(int value) {
    key = value;
    left = null;
    right = null;
 }
}

public class bst {

 private Node root;

 bst() {
    root = null;
 }

 private void printTreeRec(Node root, int depth) {
    if(root != null) {
        System.out.println(root.key);
        printTreeRec(root.left, depth + 1);
        printTreeRec(root.right, depth + 1);
    }
 }

 void printTree() {
    printTreeRec(root, 0);
 }

 public static void main(String[] Args) {
    bst tree = new bst();

    tree.insert(25);
    tree.insert(15);
    tree.insert(35);
    tree.insert(7);
    tree.insert(18);
    tree.insert(33);
    tree.insert(36);

    tree.printTree();
 }
}

Solution

  • It is not possible with the mentioned approaches indeed because they go depth-first that is they always go in a direction until they reach the end of the branch.

    Or at least not with printing the output directly on the console, as the algorithm executes.

    This is not a rigorous proof but it shall describe the issue:

    Preorder

        System.out.println(root.key);
        printTreeRec(root.left, depth + 1);
        printTreeRec(root.right, depth + 1);
    

    At a particular node you print the current node first. Then, you go left, and you print that node, and so on. As you have already moved down the console, and you can't go back, this approach won't work

    Inorder

        printTreeRec(root.left, depth + 1);
        System.out.println(root.key);
        printTreeRec(root.right, depth + 1);
    

    In this case you start at the root, and you go left. Still left, until there are no more children nodes. And then you print. But now you will go up the tree, and what will you do with the focus on the console line? Once again you have to move forward. Also not possible in this case.

    Postorder

        printTreeRec(root.left, depth + 1);
        printTreeRec(root.right, depth + 1);
        System.out.println(root.key);
    

    In this case you start at the root, and you go left. Until you can. When you can't you start going right. Go right until you can. Then start printing. But now, similarly as above, you will go up the tree, and what will you do with the focus on the console line? Once again you have to move forward. Not possible again.

    How can we make it work?

    We should cheat, and pass a level variable to know the level at which we are at a particular moment. Fill a data structure, like map, that will contain node values per level, and after the algorithm is finished computing, print the result, one level per line, using the map.