m2skills

identical java

Jun 4th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to check if 2 given binary trees are identical or not
  3.  
  4. /**
  5.  * Created by MOHIT on 25-05-2018.
  6.  */
  7.  
  8. import java.io.*;
  9. import java.lang.reflect.Array;
  10. import java.util.*;
  11.  
  12. import static java.lang.Integer.max;
  13.  
  14.  
  15. // node class
  16. class node{
  17.     int data;
  18.     node left;
  19.     node right;
  20.  
  21.     // function that returns a pointer to new node
  22.     public node(int element){
  23.         this.data = element;
  24.         this.left = null;
  25.         this.right = null;
  26.        
  27.     }
  28. };
  29.  
  30.  
  31. public class BinaryTree {
  32.  
  33.     // function to check if 2 given trees are identical to each other
  34.     static boolean isIdentical(node root1, node root2){
  35.         // if both the nodes are null then return true
  36.         if (root1 == null && root2 == null){
  37.             return true;
  38.         }
  39.        
  40.         // if the node are not null then check
  41.         // 1. do nodes have same data
  42.         // 2. do the have identical left subtrees
  43.         // 3. do the have identical right subtrees
  44.         if (root1 != null && root2 != null){
  45.             return (
  46.                 root1.data == root2.data &&
  47.                 isIdentical(root1.left, root2.left) &&
  48.                 isIdentical(root1.right, root2.right)
  49.             );
  50.         }
  51.         return false;
  52.     }
  53.  
  54.  
  55.     public static void main(String arg[]) {
  56.         node head = new node(1);
  57.         head.left = new node(2);
  58.         head.right = new node(3);
  59.         head.left.left = new node(4);
  60.         head.left.right = new node(5);
  61.         head.right.right = new node(6);
  62.         head.left.left.right = new node(7);
  63.         head.right.right.left = new node(8);
  64.         head.left.left.right.left = new node(9);
  65.         head.left.left.right.left.left = new node(10);
  66.         head.right.right.left.right = new node(11);
  67.  
  68.         node head2 = new node(5);
  69.         head2.left = new node(2);
  70.         head2.right = new node(12);
  71.         head2.left.left = new node(-4);
  72.         head2.left.right = new node(3);
  73.         head2.right.left = new node(9);
  74.         head2.right.right = new node(21);
  75.         head2.right.right.left = new node(19);
  76.         head2.right.right.right = new node(25);
  77.  
  78.         node head3 = new node(1);
  79.         head3.left = new node(2);
  80.         head3.right = new node(3);
  81.         head3.left.left = new node(4);
  82.         head3.left.right = new node(5);
  83.         head3.right.right = new node(6);
  84.         head3.left.left.right = new node(7);
  85.         head3.right.right.left = new node(8);
  86.         head3.left.left.right.left = new node(9);
  87.         head3.left.left.right.left.left = new node(10);
  88.         head3.right.right.left.right = new node(11);
  89.  
  90.         System.out.println("TREE #1 and TREE #2 are identical : " + isIdentical(head, head2));
  91.         System.out.println("TREE #1 and TREE #3 are identical : " + isIdentical(head, head3));
  92.  
  93.     }
  94. }
Add Comment
Please, Sign In to add comment