0

I came up with an algorithm to check if two trees are structurally similar i.e. the data at the corresponding nodes in the two trees may be different but if a node in tree1 has one left child then it's corresponding node in tree2 must have one left child as well.

Algorithm(node * root1, node * root2) :

    1. Create 2 queues Q1,Q2 and enqueue root1 and root2.
    2. while(Q1 and Q2 are not empty)
         2.a temp1 = deQ(Q1);
         2.b temp2 = deQ(Q2);
           2.c if temp1 has left child then enqueue temp1's left child in Q1
           2.d if temp2 has left child then enqueue temp2's left child in Q2
           2.e if temp1 has right child then enqueue temp1's right child in Q1
           2.f if temp2 has right child then enqueue temp2's right child in Q2
         2.g now check if the size of Q1 and Q2 is equal
         2.h if the size is equal then continue else the two trees are not similar
    3.  End

Is this algorithm correct?

Dubby
  • 1,154
  • 3
  • 16
  • 31
  • No, it's not. It will recognize a node with only a left child as equal to a node with only a right child. – Nico Schertler Jun 06 '14 at 07:40
  • @NicoSchertler ahh! that's a valid point! Could you point out a correction? – Dubby Jun 06 '14 at 07:46
  • So, you apparently meant *binary* tree, did you? The statement of the problem never mentions that while the algorithm seems to rely on that. Could it actually be that you were supposed to work with general trees, not just binary ones? (Granted, any tree can be mapped to an equivalent binary tree, but still...) – AnT stands with Russia Jun 06 '14 at 07:58

1 Answers1

1

At present your algorithm as such will not work. For example if tree1 root has a right child only and tree2 has a left child only, then your algorithm shall output a false positive.

You will have to modify the algorithm as follows. It is a recursive approach, but there are other ways possible.

Algorithm(node * root1, node * root2) :

   // if exactly one is NULL and other is not, return false.
   if ( root1 && !root2 ) || ( !root1 && root2 )
       return 0.

   // if both are NULL, return true.
   if ( !root1 && !root2 )
       return 1.

   // Both are valid nodes. So now check there respective child sub-trees.
   return Algorithm( root1->left, root2->left ) && Algorithm( root1->right, root2->right )

Your algorithm:

    Algorithm(node * root1, node * root2) :
        1. Create 2 queues Q1,Q2 and enqueue root1 and root2.
        // here you skip the Null check for root1 and root2. Should be added.
        2. while(Q1 is not empty or Q2 is not empty)
             2.a temp1 = deQ(Q1);
             2.b temp2 = deQ(Q2);
   // Now Compare the respective children of temp1 & temp2. If not equal, then return false.
               2.c if temp1 has left child then enqueue temp1's left child in Q1
               2.d if temp2 has left child then enqueue temp2's left child in Q2
               2.e if temp1 has right child then enqueue temp1's right child in Q1
               2.f if temp2 has right child then enqueue temp2's right child in Q2
        3.  return true.
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46