• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Perform Inorder, Non-Recursive Traversal of a Binary Tree

laptop infographic online business 6087062

Introduction

Tree traversal is a key concept in data structures, involving the systematic visiting of all nodes in a tree. In the case of inorder traversal, the process involves visiting the left child, then the current node, and finally the right child. Although recursion is a common way to implement this, using an iterative (non-recursive) method can be more effective, especially in systems with limited stack memory. In this article, we will discuss Non-Recursive Traversal of a binary tree.

What Is Inorder Traversal?

Inorder traversal is one of the standard ways to navigate a binary tree. The steps involved are:

  • First, explore the left subtree
  • Next, access the current node
  • Finally, explore the right subtree

When applied to a Binary Search Tree (BST), this traversal method results in the values being visited in ascending order.

Why Use an Iterative Method?

While recursion provides a concise way to implement tree traversal, it depends on the system’s call stack. This can lead to stack overflow issues when dealing with very deep or unbalanced trees. An iterative approach, which uses a manually managed stack, avoids this risk and gives developers more explicit control over the execution flow.

Java Implementation

Here’s how you can perform an inorder traversal in Java using an iterative technique. This version uses a Stack to keep track of nodes that need to be revisited, effectively replacing the call stack used in a recursive approach.

public class InorderTraversal {
	
	class TreeNodeStructure {
	    int value;
	    TreeNodeStructure left;
	    TreeNodeStructure right;

	    TreeNodeStructure(int value) {
	        this.value = value;
	        this.left = null;
	        this.right = null;
	    }
	}

    public static void inorderIterative(TreeNodeStructure root) {
        Stack<TreeNodeStructure> stack = new Stack<>();
        TreeNodeStructure currentNode = root;

        while (currentNode != null || !stack.isEmpty()) {
            // Reach the leftmost node of the current node
            while (currentNode != null) {
                stack.push(currentNode);
                currentNode = currentNode.left;
            }

            // Current must be null at this point
            currentNode = stack.pop();
            System.out.print(currentNode.value + " ");
            
            // Visit the right subtree
            currentNode = currentNode.right;
        }
    }

    public static void main(String[] args) {
        /*
            Constructing the following binary tree:
                    4
                   / \
                  2   5
                 / \
                1   3
        */
        TreeNodeStructure root = new InorderTraversal().new TreeNodeStructure(4);
        root.left = new InorderTraversal().new TreeNodeStructure(2);
        root.right = new InorderTraversal().new TreeNodeStructure(5);
        root.left.left = new InorderTraversal().new TreeNodeStructure(1);
        root.left.right = new InorderTraversal().new TreeNodeStructure(3);

        System.out.println("Inorder traversal (non-recursive):");
        inorderIterative(root);
    }
}

Explanation of the Code

The method inorderIterative begins by initializing a stack and setting the current node to the root. It then enters a loop that continues as long as there are unvisited nodes. The inner loop descends to the leftmost node, pushing each node onto the stack along the way. Once there are no more left children, it processes the top node on the stack (visiting it), then shifts to the right child of the visited node. This process repeats until all nodes have been visited.

Conclusion

Inorder traversal is essential for many tree-related operations, and understanding how to implement it iteratively is valuable for writing efficient, stack-safe Java applications. The non-recursive approach outlined above provides a reliable and controllable method for tree traversal that avoids the limitations of recursion.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions