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:
Introduction to Java Serialization
Guide to DelayQueue
Performance Difference Between save() and saveAll() in Spring Data
Java Program to Implement Randomized Binary Search Tree
Giới thiệu java.io.tmpdir
A Guide to TreeMap in Java
Consuming RESTful Web Services
Remove All Occurrences of a Specific Value from a List
JUnit 5 @Test Annotation
Optional trong Java 8
Java Program to Implement Leftist Heap
Mệnh đề if-else trong java
New Features in Java 15
Lập trình đa luồng với CompletableFuture trong Java 8
Java String Conversions
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Apache Commons Collections MapUtils
Calling Stored Procedures from Spring Data JPA Repositories
Một số từ khóa trong Java
Spring Boot - Eureka Server
Spring Cloud – Tracing Services with Zipkin
Java Program to Implement ConcurrentSkipListMap API
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Changing Annotation Parameters At Runtime
Java Program to Find a Good Feedback Edge Set in a Graph
OAuth 2.0 Resource Server With Spring Security 5
Immutable Map Implementations in Java
Hashing a Password in Java
Assertions in JUnit 4 and JUnit 5
Spring’s RequestBody and ResponseBody Annotations
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Java Program to Check Whether a Directed Graph Contains a Eulerian Path