#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 20
#define YES 1
#define NO 0
struct TreeNode
{
char data;
int leftSon,rightSon;
}Tree1[MAX_SIZE],Tree2[MAX_SIZE];
int buildTree(struct TreeNode Tree[])
{
int n,i,root;
char left,right;
char ch;
printf("输入节点个数:");
scanf("%d",&n);
ch=getchar();
int check[n];
if(n==0)
{
root=-1;
return root;
}
for(i=0;i<n;i++)
{
check[i]=0;
}
printf("输入节点信息:\n");
for(i=0;i<n;i++)
{
scanf("%c%c%c",&Tree[i].data,&left,&right);
ch=getchar();
if(left!='-')
{
Tree[i].leftSon=left-'0';
check[Tree[i].leftSon]=1;
}
else
{
Tree[i].leftSon=-1;
}
if(right!='-')
{
Tree[i].rightSon=right-'0';
check[Tree[i].rightSon]=1;
}
else
{
Tree[i].rightSon=-1;
}
}
for(i=0;i<n;i++)
{
if(check[i]==0)
{
root=i;
break;
}
}
printf("%d",root);
return root;
}
int isomorphism(int root1,int root2)
{
if((root1==-1)&&(root2==-1))
{
return 1;
}
if((root1==-1&&root2!=-1)||(root1!=-1&&root2==-1))
{
return 0;
}
if(Tree1[root1].data!=Tree2[root2].data)
{
return 0;
}
if((Tree1[root1].leftSon==-1)&&(Tree2[root2].leftSon==-1))
{
return isomorphism(Tree1[root1].rightSon,Tree2[root2].rightSon);
}
if((Tree1[root1].leftSon!=-1&&Tree2[root2].leftSon!=-1)&&(Tree1[Tree1[root1].leftSon].data==Tree2[Tree2[root2].leftSon].data))
{
return isomorphism(Tree1[root1].leftSon,Tree2[root2].leftSon)&&isomorphism(Tree1[root1].rightSon,Tree2[root2].rightSon);
}
else
{
return isomorphism(Tree1[root1].leftSon,Tree2[root2].rightSon)&&isomorphism(Tree1[root1].rightSon,Tree2[root2].leftSon);
}
}
int main()
{
int root1,root2;
printf("创建第一棵树:\n");
root1=buildTree(Tree1);
printf("创建第二棵树:\n");
root2=buildTree(Tree2);
if(isomorphism(root1,root2))
{
printf("YES");
}
else
{
printf("NO");
}
return 0;
}