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:
Documenting a Spring REST API Using OpenAPI 3.0
HttpClient Connection Management
Deploy a Spring Boot WAR into a Tomcat Server
Java Switch Statement
Java Program to Search for an Element in a Binary Search Tree
Hướng dẫn Java Design Pattern – Decorator
Hướng dẫn Java Design Pattern – Transfer Object
Java CyclicBarrier vs CountDownLatch
Examine the internal DNS cache
Command-Line Arguments in Java
Convert XML to JSON Using Jackson
Send email with SMTPS (eg. Google GMail)
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Perform Finite State Automaton based Search
A Guide to Queries in Spring Data MongoDB
Spring Boot - Tomcat Port Number
Java Program to Implement Knapsack Algorithm
Batch Processing with Spring Cloud Data Flow
How To Serialize and Deserialize Enums with Jackson
The Registration API becomes RESTful
Java Program to Construct an Expression Tree for an Infix Expression
Java InputStream to Byte Array and ByteBuffer
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Giới thiệu thư viện Apache Commons Chain
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Integer Constant Pool trong Java
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Represent Graph Using Adjacency List
Spring Security with Maven
Derived Query Methods in Spring Data JPA Repositories
Registration – Activate a New Account by Email
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree