Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree

This Java program is to Implement binary tree and check whether a tree is subtree of another. This can be done in two ways. A tree can be subtree of another if they have same structure (same object references but different value) and with same structure and values. This given class checks for both.

Here is the source code of the Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to check whether a binary tree is subtree of another tree
class Btrees
{
    Object value;
    Btrees Left;
    Btrees Right;
 
    Btrees(int k)
    {
        value = k;
    }
}
 
public class SubBinaryTree
{
    public static boolean ifsubtreeRef(Btrees t1, Btrees t2)
    {
        if (t2 == null)
            return true;
        if (t1 == null)
            return false;
        return (t1 == t2) || ifsubtreeRef(t1.Left, t2)
                || ifsubtreeRef(t1.Right, t2);
    }
 
    public static boolean ifsubtreeValue(Btrees t1, Btrees t2)
    {
        if (t2 == null)
            return true;
        if (t1 == null)
            return false;
        if (t1.value == t2.value)
            if (ifsubtreeValue(t1.Left, t2.Left)
                    && ifsubtreeValue(t1.Right, t2.Right))
                return true;
        return ifsubtreeValue(t1.Left, t2) || ifsubtreeValue(t1.Right, t2);
    }
 
    public static void main(String[] args)
    {
        Btrees t1 = new Btrees(1);
        t1.Left = new Btrees(2);
        t1.Right = new Btrees(3);
        t1.Right.Left = new Btrees(4);
        t1.Right.Right = new Btrees(5);
 
        Btrees t2 = new Btrees(3);
        t2.Left = new Btrees(4);
        t2.Right = new Btrees(5);
 
        if (ifsubtreeRef(t1, t2))
            System.out.println("T2 is sub-tree of T1 (Reference wise)");
        else
            System.out.println("T2 is NOT sub-tree of T1 (Reference wise)");
 
        if (ifsubtreeValue(t1, t2))
            System.out.println("T2 is sub-tree of T1 (Value wise)");
        else
            System.out.println("T2 is NOT sub-tree of T1 (Value wise)");
    }
}

Output:

$ javac SubBinaryTree.java
$ java SubBinaryTree
 
T2 is NOT sub-tree of T1 (Reference wise)
T2 is sub-tree of T1 (Value wise)

Related posts:

A Guide to Concurrent Queues in Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Adding a Newline Character to a String in Java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Lớp Collectors trong Java 8
A Guide to System.exit()
Jackson vs Gson
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Perform Partition of an Integer in All Possible Ways
Java CyclicBarrier vs CountDownLatch
Java Program to Perform Searching in a 2-Dimension K-D Tree
Java Program to find the peak element of an array using Binary Search approach
String Operations with Java Streams
Java Program to Solve any Linear Equations
Create a Custom Auto-Configuration with Spring Boot
Spring Security Logout
Java Program to Implement TreeMap API
Exception Handling in Java
Java Program to Implement Caesar Cypher
Spring Data JPA @Query
DistinctBy in the Java Stream API
Java Program to Implement ConcurrentHashMap API
Lấy ngày giờ hiện tại trong Java
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Quick Guide to Spring Bean Scopes
Java Program to Find Nearest Neighbor Using Linear Search
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Implement IdentityHashMap API
Jackson – Marshall String to JsonNode
Java Program to Solve a Matching Problem for a Given Specific Case
Introduction to the Java ArrayDeque