程序不正确,请大哥看看哪里出了问题?
#include<stdio.h>
#define maxsize 10
typedef struct node
{
char element;
int left;
int right;
}tree;
int issame(tree firsttree[], int root1, tree sectree[], int root2);
int buildtree(tree trec[]);//纯粹建树
int main(){
int r1, r2, ret, i;//静态链表所构成树的根节点
tree t1[maxsize], t2[maxsize];//一个数组用来记录这个树
r1 = buildtree(t1);//构造这棵树,并返回根
r2 = buildtree(t2);
if(issame(t1, r1, t2, r2))
printf("同构\n");
else
printf("不同\n");
return 0;
}
int buildtree(tree trec[])//用于构造一棵树,
并且把树根的位置return
{
int N, i,;
char ele, l, r;
printf("输入节点数量\n");
scanf("%d", &N);
fflush(stdin);
int check[maxsize] = {0};//记录数组,最后遍历以找到根节点
if(N)
{
for(i=0; i<N; i++)
check[i] = 0;
for(i=0; i<N; i++)
{
printf("请输入第%d,amount to %d times\n", i+1, N);
scanf("%c %d %d", &ele, &l, &r);
fflush(stdin);
trec[i].element = ele;//接受本节点字母
if(l!='-')
{
trec[i].left = l-'0';
check[trec[i].left] = 1;
}
else
trec[i].left = -1;
if(r!='-')
{
trec[i].right = r-'0';
check[trec[i].right] = 1;
}
else
trec[i].right = -1;
}
}
for(i=0; i<N; i++)
if(check[i] == 0)
break;
return i;
}
int issame(tree firsttree[], int root1, tree sectree[], int root2)
{
if ((firsttree[root1].element == -1)&&(sectree[root2].element == -1))//左右根节点都不存在
return 1;
if(((firsttree[root1].element == -1)&&(sectree[root2].element != -1))||((firsttree[root1].element != -1)&&(sectree[root2].element == -1)))//一个有根一个没有
return 0;
if(firsttree[root1].element != sectree[root2].element)//根节点元素不同
return 0;
if(firsttree[root1].left == -1 && sectree[root1].left == -1) //都没有左孩子 判断右孩子
return (issame(firsttree, firsttree[root1].right, sectree, sectree[root2].right));
if((firsttree[root1].left != -1)&&(sectree[root2].left != -1)&&(firsttree[firsttree[root1].left].element == sectree[sectree[root2].left].element))
//两棵树的左孩子相同,那么就意味着不需要互换,则把左孩子和右孩子继续递归判定
return ((issame(firsttree, firsttree[root1].left, sectree, sectree[root2].left))&&(issame(firsttree, firsttree[root1].right, sectree, sectree[root2].right)));
else//左右孩子相同 不需要互换
return (issame(firsttree, firsttree[root1].left, sectree, sectree[root2].right)&&issame(firsttree, firsttree[root1].right, sectree, sectree[root2].left));
}
https://blog.csdn.net/yin__ren/article/details/78992175