Home > other >  Understanding BFS tree traversal by levels on the sample
Understanding BFS tree traversal by levels on the sample

Time:01-25

I'm learning how to traverse a tree by levels. The method I do should take the level of tree number and print back a nodes at the current level. I watched this tutorial - "Print nodes at given Level", but still can't figure out how is recursion working in this concrete sample. So please, help me to understand.

// Java program for Inserting a node
class GFG1 {

    static class node {
        int key;
        node left, right;
    }

    static node newNode(int item)
    {
        node temp = new node();
        temp.key = item;
        temp.left = temp.right = null;
        return temp;
    }

    // Function to insert a new node
    static node insert(node node, int key)
    {
        // If the tree is empty, return a new node
        if (node == null)
            return newNode(key);

        // Otherwise, recur down the tree
        if (key < node.key) {
            node.left = insert(node.left, key);
        }
        else if (key > node.key) {
            node.right = insert(node.right, key);
        }
        return node;
    }

    static void printGivenLevel(node root, int level)
    {
        if (root == null)
            return;
        if (level == 1) {
            System.out.print(" "   root.key);
        }
        else if (level > 1) {
            printGivenLevel(root.left, level - 1);
            printGivenLevel(root.right, level - 1);
        }
    }

    public static void main(String[] args)
    {
        node root = null;
        root = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);
        printGivenLevel(root,2);

    }
}

The method of traversal by levels is printGivenLevel:
So why it requires to write level-1 in printGivenLevel(root.left, level - 1) and not works with 'level 1', for example? How do we come to the base condition, for example on the 3-th or 2-nd tier?

The tree looks like

   50
  30 70
20 40 60 80 

CodePudding user response:

level is an ambiguous term here, in the printGivenLevel it starts from the top "level", which is technically level 1, and we recurse down until we reach our desired "level".

    Actual level       "level" inside printGivenLevel(root, level)    
        1          50              3
        2        30   70           2
        3      20 40 60 80         1

Let's see how printGivenLevel(root, 2) executes, renaming printGivenLevel as print:

 1. print(root, 2) -> print(root.left(30), 1) and print(root.right(70), 1)
 2. print(root(30), 1) -> we are at "level 1", print the value 30.
 3. print(root(70), 1) -> we are at "level 1", print the value 70.

The base condition always reaches when "level" becomes equal to 1, we start counting down from the top level, and when we reach our desired level, the "level" count becomes 1 and we print the value of the node.

So why it requires to write level-1 in printGivenLevel(root.left, level - 1) and not works with 'level 1', for example? How do we come to the base condition, for example on the 3-th or 2-nd tier?

If level is always increased, it'll keep on increasing and won't reach level 1, no value would be printed and it'd result in stackoverflow.

  • Related