How to implement Java Program to find the height of a Binary Tree?

              Binary tree is always having two nodes. The height of the binary tree is nothing but the levels of the binary tree.

Let us implement this concept in java.

How it Works?

  • ·       Create a binary tree with Node. This Node has a integer data and left child and right child.
  • ·       Initialise the values.
  • ·       Create a binary tree using a class.
  • ·       It has root node.
  • ·       Member Functions are insert(), FindItHeight().
  • ·       ‘main()’ function creates the object for binary tree.
  • ·       Using Scanner class, inputs read from the user.
  • ·       The member functions are called for finding the height.
  • ·       Finally,the height value is displayed in the output screen.

Program:

import java.util.Scanner;

class Node {

    int b_data;

    Node c_left, c_right;

    public Node(int item) {

        b_data = item;

        c_left = c_right = null;

    }

}

class BTreeEg {

    Node root;

    Node insert(Node node, int b_data) {

        if (node == null) {

            return new Node(b_data);

        }

        Scanner s1 = new Scanner(System.in);

        System.out.println("Please Enter 1 to insert left of binary tree " + node.b_data + ", 2 to insert right of the binary tree:");

        int usr_choice = s1.nextInt();

        if (usr_choice == 1) {

            node.c_left = insert(node.c_left, b_data);

        } else {

            node.c_right = insert(node.c_right, b_data);

        }

        return node;

    }

    int findItHeight(Node node) {

        if (node == null)

            return -1; // It means Tree is empty

        int lHeight = findItHeight(node.c_left);

        int rHeight = findItHeight(node.c_right);

        return Math.max(lHeight, rHeight) + 1;

    }

    public static void main(String[] args) {

        Scanner s1 = new Scanner(System.in);

        BTreeEg bt = new BTreeEg();

        System.out.println("Enter the root node value:");

        int root_Value = s1.nextInt();

        bt.root = new Node(root_Value);

        System.out.println("Enter number of the nodes to insert in the binary tree:");

        int n = s1.nextInt();

        for (int i = 0; i < n; i++) {

            System.out.println("Enter the value for node:");

            int value = s1.nextInt();

            bt.insert(bt.root, value);

        }

        System.out.println("Height of the tree is : " + bt.findItHeight(bt.root));

        s1.close();

    }

}

Output:

C:\raji\blog>java BTreeEg

Enter the root node value:

56

Enter number of the nodes to insert in the binary tree:

4

Enter the value for node:

23

Please Enter 1 to insert left of binary tree 56, 2 to insert right of the binary tree:

1

Enter the value for node:

12

Please Enter 1 to insert left of binary tree 56, 2 to insert right of the binary tree:

2

Enter the value for node:

67

Please Enter 1 to insert left of binary tree 56, 2 to insert right of the binary tree:

1

Please Enter 1 to insert left of binary tree 23, 2 to insert right of the binary tree:

15

Enter the value for node:

45

Please Enter 1 to insert left of binary tree 56, 2 to insert right of the binary tree:

1

Please Enter 1 to insert left of binary tree 23, 2 to insert right of the binary tree:

19

Please Enter 1 to insert left of binary tree 67, 2 to insert right of the binary tree:

1

Height of the tree is : 3

This is the way of creating java program to find the length of binary tree. Hope this code is useful to you. Keep Coding!!!

No comments:

Post a Comment