Determines whether the T1 tree contains all the topologies of the T2 tree public class containsubtree{the public static class node{the public int value;
Public Node left;
public Node right;
public Node (int data) {this.value=data;
}//To determine whether to include public static Boolean Iscontainsubtree (Node h1,node h2) {if (h2==null) {
return true;
} if (H1==null) {return false; ///Three case matching (recursive call) return check (H1,H2) | | Iscontainsubtree (H1.LEFT,H2) | |
Iscontainsubtree (H1.RIGHT,H2);
public static Boolean check (Node h1,node h2) {if (H2==null) {return true; } if (h1==null| |
H1.value!=h2.value) {return false;
return check (h1.left,h2.left) &&check (h1.right,h2.right);
public static void Main (String []args) {//system.out.print ("Hello");
Node H1=new node (1);
H1.left=new Node (2); H1.right=new Node(3);
H1.left.left=new Node (4);
H1.left.right=new Node (5);
H1.right.left=new Node (6);
H1.right.right=new Node (7);
H1.left.left.left=new Node (8);
H1.left.left.right=new Node (9);
H1.left.right.left=new Node (10);
Node H2=new node (2);
H2.left=new Node (4);
H2.right=new Node (5);
H2.left.left=new Node (8);
System.out.print (Iscontainsubtree (H1,H2)); }
}