【求助】c的二叉树带括号输出
这个是要求,它的创建都写好了,就它带括号和逗号输出那里,空的地方代表0结点。我还没想出来,希望各位帮帮手
利用链式存储结构建立二叉树。
要求:输入一串数字,0代表虚节点,-1代表输入结束
输出:以如下的形式输出,如1(2(,4),3(,5))
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef struct node
{
int data;
struct node *lchild,*rchild;
} bitree;
bitree *Q[maxsize];
bitree *Creatree()
{
int front,rear;
int ch;
bitree *s,*root;
s = NULL;
root = NULL;
front = 1;
rear = 0;
scanf("%d",&ch);
while(ch!=-1)
{
s=NULL;
if(ch!=0)
{
s = (bitree*)malloc(sizeof(bitree));
s->data = ch;
s->lchild = NULL;
s->rchild = NULL;
}
rear ++;
Q[rear] = s;
if(rear == 1)
root = s;
else
{
if(s!=NULL&&Q[front]!=NULL)
if(rear%2== 0)
Q[front]->lchild = s;
else
Q[front]->rchild = s;
if(rear%2==1)
front ++;
}
scanf("%d",&ch);
}
return root;
}
int main()
{
bitree *s;
s=Creatree();
return 0;
}