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:
Examine the internal DNS cache
Guide to CopyOnWriteArrayList
Java Program to Compute the Area of a Triangle Using Determinants
Converting a Stack Trace to a String in Java
Java Program to Implement Coppersmith Freivald’s Algorithm
Arrays.asList vs new ArrayList(Arrays.asList())
Write/Read cookies using HTTP and Read a file from the internet
Java Map With Case-Insensitive Keys
Hướng dẫn Java Design Pattern – Prototype
Java Program to Check Multiplicability of Two Matrices
Spring Boot - Scheduling
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
A Guide to ConcurrentMap
HashSet trong Java hoạt động như thế nào?
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Validate email address exists or not by Java Code
Creating a Web Application with Spring 5
How to Define a Spring Boot Filter?
Java Program to Implement the Monoalphabetic Cypher
Java Program to Implement Disjoint Sets
Spring MVC Tutorial
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Implement Unrolled Linked List
Spring Boot - Creating Docker Image
Checked and Unchecked Exceptions in Java
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Overview of Spring Boot Dev Tools
Spring NoSuchBeanDefinitionException
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm