#include <stdio.h>
#include <stdlib.h>
#define maxsize 1024
typedef char datatype;
typedef struct node {
datatype data;
struct node *lchild, *rchild;
} bitree;
bitree *CreaTree() {
char ch;
bitree *q[maxsize];
int front, rear;
bitree *root, *s;
root = NULL;
front = 1;
rear = 0;
while ((ch = getchar()) != '#') {
s = NULL;
if (ch != '@') {
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 && q[front])
if (rear % 2 == 0)
q[front]->lchild = s;
else
q[front]->rchild = s;
if (rear % 2 == 1)
front++;
}
}
return root;
}
int main() {
bitree *tPtr;
printf("层次输入法创建二叉链表\n");
printf("按完全二叉树结点编号规则顺序输入结点值,空结点为@,#为结束符。\n");
printf("如输入 ABCDE@F@@@G@@H@# (教材第131页图6.10中的二叉树,层次输入结点)。\n");
tPtr = CreaTree();
return 0;
}