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:
Java Program to Implement Range Tree
New Features in Java 14
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Introduction to Spring Data JPA
Request Method Not Supported (405) in Spring
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Adjacency List
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
The XOR Operator in Java
Tính đóng gói (Encapsulation) trong java
Merging Two Maps with Java 8
Jackson – Marshall String to JsonNode
Guide to the ConcurrentSkipListMap
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Tổng quan về ngôn ngữ lập trình java
Java Program to Implement TreeMap API
Reactive WebSockets with Spring 5
Java Program to Evaluate an Expression using Stacks
Generating Random Numbers in a Range in Java
Java Program to Implement Euclid GCD Algorithm
Java Program to Implement Word Wrap Problem
OAuth 2.0 Resource Server With Spring Security 5
Handling Errors in Spring WebFlux
Java Program to Implement Strassen Algorithm
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Ways to Iterate Over a List in Java
Java 8 – Powerful Comparison with Lambdas
Hướng dẫn Java Design Pattern – Builder
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Implement Weight Balanced Tree
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Login For a Spring Web App – Error Handling and Localization