递归建立二叉树
二叉树在数据结构中是很重要的,常规的建树是用一个队列来建,语句比较复杂,我在学习数据结构时编了一个递归建树的算法,语句比较简洁,算法如下:
typedef struct node //二叉树结点的结构
{
char ch;
struct node *lchild, *rchild; 左指针和右指针
}bitree;
bitree * CreateBitree()
{
char ch;
bitree *b;
ch = getchar();
if(ch == '@') //表示该结点为空结点
{
b = NULL;
}
else
{
b = (bitree *)malloc(sizeof(bitree));
b->data = ch;
b->lchild = CreateBitree();
b->rchild = CreateBitree();
}
return b;
}