#include"stdio.h"
typedef struct tree
{
char element;
struct tree leftchild, *rightchild;
}Tree;
Tree malloc2(void)//为新节点申请动态空间
{
Tree* p;
p = (Tree*)malloc(sizeof(Tree));
/*if (IS_FULL(p))
{
fprintf(stderr, "The memery is full\n");
exit(1);
}*/
return p;
}
void linkchild(Tree Bt, char element, Tree *lchild, Tree *rchild)//给树连接孩子
{
Tree p = malloc2();//为p申请动态空间
p->leftchild = lchild;
p->rightchild = rchild;
p->element = element;
lchild = rchild = NULL;
Bt = p;
}
void BreakBTree(Tree Bt, char *element, Tree *lchild, Tree *rchild)//释放Bt的空间
{
if (Bt)
{
*element = Bt->element;
lchild = Bt->leftchild;
rchild = Bt->rightchild;
Bt = NULL;
free(Bt);
}
}
void visit(Tree Bt) //打印节点元素
{
if (Bt)
printf("%c", Bt->element);
}
void Fvisit(Tree *Bt) //先序遍历
{
if (Bt)
{
visit(Bt);
Fvisit(Bt->leftchild);
Fvisit(Bt->rightchild);
}
}
void Mvisit(Tree *Bt) //中序遍历
{
if (Bt)
{
Mvisit(Bt->leftchild);
visit(Bt);
Mvisit(Bt->rightchild);
}
}
void Lvisit(Tree *Bt) //中序遍历
{
if (Bt)
{
Lvisit(Bt->leftchild);
Lvisit(Bt);
Lvisit(Bt->rightchild);
}
}
void main()
{
Tree a, x, y, z;
linkchild(&x, 'E', &a, &a);
linkchild(&y, 'F', &a, &a);
linkchild(&z, 'C', &x, &y);
linkchild(&y, 'D', &a, &a);
linkchild(&x, 'B', &y, &z);
Fvisit(&x);
while (1)
{
}
}