Java Program to Perform the inorder tree traversal

Java Program to Perform the inorder tree traversal

What are Trees?

In this Article, we will write a program to Perform the inorder tree traversal in java.
In Java, a tree is a data structure that is similar to a linked list, but with a hierarchical structure. Each node in a tree has a value and one or more child nodes, and the topmost node is called the root. Trees are commonly used to implement algorithms such as search, sorting, and traversal.

Tree Class:

There is no specific “Tree” class in the core Java libraries. However, the Java Collections Framework does provide several classes that are based on tree data structures. These include the TreeSet and TreeMap classes.
The TreeSet class is a collection that stores elements in a sorted order. The TreeMap class is a map that stores key-value pairs in a sorted order.
Both TreeSet and TreeMap classes provide useful methods for adding, removing, and searching for elements, as well as for traversing the tree in different ways.
Additionally, there are also several other classes in the Java libraries that can be used to implement trees, such as ArrayDeque, LinkedList, and PriorityQueue. Additionally, there are several third party libraries such as Google’s Guava, Apache’s Commons, and others that provide Tree data structures.

What is Tree Traversal?

Tree traversal is a process of visiting all the nodes of a tree. It is used to traverse the tree one node at a time and is used to access or change the data in the nodes. There are three main types of tree traversal:

Java Program to Perform the inorder tree traversal

Program to Perform the inorder tree traversal:

Run
// Importing all the required Packages
import java.util.*;

// Define the tree node class
class Node {
    int value;
    Node left;
    Node right;
    
    // defining the constructor
    Node(int val) {
        value = val;
        left = null;
        right = null;
    }
}

// Define the main class
class Main{
    
    // Function Definition to perform inorder traversal
    public static void inorderTraversal(Node node) {
        if (node == null) {
            return;
        }
        
        // Recursive calls 
        inorderTraversal(node.left);
        System.out.print(node.value + " ");
        inorderTraversal(node.right);
    }
    
    // Define the main function to test the inorder traversal
    public static void main(String[] args) {
        // Creating a binary tree
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        
        // Perform inorder traversal and print the output
        System.out.print("Inorder Traversal: ");
        inorderTraversal(root);
    }
}
Output:

Inorder Traversal: 4 2 5 1 3

Explanation:

In the above program, we define a Node class to represent the tree nodes, and a InorderTraversal class to define the inorderTraversal function and the main function. The inorderTraversal function performs the inorder traversal recursively by traversing the left subtree first, then visiting the current node, and finally traversing the right subtree. The main function creates a sample binary tree and calls the inorderTraversal function to print the inorder traversal output.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription