Java program to implement in order traversal in binary tree:

              Binary tree is a data structure which stores data in tree structure. It has a root with two child(Left and right child). Root is a parent element.

In order Traversal:

              It follows a simple pattern for traversal (Left -Root -Right).  In the beginning, traverse a left sub tree followed by root node. Finally, Right sub tree is traversed.

Java program to implement in order traversal in binary tree:

  • ·       First, create a binary tree. It needs a node to be defined. So, create a class “node” with three elements(Left and right nodes and key).
  • ·       Next, create a member function Node. Assign the value to key. Left and right values as null.
  • ·       Develop a BinaryTree class. Create a object for Node. Initialise the constructor.
  • ·       A method insert(key)  is created to insert the values to a newnode in binary tree.
  • ·       Next method is insertRec() with root and key. It inserts the key in proper position. Either left or right child of node.
  • ·       There are two methods to implement inorder traversal (inorder(), inorderRec()). These are used to perform in order traversal.
  • ·       Now, in the main(), a new object is created for BinaryTree class. Insert values to the node by insert() function.
  • ·       Call the inorder member function and perform the inorder traversal.
  • ·       Finally, print the tree values.

//Creating  a class node

class node {

    int key;

    node left, right;

    public node(int value) {

        key = value;

        left = right = null;

    }

}

//Creating a class BinaryTree

public class BinaryTree {

    node root;

    public BinaryTree() {

        root = null;

    }

 

    // Insert the key value to root node.

    public void insert(int key) {

        root = insertRec(root, key);

    }

 

    // Insert other key values to node using a recursive function

    private node insertRec(node root, int key) {

        if (root == null) {

            root = new node(key);

            return root;

        }

        if (key < root.key) {

            root.left = insertRec(root.left, key);

        } else if (key > root.key) {

            root.right = insertRec(root.right, key);

        }

        return root;

    }

 

    // Inorder traversal function

    public void inorder() {

        inorderRec(root);

    }

 

    // supportive function for inorder traversal

    private void inorderRec(Node root) {

        if (root != null) {

            inorderRec(root.left);

            System.out.print(root.key + " ");

            inorderRec(root.right);

        }

    }

 

    public static void main(String[] args) {

        BinaryTree tree1 = new BinaryTree();

 

        // Insert values to binary tree

        tree1.insert(78);

        tree1.insert(39);

        tree1.insert(55);

        tree1.insert(20);

        tree1.insert(87);

        tree1.insert(125);

        tree1.insert(60);

 

        // display the values of inorder traversal

        System.out.println("Inorder traversal:");

        tree1.inorder();

    }

}

You will get this output when you execute this program.

This is the way of implementing  a java program to perform inorder traversal of binary tree.

No comments:

Post a Comment