
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Perform the inorder tree traversal
In this article, we will understand how to perform the inorder tree traversal. In InOrder traversal, each node is processed between subtrees.In simpler words, visit left subtree, node and then right subtree.
Below is a demonstration of the same −
Suppose our input is −
Run the program
The desired output would be −
The In-Order traversal of the tree_object is: 5->12->6->1->9->
Algorithm
Step 1 - START Step 2 - A class with the data specifications is previously defined. Step 3 - Create a new instance of the class. Step 4 - Initialize the instance with relevant values. Step 5 - Invoke the method to perform inorder traversal. Step 6 - Display the result Step 7 - Stop
Example 1
Here, we have used the recursive method for in-order traversal.
class Node { int item; Node left_node, right_node; public Node(int key) { item = key; left_node = right_node = null; } } public class Tree { Node root; Tree() { root = null; } void inOrder(Node node) { if (node == null) return; inOrder(node.left_node); System.out.print(node.item + "->"); inOrder(node.right_node); } public static void main(String[] args) { Tree tree_object = new Tree(); System.out.println("A tree_object object is defined: "); tree_object.root = new Node(1); tree_object.root.left_node = new Node(12); tree_object.root.right_node = new Node(9); tree_object.root.left_node.left_node = new Node(5); tree_object.root.left_node.right_node = new Node(6); System.out.println("The In-Order traversal of the tree_object is: "); tree_object.inOrder(tree_object.root); } }
Output
A tree_object object is defined: The In-Order traversal of the tree_object is: 5->12->6->1->9->
Example 2
Here, we are using the non-recursive method for in-order traversal.
import java.util.Stack; class Node { int data; Node left_node, right_node; public Node(int item) { data = item; left_node = right_node = null; } } public class tree { Node root; void inorder() { if (root == null) return; Stack<Node> temp_stack = new Stack<Node>(); Node current_node = root; while (current_node != null || temp_stack.size() > 0) { while (current_node != null) { temp_stack.push(current_node); current_node = current_node.left_node; } current_node = temp_stack.pop(); System.out.print(current_node.data + " "); current_node = current_node.right_node; } } public static void main(String args[]) { tree tree = new tree(); System.out.println("A tree_object object is defined: "); tree.root = new Node(1); tree.root.left_node = new Node(2); tree.root.right_node = new Node(3); tree.root.left_node.left_node = new Node(4); tree.root.left_node.right_node = new Node(5); System.out.println("The In-Order traversal of the tree_object is: "); tree.inorder(); } }
Output
A tree_object object is defined: The In-Order traversal of the tree_object is: 4 2 5 1 3
- Related Articles
- Golang program to perform inorder tree traversal
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- Program to perform an Inorder Traversal of a binary tree in Python
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
- C++ Program for Inorder Tree Traversal without Recursion
- Golang Program to Perform the preorder tree traversal
- Golang Program to Perform the postorder tree traversal
- Binary Tree Inorder Traversal in Python
- Program to generate tree using preorder and inorder traversal in python
- Golang program to traverse a given tree in Inorder Traversal (Recursive).
- Python Program to Find Nth Node in the Inorder Traversal of a Tree
- Python Program to Find the Largest value in a Tree using Inorder Traversal
- Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input
- Inorder Traversal of a Threaded Binary Tree in C++
- Program to perform level order traversal of binary tree in C++

Advertisements