- 北大“数据结构”上机考试复习题总结(1)
- 发布日期时间:2007-3-16 来源:不详 点击数: 作者:佚名
{
int i;
if(pres>pree||is>ie)
*r=NULL;
else{
*r=malloc(sizeof(Tnode));
for(i=is;i<=ie;i++)
if(pre[pres]==in[i])
{
MKTree(pre,pres+1,pres+i-is,in,is,is+i-1,&(*r)->lchild);
MKTree(pre,pres+i+is+1,pree,in,is+i+1,ie,&(*r)->rchild);
break;
}
}
}
void postorder(Tnode *r)
{
if(r)
{
postorder(r->lchild);
postorder(r->rchild);
printf(“%c”,r->d);
}
}
int num(Tnode *r)
{
if(r==NULL)
return 0;
else
if(r->lchild==NULL&&r->rchild==NULL)
return 1;
else
return num(r->lchild)+num(r->rchild);
}
int height(Tnode *r)
{
int h1,h2;
if(r==NULL)
return 0;
else
{
h1=height(r->lchild);
h2=height(r->rchild);
return 1+(h1>h2)?h1:h2;
}
}
void main()
{
Tnode *r;
char pre[MAX],in[MAX];
printf(“input preorder and inorder \n”);
gets(pre);
gets(in);
MKTree(pre,0,strlen(pre)-1,in,0,strlen(in)-1,&r);
printf(“The postorder is as follow:\n”);
postorder(r);
printf(“\n there are %d leaves in the tree\n”,num(r));
printf(“h=%d\n”,height(r));
}
2.编一C程序,它能读入一串(n个)整数(以-9999为结束标记),并判断第1个整数在后(n-1)个整数中出现的次数,再输出该次数。(输入时,两个相邻的整数用空格隔开)。
(注:程序的可执行文件名必须是 e2.exe,存于你的账号或其debug目录下。)
文章转载请注明来源于:汕头自考网
|
|



