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:
Một số từ khóa trong Java
Java Program to Implement Karatsuba Multiplication Algorithm
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Find Maximum Element in an Array using Binary Search
What is Thread-Safety and How to Achieve it?
Guide to Java OutputStream
Spring Boot Tutorial – Bootstrap a Simple Application
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Perform Deletion in a BST
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Spring Boot - Database Handling
Guide to the Volatile Keyword in Java
Java Program to Implement Binary Search Tree
Removing Elements from Java Collections
Java Program to Check whether Undirected Graph is Connected using BFS
Remove the First Element from a List
Java – Create a File
Iterable to Stream in Java
HashSet trong Java hoạt động như thế nào?
Java Program to Implement Randomized Binary Search Tree
Hashtable trong java
Control Structures in Java
Compare Two JSON Objects with Jackson
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java Program to Implement Horner Algorithm
Java String Conversions
HttpClient 4 – Follow Redirects for POST
Java Program to Perform the Sorting Using Counting Sort
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Perform Encoding of a Message Using Matrix Multiplication