Another recursive solution:
isIsomorphic(node1, node2)
{
if (node1 != null && node2 != null)
{
if (node1->value != node2->value)
return 0;
if (isIsomorphic(node1->left, node2->left) && isIsomorphic(node1->right, node2->right))
return 1;
if (isIsomorphic(node1->right, node2->left) && isIsomorphic(node1->left, node2->right))
return 1;
}
return node1 == null && node2 == null;
}
On 10/26/06, Karthik Singaram L <
[EMAIL PROTECTED]> wrote:
Let us say we call the following with the roots of both the trees
algo: checkIsomorphism (node1, node2)
{
if(node1==NULL && node2==NULL) return 1;
if(node1==NULL) return 0;
if(node2==NULL) return 0;
child11=node1->left;
child12=node1->right;
child21=node2->left;
child22=node2->right;
if((child11==NULL && child21==NULL) || (child11->value==child21- >value)) // what if child11 is null and child21 is not null? You are going to access child11->value
{
if((child12==NULL && child 22==NULL) || (child12->value==child22- >value)
{
if((child11==NULL && child12==NULL) || (child11->value==child12->value))
{
if((checkIsomorphism(child11, child21) && checkIsomorphism(child12, child22)) || (checkIsomorphism(child11, child22) && checkIsomorphism(child12, child21))
return 1;
else
return 0;
}
else
{
if(checkIsomorphism (child11, child21) && checkIsomorphism(child12, child22))
return 1;
else
return 0;
}
}
else
{
return 0;
}
}
else
{
if((child11==NULL && child22==NULL) || (child11->value == child22->value))
{
if(checkIsomorphism(child11, child22) && checkIsomorphism(child12, child21))
return 1;
}
else
return 0;
}
}On 10/25/06, None <[EMAIL PROTECTED]> wrote:
Hello, how can I compare two trees that only have ints for
isomorphism...
can someone show me a simple algorithm to do this...
--
Deepak Manohar T
Trilogy
09342889008
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups-beta.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---