编写程序实现二叉树的创建,三种遍历自己销毁
1.节点创建
Btree create_node()
{
Btree t=(Btree)malloc(sizeof(struct Node));
if(NULL ==t)
return NULL;
//新节点t的数据域
t->data=0; //--?t->data='\0';
//t的指针域
t->lchild=t->rchild=NULL;
return t;
}
2.先序遍历
void first_output(Btree tree)
{
if(NULL == tree)
return ;
printf("%c",tree->data);//根
first_output(tree->lchild);//递归左孩子
first_output(tree->rchild);//递归右孩子
}
3.中序遍历
void mid_output(Btree tree)
{
if(NULL ==tree)
return;
//递归左孩子
mid_output(tree->lchild);
printf("%c",tree->data);
//递归右孩子
mid_output(tree->rchild);
}
3.后序遍历
void last_output(Btree tree)
{
if(NULL ==tree)
return;
last_output(tree->lchild);
last_output(tree->rchild);
printf("%c",tree->data);
}
4.销毁
Btree free_space(Btree tree)
{
if(NULL ==tree)
return NULL;
free_space(tree->lchild);
free_space(tree->rchild);
free(tree);tree=NULL;
}