Java Program to Perform the inorder tree traversal

Updated on December 20, 2024
Perform the inorder tree traversal header image

Introduction

In computer science, tree traversal is a fundamental technique often employed for searching, sorting, and manipulating hierarchical data structures. Among various tree traversal methods, the inorder traversal of a binary tree holds a special place, especially when it comes to retrieving data in a sorted order directly from a binary search tree. This method involves visiting the left subtree first, then the node, and finally the right subtree.

In this article, you will learn how to perform inorder tree traversal in Java. Explore how to implement this traversal technique through a well-structured binary tree class and understand its practical applications through detailed examples. This knowledge is not only essential for academic purposes but also useful in real-world applications where hierarchical data processing is required.

Implementing Inorder Traversal in Java

Understanding The Binary Tree Node Class

  1. Define the TreeNode class.

  2. Include integer data and pointers to the left and right child nodes.

    java
    class TreeNode {
        int data;
        TreeNode left, right;
    
        TreeNode(int item) {
            data = item;
            left = right = null;
        }
    }
    

    This block defines a simple TreeNode class essential for tree constructs, it holds the data and connects to the left and right child nodes. The constructor initializes the node with a specific value.

Creating the Tree Class

  1. Define the BinaryTree class.

  2. Initialize a root node as null initially.

    java
    class BinaryTree {
        TreeNode root;
    
        BinaryTree() {
            root = null;
        }
    }
    

    In this snippet, the BinaryTree class is designed to encapsulate the tree structure. The root node is initialized to null representing an empty tree initially.

Implementing the Inorder Traversal Method

  1. Add an inorder traversal method in the BinaryTree class.

  2. Conduct the traversal recursively.

    java
    class BinaryTree {
        TreeNode root;
    
        BinaryTree() {
            root = null;
        }
    
        void inorderTraversal(TreeNode node) {
            if (node == null)
                return;
    
            inorderTraversal(node.left);
            System.out.print(node.data + " ");
            inorderTraversal(node.right);
        }
    
        void printInorder() {
            inorderTraversal(root);
        }
    }
    

    The method inorderTraversal is a recursive function that first calls itself on the left child, prints the data of the node, and then calls itself on the right child, ensuring elements are visited in the inorder sequence.

Example: Constructing a Tree and Performing Inorder Traversal

  1. Create instances and construct a binary tree.

  2. Invoke the inorder traversal.

    java
    public class Main {
        public static void main(String[] args) {
            BinaryTree tree = new BinaryTree();
            tree.root = new TreeNode(1);
            tree.root.left = new TreeNode(2);
            tree.root.right = new TreeNode(3);
            tree.root.left.left = new TreeNode(4);
            tree.root.left.right = new TreeNode(5);
    
            System.out.println("Inorder traversal of binary tree is:");
            tree.printInorder();
        }
    }
    

    Here, a binary tree is composed manually by assigning values to nodes and tree structure. After building the tree, printInorder() is called to perform and display the inorder traversal of the tree.

Conclusion

Employing inorder traversal in Java allows for efficient processing and output of binary tree data in a naturally sorted order, which is especially useful for binary search trees. The understanding gained here lays a strong foundation for further exploration of other tree-based algorithms like those for tree modifications, balancing, or even more complex operations handling larger and more dynamic datasets. By integrating these examples into your Java projects, you ensure your hierarchical data manipulations are both effective and efficient.