Posted on by Kalkicode
Code Queue

Level order Tree Traversal

The given problem aims to perform a level-order traversal of a binary tree using a queue data structure. Level-order traversal, also known as breadth-first traversal, visits all the nodes in a tree level by level, starting from the root and moving to the leftmost node in each level and then to the right.

Problem Statement

The task is to create a Java program that implements a binary tree with a queue to perform level-order traversal. The program should print the data of each node in the tree in level-order sequence.

Explanation with Example

Let's take an example of the binary tree:

     / \
    2   3
   /   / \
  4   5   6
 /   / \
7   8   9

Idea to Solve the Problem

To perform a level-order traversal, we will use a queue data structure. Starting from the root, we enqueue each node's left and right children while printing the data of the current node. We then dequeue the current node and continue the process until the queue is empty.


1. Create a queue data structure (MyQueue) and a class to represent nodes of the queue (QNode).
2. Create a class to represent the binary tree nodes (TreeNode).
3. Initialize an empty binary tree (BinaryTree) with a root node set to null.
4. Implement methods in MyQueue class:
    - enqueue(TreeNode value): Add a node to the queue.
    - dequeue(): Remove a node from the front of the queue.
    - peek(): Get the data of the front node without removing it.
    - size(): Get the number of nodes in the queue.
    - isEmpty(): Check if the queue is empty.
5. Implement the levelOrder() method in the BinaryTree class to perform level-order traversal using the MyQueue data structure.
6. Initialize the binary tree with nodes.
7. Call the levelOrder() method to print the nodes in level-order sequence.
  1. Create the MyQueue class:

    • Initialize head, tail, and count.
    • Implement enqueue(), dequeue(), peek(), size(), and isEmpty() methods.
  2. Create the TreeNode class:

    • Initialize data, left, and right pointers.
  3. Create the BinaryTree class:

    • Initialize the root of the binary tree.
  4. Implement the levelOrder() method in the BinaryTree class:

    • Initialize an empty queue (MyQueue q).
    • Enqueue the root node to the queue.
    • Loop until the queue is empty:
      • Dequeue a node from the queue.
      • Print the data of the dequeued node.
      • Enqueue its left and right children (if exist).
      • Repeat the loop.
  5. Create a binary tree by adding nodes.

  6. Call the levelOrder() method on the binary tree to print the nodes in level-order sequence.

Code Solution

Resultant Output Explanation

The program will print the data of each node in the binary tree in level-order sequence. In the provided example, the binary tree looks like:

       / \
      2   3
     /   / \
    4   5   6
   /   / \
  7   8   9

The level-order traversal will visit each node level by level and print their data:

Output: 1 2 3 4 5 6 7 8 9

Time Complexity

The time complexity of the level-order traversal using a queue is O(N), where N is the number of nodes in the binary tree. This is because each node is visited once, and enqueuing and dequeuing operations on the queue take constant time.


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment