#include "stdio.h"
#include "stdlib.h"
#define max 100
#define null 0
typedef struct node
{ char data;
struct node *lchild,*rchild;
} btree;
btree *Q[max]; //定义队列
btree *CREATREE(){ //非递归建立二叉树
char ch;
int front=1,rear=0; //初始化队列
btree *root,*s;
root=null; //置空队列
printf("'@'表示'空','#'表示'结束'\n");
ch=getchar();
while(ch!='#') //假设结点值为单字符,#为输入结束符号
{ s=null; //先假设读入的为空结点"@"
if(ch!='@')
{ s=(btree *)malloc(sizeof(btree)); //生成新结点
s->data=ch;
s->lchild=null;
s->rchild=null; //新结点赋值
}
rear++; //队尾指针自增
Q[rear]=s; //将新结点地址或空结点地址(NULL)入队
if(rear==1) root=s; //若rear为1,则说明是根结点,用root指向它
else
{ if(s&&Q[front]) //当前结点及其双亲结点都不是空结点
if(rear%2==0) Q[front]->lchild=s; //rear为偶数,新结点应作为左孩子结点
else Q[front]->rchild=s; //rear为奇数,新结点应作为右孩子结点
if(rear%2==1) front++; //rear为奇数,说明两个孩子已经处理完,front指向下一个双亲
}
ch=getchar(); //读出下一个结点的值
} return root;
}
void main()
{
btree *tree;
tree=CREATREE();
}