我用递归方法来求树的节点树,叶子数以及有两个孩子的节点数,但每次运行输入数据后都是Tempfile.exe.停止工作,怎么回事啊。
#include
#include
typedef struct bt
{
char data;
struct bt *Lchild,*Rchild;
}bt;
bt* Creattree(bt *t)
{char ch;
ch=getchar();
getchar();
if(ch=='.')
t=NULL;
else
{t=(bt*)malloc(sizeof(bt));
t->data=ch;
t->Lchild=Creattree(t->Lchild);
t->Rchild=Creattree(t->Rchild);
}
return t;
}
int countnode(bt *t)
{
int count=0;
if(t=NULL)
return 0;
else
{
count++;
count=count+countnode(t->Lchild);
count=count+countnode(t->Rchild);
return count;
}
}
int countleaves(bt *t)
{
int count=0;
if(t=NULL)
return 0;
if(t->Lchild==NULL && t->Rchild==NULL)
return 1;
else
{
count+=countleaves(t->Lchild);
count+=countleaves(t->Rchild);
return count;
}
}
int countTnode(bt *t)
{
int count=0;
if(t=NULL)
return 0;
if(t->Lchild!=NULL && t->Rchild!=NULL)
count=1;
count+=countTnode(t->Lchild);
count+=countTnode(t->Rchild);
return count;
}
void main()
{
bt *t=NULL;
int a, b, c;
printf("建立二叉树:");
t=Creattree(t);
a=countnode(t);
printf("节点数:%d\n",a);
b=countleaves(t);
printf("叶子数:%d\n",b);
c=countTnode(t);
printf("有两个孩子的节点数:%d\n",c);
}
|