m2skills

isomorphic bt java

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