Wednesday, 17 May 2017

Binary Tree Inorder Traversal in Java

Binary Tree Inorder Traversal in Java OR
Inorder Traversal Java Program


Inorder traversal is one of the way to traverse binary Tree. In Inorder traversal, Left subtree is read first then Root Node and then Right subtree.

Binary Tree Inorder traversal is a very popular interview question so better to understand it properly. 

There are 2 ways of doing Inorder traversal,
  1. Recursive Inorder traversal of Binary tree.
  2. Iterative Inorder traversal of Binary tree.
Inorder Traversal

There are multiple ways to traversing a Binary Tree like, Preorder traversal, Postorder traversal, Inorder traversal, Spiral order traversal, Vertical order traversal, Level order traversal etc.

In this post, we will focus on Inorder Traversal of Binary Tree

Algorithm: 

Inorder traversal: To traverse a binary tree in Inorder fashion,
  1. Traverse the Left subtree.
  2. Visit the root node and print data of that node.
  3. Traverse the Right subtree.
Inorder traversal example.
inorder traversal of binary tree
Inorder traversal of binary tree
Therefore, the Inorder traversal of the above sample tree will output: 15  35  25  75  45.

Let's see Inorder traversal step by step.
Inorder traversal recursion explanation
Inorder traversal with recursion 

Inorder Traversals Java Program.


InorderBinaryTreeTraversal.java
package javabypatel.tree;

import java.util.LinkedList;
import java.util.Queue;

public class InorderBinaryTreeTraversal {
 private Node rootNode;

 public static void main(String[] args) {
  new InorderBinaryTreeTraversal();
 }

 public InorderBinaryTreeTraversal(){

  addNode(rootNode, 45); 
  addNode(rootNode, 25); 
  addNode(rootNode, 75); 
  addNode(rootNode, 15); 
  addNode(rootNode, 35); 
    
  System.out.println("\nIn Order Traversal :");
  printTreeInOrder(rootNode);
 }
  
 //Inorder Printing.
 private void printTreeInOrder(Node rootNode){
  if(rootNode==null)
   return;
  printTreeInOrder(rootNode.getLeft());
  System.out.print(rootNode.getData() + " ");
  printTreeInOrder(rootNode.getRight());
 }
   
 private void addNode(Node rootNode, int data){
  if(rootNode==null){
   Node temp1 = new Node(data);
   this.rootNode=temp1;
  }else{
   addNodeInProperPlace(rootNode, data);
  }
 }
  
 private void addNode(Node rootNode, int data){
  if(rootNode==null){
   Node temp1 = new Node(data);
   this.rootNode=temp1;
  }else{
   addNodeInProperPlace(rootNode, data);
  }
 }
 
 private void addNodeInProperPlace(Node rootNode, int data){ 
  if(data>rootNode.getData()){
   if(rootNode.getRight()!=null){
    addNode(rootNode.getRight(), data);
   }else{
    Node temp1 = new Node(data);
    rootNode.setRight(temp1);
   }
  }else if(data<rootNode.getData()){
   if(rootNode.getLeft()!=null){
    addNode(rootNode.getLeft(), data);
   }else{
    Node temp1 = new Node(data);
    rootNode.setLeft(temp1);
   }
  }
 }
}
Node.java
package javabypatel.tree;

public class Node {
 private int data;
 private Node left;
 private Node right;

 public Node(int data) {
  this.data=data;
 }

 public int getData() {
  return data;
 }
 public void setData(int data) {
  this.data = data;
 }
 public Node getLeft() {
  return left;
 }
 public void setLeft(Node left) {
  this.left = left;
 }
 public Node getRight() {
  return right;
 }
 public void setRight(Node right) {
  this.right = right;
 }
}

You may also like to see


Postorder Traversal Java Program

Preorder Traversal of Binary Tree in Java 

Level order Traversal of Binary Tree in Java 

Zig Zag Traversal of Binary Tree. 

Boundary Traversal of binary tree. 

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

No comments:

Post a Comment