#include "function.h"
int main() {
BiTree pnew;//用来指向新申请的树结点
BiElemType value;//char型
BiTree tree=NULL;//tree是指向树根的,代表树
ptag_t phead=NULL;//队头
ptag_t ptail=NULL;//队尾
ptag_t listpnew=NULL;
ptag_t pcur;
while(scanf("%c",&value)){
if(value=='\n'){// '\n'不放入树中
break;//读到换行就结束
}
//calloc申请的空间大小是两个参数直接相乘 1*sizeof,并对空间进行初始化,赋值为0
pnew= (BiNode*)calloc(1,sizeof (BiNode));
pnew->data=value;
listpnew=(ptag_t) calloc(1,sizeof (tag));//给队列申请空间
listpnew->p=pnew;
if(NULL==tree){
tree=pnew;//tree指向树的根结点
// phead=listpnew;//第一个结点,既是队头,也是队尾
ptail=listpnew;
pcur=listpnew;//pcur要指向要进入树的父亲元素
}
else{
ptail->next=listpnew;
ptail=listpnew;
}
if(NULL==pcur->p->lChild)
{//把b结点放入树中
pcur->p->lChild=pnew;//pcur->p左孩子为空,结点放入左孩子
} else if(NULL==pcur->p->rChild){
pcur->p->rChild=pnew;//pcur->p右孩子为空,结点放入右孩子
pcur=pcur->next;
}
}
return 0;
}
#ifndef BITREE_FUNCTION_H
#define BITREE_FUNCTION_H
#include <stdio.h>
#include <stdlib.h>
typedef char BiElemType;
typedef struct BiNode{
BiElemType data;//数据域
struct BiNode *lChild;//左孩子
struct BiNode *rChild;//右孩子
}BiNode,*BiTree;
typedef struct tag{
BiTree p;//树的某一个结点的地址值,有左孩子右孩子
struct tag *next;
}tag_t,*ptag_t;
#endif //BITREE_FUNCTION_H