#include
#include
typedef char elemtype;
typedef struct node
{
struct node *lchild;
struct node *rchild;
elemtype data;
} btnode;
void createbt(btnode *&b,char *str)
{
btnode *p,*st[100];
int j=0,k,top=-1;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case '(':top++;st[top]=p;k=1;break;
case ')':top--;break;
case ',':k=2;break;
default:
p=(btnode *)malloc(sizeof(btnode));
p->data=ch;
p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;
else
{
switch(k)
{
case 1:st[top]->lchild=p;break;
case 2:st[top]->rchild=p;break;
}
}
}
j++;ch=str[j];
}
}
void displeaf(btnode *b)
{
if(b!=NULL)
{
if(b->lchild==NULL&&b->rchild==NULL)
printf("%c",b->data);
displeaf(b->lchild);
displeaf(b->rchild);
}
}
int main()
{
btnode *b;
char str[50];
printf("输入二叉树: ");
scanf("%s",str);
createbt(b,str);
printf("叶子结点: ");
displeaf(b);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)