jain12

check similarity of two structures

Feb 24th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Node{
  5.   public:
  6.       int data;
  7.       Node *left,*right;
  8.       Node(int key){
  9.         data=key;
  10.         left=NULL;
  11.         right=NULL;
  12.         }
  13.   };
  14.  
  15. int CheckSimilarity(Node *root1,Node *root2){
  16.   if(root1==NULL && root2==NULL)
  17.     return 1;
  18.   if(root1==NULL || root2==NULL)
  19.     return 0;
  20.   return(CheckSimilarity(root1->left,root2->left) && CheckSimilarity(root1->right,root2->right));
  21.   }
  22.  
  23.  
  24. int main(){
  25.   Node *root1=NULL;
  26.   Node *newnode=new Node(1);
  27.   root1=newnode;
  28.   root1->left=new Node(2);
  29.   root1->right=new Node(3);
  30.   (root1->left)->left=new Node(4);
  31.   (root1->right)->right=new Node(5);
  32.   ((root1->left)->left)->left=new Node(6);
  33.  
  34.   Node *root2=NULL;
  35.   Node *node=new Node(7);
  36.   root2=node;
  37.   root2->left=new Node(8);
  38.   root2->right=new Node(9);
  39.   (root2->left)->left=new Node(10);
  40.   (root2->right)->right=new Node(11);
  41.   ((root2->left)->left)->left=new Node(12);
  42.   int result=CheckSimilarity(root1,root2);
  43.   if(result==1)
  44.     cout<<"both structures are similar";
  45.   else
  46.     cout<<"both structures are not similar";
  47.   return 0;
  48.   }
Add Comment
Please, Sign In to add comment