top of page

Java Program to implement Binary Tree operations

In this blog, we will learn how to find the largest element in the binary tree, height of the binary tree and count the number of leaf nodes.


Implementation using Java:


import java.util.Scanner;
public class BinaryTreeUse<T> {
        public T data;
	public BinaryTreeUse<T> left;
	public BinaryTreeUse<T> right;
	 public BinaryTreeUse(T data)
	{
		this.data=data;
	}
	public static void main(String[] args) {
		BinaryTreeUse<Integer> root=takeInput(true,1,true);
		printTree(root);
		int calHeight=height(root);
		System.out.println("Height of the tree is "+calHeight);
		int local=largest(root);
		System.out.println("largest element is"+local);
		int local2=countLeafNode(root);
		System.out.println("the number of leaf nodes are"+local2);
		calDepth(root,1);
		}
	private static int height(BinaryTreeUse<Integer> root) {		
		if(root==null)
		{
			return 0;
		}
		int leftHeight=height(root.left);
		int rightHeight=height(root.right);
		
		return 1+Math.max(leftHeight, rightHeight); 
	}
	public static int countLeafNode(BinaryTreeUse<Integer> root) {
		
		if(root==null)
		{
			return 0;
		}
		if(root.left==null && root.right==null)
		{
			return 1;
		}
		
		return countLeafNode(root.left)+countLeafNode(root.right);
		}
	public static int largest(BinaryTreeUse<Integer> root) {
		if(root==null)
		{
			return -1;
		}
		int largestLeft=largest(root.left);
		int largestRight=largest(root.right);
		
		return Math.max(root.data, Math.max(largestLeft, largestRight));
		}
	private static BinaryTreeUse<Integer> takeInput(boolean isRoot, int parentData, boolean isLeft) {
		if(isRoot)
		{
			System.out.println("Enter the root element");
		}else
		{
			if(isLeft)
			{
				System.out.println("Enter the left child of"+parentData);
			}
			else
			{
				System.out.println("Enter the right child of"+parentData);
			}
		}
		Scanner sc=new Scanner(System.in);
		int rootData=sc.nextInt();
		
		if(rootData==-1)
		{
			return null ;
		}
        BinaryTreeUse<Integer> root=new BinaryTreeUse<Integer>(rootData);
		BinaryTreeUse<Integer> leftChild=takeInput(false,rootData,true);
		BinaryTreeUse<Integer> rightChild=takeInput(false,rootData,false);
		
		root.left=leftChild;
		root.right=rightChild;
		return root;
}
        public static void printTree(BinaryTreeUse<Integer> root) {
		if(root==null)
		{
			return;
		}
		System.out.print(root.data+":");
		if(root.left!=null)
		{
			System.out.print("L"+root.left.data+",");
		}
		if(root.right!=null)
		{
			System.out.print("R"+root.right.data+",");
		}
		System.out.println();
		printTree(root.left);
		printTree(root.right);
	}
}
Input: 
Enter the root element: 3
Enter the left child of 3: 2
Enter the left child of 2: 1
Enter the left child of 1: -1
Enter the right child of 1: 0
Enter the left child of 0: -1
Enter the right child of 0: -1
Enter the right child of 2: 9
Enter the left child of 9: -1
Enter the right child of 9: -1
Enter the right child of 3: 5
Enter the left child of 5: 4
Enter the left child of 4: -1
Enter the right child of 4: -1
Enter the right child of 5: 6
Enter the left child of 6: -1
Enter the right child of 6: -1
Output:
3:L2,R5
2:L1,R9
1:R0
5:L4,R6
Height of the tree is 4
largest element is 9
The number of leaf nodes are 4  
 

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor

Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

16 views0 comments

Recent Posts

See All
bottom of page