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:
Write/Read cookies using HTTP and Read a file from the internet
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to implement Priority Queue
Hướng dẫn Java Design Pattern – Singleton
Why String is Immutable in Java?
Get and Post Lists of Objects with RestTemplate
Guide to java.util.concurrent.BlockingQueue
Mockito and JUnit 5 – Using ExtendWith
Guide to Dynamic Tests in Junit 5
Java Program to Implement Lloyd’s Algorithm
Chuyển đổi từ HashMap sang ArrayList
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Implement HashSet API
Custom Exception trong Java
@Lookup Annotation in Spring
HashSet trong Java hoạt động như thế nào?
Java Program to Implement Booth Algorithm
Sorting in Java
Check If a File or Directory Exists in Java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Jackson Ignore Properties on Marshalling
Spring’s RequestBody and ResponseBody Annotations
A Guide to TreeMap in Java
Java Program to Implement LinkedHashMap API
Java Program to Implement Shoelace Algorithm
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Guide to the Volatile Keyword in Java
Từ khóa throw và throws trong Java
Java Program to Find Nearest Neighbor for Dynamic Data Set
Java – Random Long, Float, Integer and Double
Introduction to Using Thymeleaf in Spring
Java Program to Implement Hash Tables with Linear Probing