数据结构二叉树
#include<stdio.h>#include<malloc.h>
#include<string.h>
#define n 10
#define null 0
typedef struct node2
{
char data;
struct node2 *lchild;
struct node2 *rchild;
}btnode;
void disbtnode(btnode *b)
{
if(b!=null)
{
printf("%c",b->data);
if(b->lchild!=null||b->rchild!=null)
{
printf("(");
disbtnode(b->lchild);
if(b->rchild!=null)
printf(",");
disbtnode(b->rchild);
printf(")");
}
}
}
void createbtnode(btnode *&b,char *str)
{
btnode *s[n],*p=null;
int top=-1,tag,j=0;
char ch;
b=null;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case'(':top++;s[top]=p;tag=1;break;
case')':top--;break;
case',':tag=2;break;
default:p=(btnode *)malloc(sizeof(btnode));
p->data=ch;
p->lchild=p->rchild=null;
if(b==null) b=p;
else
{
switch(tag)
{
case 1:s[top]->lchild=p;break;
case 2:s[top]->rchild=p;break;
}
}
}
j++;ch=str[j];
}
}
void main()
{
btnode *b;
b=(btnode *)malloc(sizeof(btnode));
createbtnode(b,"a,c(x,k)");
disbtnode(b);
printf("\n");
}
怎么只输出一个而不是输出一串呢?