typedef struct node
{char data;struct node *lchild; //指向左子树的指针struct node *rchild; //指向右子树的指针
}node,*tree_p;
#include "btree.h"
//1、创建结点
tree_p create_node(char data)
{tree_p new = (tree_p)malloc(sizeof(node));if(new==NULL){return NULL;}new->data = data;return new;
}
//2、创建二叉树,二叉树结点的数据由终端输入完成,当输入#表示无需创建结点
tree_p create_tree()
{char data = '\0'; //用于接收终端输入的结果,初始值不能是'#'scanf(" %c",&data);if(data=='#'){return NULL;}tree_p T = create_node(data); //给数据申请结点T->lchild = create_tree(); //根节点的左子树仍然是一颗二叉树T->rchild = create_tree(); return T;
}
//3、先序遍历
void pre_show(tree_p T)
{if(T==NULL){return;}//根左右printf("%c\n",T->data); //输出根节点的数据pre_show(T->lchild); //先序遍历左子树pre_show(T->rchild); //先序遍历右子树
}
//4、中序遍历
void mid_show(tree_p T)
{if(T==NULL){return;}//左根右mid_show(T->lchild);printf("%c\n",T->data);mid_show(T->rchild);
}
//5、后序遍历
void last_show(tree_p T)
{if(T==NULL){return;}//左右根last_show(T->lchild);last_show(T->rchild);printf("%c\n",T->data);
}