Binary Tree



Program In Java To Implement Binary Tree.

The Following Program Reads The Root Data From User And Displays Menu Containing Binary Tree Operations/Traversal Techniques And Reads Users Choice And Works According To Input And Displays Message Accordingly.
This Menu Is In Infinite Loop Until User Enters Exit.


import java.util.*;
class Node {
    int info;
    Node right, left;
    Node() {
        left = right = null;
    }
    void insert(int d) {
        Node p = new Node();
        p.info = d;
        if (d < this.info) {
            if (this.left != null) this.left.insert(d);
            else this.left = p;
        } else {
            if (this.right != null) this.right.insert(d);
            else this.right = p;
        }
    }
    void makeRoot(int d) {
        this.info = d;
    }
    void inOrder(Node root) {
        if (root != null) {
            inOrder(root.left);
            System.out.println(root.info);
            inOrder(root.right);
        }
    }
    void postOrder(Node root) {
        if (root != null) {
            postOrder(root.left);
            postOrder(root.right);
            System.out.println(root.info);
        }
    }
    void preOrder(Node root) {
        if (root != null) {
            System.out.println(root.info);
            preOrder(root.left);
            preOrder(root.right);
        }
    }
}
class BTree {
    public static void main(String[] arg) {
        Node bt = new Node();
        int ch, e;
        Scanner sc = new Scanner(System.in);
        System.out.print("\nEnter Root Data::");
        int r = sc.nextInt();
        bt.makeRoot(r); //make root
        while (true) {
            System.out.println("\n\n\tImplementation Of Binary Tree");
            System.out.println("\n1. Insert\n2. Inorder\n3. Preorder\n4. Postorder\n5. Exit");
            System.out.print("\tEnter Your Choice::");
            ch = sc.nextInt();
            switch (ch) {
                case 1: //Inserting Node In Linked List
                    System.out.print("\n\tEnter Data To Be Inserted::");
                    e = sc.nextInt();
                    bt.insert(e);
                    break;
                case 2: //Inorder Traversal
                    System.out.println("\nInorder Traversal:");
                    bt.inOrder(bt);
                    break;
                case 3: //Preorder Traversal
                    System.out.println("\nPreorder Traversal:");
                    bt.preOrder(bt);
                    break;
                case 4: //Postorder Traversal
                    System.out.println("\nPostorder Traversal:");
                    bt.postOrder(bt);
                    break;
                case 5: //Exiting
                    System.out.println("\n\tExited Successfully.");
                    System.exit(1);
                    break;
                default:
                    System.out.println("\n\tImproper Input.\n\tEnter Proper Input.");
                    break;
            }
        }
    }
}


OUTPUT::


Enter Root Data::10
Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::1

Enter Data To Be Inserted::20

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::1

Enter Data To Be Inserted::30

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::1

Enter Data To Be Inserted::6

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::1

Enter Data To Be Inserted::8

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::1

Enter Data To Be Inserted::5

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::2

Inorder Traversal: 5
6
8
10
20
30

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::3

Preorder Traversal: 10
6
5
8
20
30

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::4

Postorder Traversal: 5
8
6
30
20
10

Implementation Of Binary Tree
1. Insert
2. Inorder
3. Preorder
4. Postorder
5. Exit

Enter Your Choice::5

Exited Successfully.
Previous Post Next Post