Java Program to Perform the Inorder Tree Traversal Last Updated : 27 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The binary tree is the hierarchical data structure in which each node has at most two children and it can referred to as the left child and the right child. Inorder tree traversal is one of the fundamental ways to visit all the nodes in the binary tree. It can specifically visiting the nodes in the left-root-right order. Algorithm for Inorder TraversalThe inorder traversal of the binary tree can be implemented recursively. The recursive approach which is more straightforward and commonly used due to its natural, elegant implementation that follows directly from the definition. Algorithm Steps:Visit the Left Subtree: We can begin with the left child of the current node and it can perform the inorder traversal on it.Visit the Current Node: After visiting the entire left subtree and it can process the current node and print it.Visit the Right Subtree: Move to the right child of the current node and it can perform inorder traversal on it.Pseudocode:INORDER(node) if node is not null then INORDER(node.left) visit(node) INORDER(node.right)Program to Perform the Inorder Tree TraversalBelow is the Program to Perform the Inorder Tree Traversal: Java // Node class representing a single node of a binary tree class Node { int data; // Data element stored in the node Node left, right; // Pointers to left and right child // Constructor to create a new node with given data public Node(int item) { data = item; left = right = null; // Initially, left and right children are set to null } } // BinaryTree class that defines a binary tree class BinaryTree { Node root; // Root of the binary tree // Constructor to create an empty binary tree with root set to null BinaryTree() { root = null; } // Recursive function to perform inorder traversal of the tree void inorderTraversal(Node node) { if (node == null) return; // Base case: if current node is null, return inorderTraversal(node.left); // Recursively traverse the left subtree System.out.print(node.data + " "); // Visit the current node (print the node's data) inorderTraversal(node.right); // Recursively traverse the right subtree } // Main method to run the code public static void main(String[] args) { BinaryTree tree = new BinaryTree(); // Create an instance of BinaryTree tree.root = new Node(1); // Set root of the tree tree.root.left = new Node(2); // Set left child of root tree.root.right = new Node(3); // Set right child of root tree.root.left.left = new Node(4); // Set left child of node 2 tree.root.left.right = new Node(5); // Set right child of node 2 // Perform inorder traversal and print the output System.out.println("Inorder traversal of binary tree is: "); tree.inorderTraversal(tree.root); } } OutputInorder traversal of binary tree is: 4 2 5 1 3 Time and Space ComplexityThe time complexity of the inorder traversal is O(n), where n is the number of the nodes in binary tree.The space complexity of the inorder traversal is O(log n).Application of the Binary Trees and Inorder TraversalIt can applies on the Binary Search tree and used to maintain the sorted stream of the data.It can be used in the Syntax tree and used in the compilers to represent the syntax of the programs.It can be used in the Heap trees and used in the priority queues for the efficient implementation of the algorithms.ConclusionBinary trees are the versatile and fundamental data structure used in the various applications in the computer science from maintaining the ordered data in the binary search tree to parsing expressions in the compliers. Inorder traversal is the critical operation that can allows the one to visit all the nodes in the left-root-right order which is particularly useful in the BST to the retrieve elements in the sorted order. Comment More infoAdvertise with us Next Article Java Program for the Preorder Tree Traversal in Binary Tree M maheshkp8kl Follow Improve Article Tags : Java Java-DSA Practice Tags : Java Similar Reads Java Program to Perform the Postorder Tree Traversal The Binary Tree can consist of nodes where each node contains the value and the references to its left and right children. The structure is organized hierarchically with a single root node at the top and each node having at Most Two Children. In this article, we will learn to perform Postorder Tree 3 min read Java Program for the Preorder Tree Traversal in Binary Tree Preorder traversal is the method used to traverse the tree data structure. In the preorder traversal, the nodes are visited in the order: of root, left subtree, and right subtree. It is called as "preorder" because the root node is visited before its children. This traversal technique is widely used 2 min read Level Order Traversal of a Binary Tree in Java Binary Tree is a hierarchical data structure in which each node has at most two children and it can referred to as the left child and right child. It is called a tree because it resembles an inverted tree with the root at the top and branches extending downward. In this article, we will learn to per 3 min read Binary Search Tree (BST) Traversals â Inorder, Preorder, Post Order Given a Binary Search Tree, The task is to print the elements in inorder, preorder, and postorder traversal of the Binary Search Tree. Input: A Binary Search TreeOutput: Inorder Traversal: 10 20 30 100 150 200 300Preorder Traversal: 100 20 10 30 200 150 300Postorder Traversal: 10 30 20 150 300 200 1 10 min read Preorder Tree Traversal of Binary Tree in C A binary tree is a hierarchical data structure composed of nodes where each node has at most two children. It can referred to as the left child and the right child. Due to having a non-linear structure, a binary tree can be traversed in multiple ways. One such way is preorder traversal which is a De 3 min read Preorder Tree Traversal of Binary Tree in C++ A binary tree is a non-linear hierarchical data structure where each node has at most two children which are referred to as the left child and the right child. As it has non-linear structure it can be traversed in multiple ways one such way is pre-order traversal which is a technique in which nodes 4 min read Like