#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct BiNode
{
char *s;
struct BiNode *lchild;
struct BiNode *rchild;
}BiNode,*BiTree;
void PreOrder(BiTree T)
{
if(T)
{
printf("%s\n",T->s);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
int main()
{
char **str,e;
int row=1,col=1;
int tag,i,len;
BiTree T,r,t;
str=(char **)malloc(row*sizeof(char *));
str[col-1]=(char *)malloc(col*sizeof(char));
tag=1;
while((e=getchar())!='\n')
{
if(e==' ')
{
str[row-1][col-1]='\0';
tag=0;
continue;
}
else
{
if(tag==0)
{
row++;
col=2;
str=(char **)realloc(str,row*sizeof(char *));
str[row-1]=(char *)malloc(col*sizeof(char));
str[row-1][col-2]=e;
tag=1;
}
else
{
col++;
str[row-1]=(char *)realloc(str[row-1],col*sizeof(char));
str[row-1][col-2]=e;
}
}
}
str[row-1][col-1]='\0';
for(i=0;i<row;i++)
printf("%s\n",str[i]);
printf("\n");
len=strlen(str[0]);
T=(BiTree)malloc(sizeof(BiNode));
T->s=(char *)malloc((len+1)*sizeof(char));
strcpy(T->s,str[0]);
T->lchild=T->rchild=NULL;
t=T;
for(i=1;i<row;i++)
{
len=strlen(str[i]);
r=(BiTree)malloc(sizeof(BiNode));
r->s=(char *)malloc((len+1)*sizeof(char));
r->lchild=NULL;
r->rchild=NULL;
strcpy(r->s,str[i]);
while(t)
{
if(strcmp(t->s,r->s)>0)
{
if(t->lchild)
t=t->lchild;
else
{
t->lchild=r;
break;
}
}
else
{
if(t->rchild)
t=t->rchild;
else
{
t->rchild=r;
break;
}
}
}
t=T;
}
PreOrder(T);
return 0;
}
2011_3华中科技大学机试真题
最新推荐文章于 2023-06-24 15:47:33 发布