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:
Spring Boot - Code Structure
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Implement the Program Used in grep/egrep/fgrep
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java – Generate Random String
Spring Boot - Build Systems
Java Program to Implement RoleList API
Hướng dẫn Java Design Pattern – Null Object
Logging in Spring Boot
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Find the Mode in a Data Set
Custom Thread Pools In Java 8 Parallel Streams
Jackson – Marshall String to JsonNode
Anonymous Classes in Java
Java Program to Find Basis and Dimension of a Matrix
A Guide to Iterator in Java
Convert Hex to ASCII in Java
Spring’s RequestBody and ResponseBody Annotations
Exploring the Spring 5 WebFlux URL Matching
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Java Program to Find kth Largest Element in a Sequence
SOAP Web service: Authentication trong JAX-WS
Disable Spring Data Auto Configuration
Remove All Occurrences of a Specific Value from a List
Error Handling for REST with Spring
Java Program to Implement Adjacency Matrix
Java Program to Implement Knight’s Tour Problem
New in Spring Security OAuth2 – Verify Claims
Deploy a Spring Boot App to Azure
Binary Tree