Algorithm


1. If the root is null, return.
2. Perform InorderTraversal(root.left)   // Recursively traverse the left subtree.
3. Visit the root node.                   // Process the current node.
4. Perform InorderTraversal(root.right)  // Recursively traverse the right subtree.

Code Examples

#1 Code Example- Java Program to perform inorder tree traversal

Code - Java Programming

class Node {
  int item;
  Node left, right;

  public Node(int key) {
  item = key;
  left = right = null;
  }
}

class Tree {
  // root of Tree
  Node root;

  Tree() {
  root = null;
  }

  void inOrder(Node node) {
    if (node == null)
      return;

    // traverse the left child
    inOrder(node.left);

    // traverse the root node
    System.out.print(node.item + "->");

    // traverse the right child
    inOrder(node.right);
  }


  public static void main(String[] args) {

    // create an object of Tree
    Tree tree = new Tree();

    // create nodes of tree
    tree.root = new Node(1);
    tree.root.left = new Node(12);
    tree.root.right = new Node(9);

    // create child nodes of left child
    tree.root.left.left = new Node(5);
    tree.root.left.right = new Node(6);

    System.out.println("In Order traversal");
    tree.inOrder(tree.root);
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
In Order traversal
5->12->6->1->9->
Advertisements

Demonstration


Java Programing Example to Perform the inorder tree traversal-DevsEnv