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 @Primary Annotation
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Hướng dẫn sử dụng String Format trong Java
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Implement Min Hash
Spring Data JPA @Modifying Annotation
Java Program to Implement Sieve Of Sundaram
Queue và PriorityQueue trong Java
Java Program to Implement Binomial Heap
How to Read a File in Java
Java Program to Find Nearest Neighbor for Dynamic Data Set
Mệnh đề Switch-case trong java
Hướng dẫn sử dụng lớp Console trong java
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Properties with Spring and Spring Boot
A Guide to TreeSet in Java
Lớp TreeMap trong Java
Java Program to Implement Counting Sort
Java Program to Implement Patricia Trie
Display Auto-Configuration Report in Spring Boot
Why String is Immutable in Java?
Immutable ArrayList in Java
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Implement Randomized Binary Search Tree
Spring Cloud AWS – S3
Spring Boot - OAuth2 with JWT
Java Program to Use rand and srand Functions
Java Program to implement Dynamic Array
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Comparing Long Values in Java
Jackson – Decide What Fields Get Serialized/Deserialized