Java implementation of Binary Tree Traversals - IT magazine

IT magazine

Knowledge that matters

Java implementation of Binary Tree Traversals

Share This

Java implementation of Binary Tree Traversals 

In order to implement the binary tree traversals recursive methods are used 
  
//Program to implement Binary Tree and perform tree traversals
import java.util.*;
class Node
{
    public int data;
    public Node left, right;
    public Node (int d)
    {
        left = right = null;
        data = d;
    }
    public void setLeft (Node n)
    {
        left = n;
    }
    public void setRight (Node n)
    {
        right = n;
    }
}
class BinaryTree
{
    public Node root;
    public BinaryTree ()
    {
        root = null;
    }
    public boolean isEmpty()
    {
        return root == null;
    }
    public void insert(int item)
    {
        root = insertNode(root, item);
    }
    /* Function to insert data recursively */
    public Node  insertNode(Node r, int item)
    {
        if (r == null)
            r=new Node(item);
        else
        {
            if(r.data==item) //check whether the item is duplicate
                System.out.println("Dulpcate");
            else if(item<r.data) //check whether item less than root
                r.setLeft(insertNode(r.left,item)); //set left
            else
                r.setRight(insertNode (r.right, item)); //set right
        }
        return r;
    }
     /* Function for inorder traversal */
    public void inorder()
    {
        inorder(root);
    }
    public void inorder(Node r)
    {
        if (r != null)
        {
            inorder(r.left);
            System.out.print(r.data+" ");
            inorder(r.right);
        }
    }
     /* Function for preorder traversal */
    public void preorder()
    {
        preorder(root);
    }
    public void preorder(Node r)
    {
        if (r != null)
        {
            System.out.print(r.data+" ");
            preorder(r.left);            
            preorder(r.right);
        }
    }
     /* Function for postorder traversal */
    public void postorder()
    {
        postorder(root);
    }
    public void postorder(Node r)
    {
        if (r != null)
        {
            postorder(r.left);            
            postorder(r.right);
            System.out.print(r.data +" ");
        }
    }
    public int countNodes(Node r)
    {
        if (r == null)
            return 0;
        else
        {
            int l = 1;
            l += countNodes(r.left);
            l += countNodes(r.right);
            return l;
        }
    }
} //closing of BinaryTree Class

public class BinaryTreeApp
 {
    public static void main(String[] args)
    {           
        Scanner sc = new Scanner(System.in);
        BinaryTree BT = new BinaryTree();
        int d;       
        while(true)   
        {
            System.out.println("\nBinary Tree Operations");
            System.out.print("1.Insert\n2.Pre Order\n");
           System.out.println("3.In Order\n4.Post Order\n5.Exit");

            System.out.println("Please enter your option:");
            int choice = sc.nextInt();           
            switch (choice)
            {
                case 1 :
                    System.out.println("Enter integer element to insert");
                    d=sc.nextInt();
                    BT.insert(d);
                    System.out.println("No. of Nodes:"+BT.countNodes(BT.root));
                    break;                         
                case 2 :
                    System.out.println("Pre Order Traversal");
                    BT.preorder();
                    break;                                         
                case 3 :
                    System.out.println("In Order Traversal");
                    BT.inorder();
                    break;    
                case 4 :
                    System.out.println("Post Order Traversal");
                    BT.postorder();
                    break;
                case 5:
                    System.exit(0);
                default :
                    System.out.println("Wrong Option");
                    break;  
            }
        }
    }
 }



No comments:

Post a Comment