#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *lchild;
struct Node *rchild;
} NODE;
void createNode(NODE *node,int data );
void createtree(NODE *tree_header);
void PreOrderTraverse(NODE *tree_header);
void visit(NODE *node);
void MidOrderTraverse(NODE *tree_header);
void BackOrderTraverse(NODE *tree_header);
main()
{
NODE *tree_header=(NODE *)malloc(sizeof(NODE));
createNode(tree_header,0);
createtree(tree_header);
printf("PreOrerTraverse begin:/n");
PreOrderTraverse(tree_header);
printf("**********************/n");
printf("MidOrderTraverse begin:/n");
MidOrderTraverse(tree_header);
printf("**********************/n");
printf("BackOrderTraverse begin:/n");
BackOrderTraverse(tree_header);
printf("**********************/n");
}
/*********************************************
*递归构建任意结构的二叉树,当给一结点的lchild赋值为0时,
*表示该结点为叶子结点,如果一结点没有lchild时,给该结点
*的rchild为NULL。
**********************************************/
void createtree(NODE *tree_header)
{
int data=0;
if(tree_header->data==0){
printf("give the value to the tree root:/n");
scanf("%d",&data);
tree_header->data=data;
}
printf("give the value to the left child:%d(give zero means this node have no child):/n",tree_header->data);
scanf("%d",&data);
if( (data !=0)){
NODE * nodeleft=(NODE *)malloc(sizeof(NODE));
createNode(nodeleft,data);
tree_header->lchild=nodeleft;
printf("give the value to the right child :%d(give zero means this node have no right child):/n",tree_header->data);
scanf("%d",&data);
if(data !=0){
NODE * noderight=(NODE *)malloc(sizeof(NODE));
createNode(noderight,data);
tree_header->rchild=noderight;
}
}
if(tree_header->lchild!=NULL){
createtree(tree_header->lchild);
}
if(tree_header->rchild!=NULL){
createtree(tree_header->rchild);
}
}
/*********************************************
*创建结点
**********************************************/
void createNode(NODE *node,int data )
{
node->data=data;
node->lchild=NULL;
node->rchild=NULL;
}
/*********************************************
*递归前序遍历二叉树
**********************************************/
void PreOrderTraverse(NODE *tree_header)
{
if(tree_header){
visit(tree_header);
if(tree_header->lchild){
PreOrderTraverse(tree_header->lchild);
if(tree_header->rchild){
PreOrderTraverse(tree_header->rchild);
}
}
}
}
/*********************************************
*递归中序遍历二叉树
**********************************************/
void MidOrderTraverse(NODE *tree_header)
{
if(tree_header){
if(tree_header->lchild){
MidOrderTraverse(tree_header->lchild);
}
visit(tree_header);
if(tree_header->rchild){
MidOrderTraverse(tree_header->rchild);
}
}
}
/*********************************************
*递归后序遍历二叉树
**********************************************/
void BackOrderTraverse(NODE *tree_header)
{
if(tree_header){
if(tree_header->lchild){
BackOrderTraverse(tree_header->lchild);
if(tree_header->rchild){
BackOrderTraverse(tree_header->rchild);
}
}
visit(tree_header);
}
}
/*********************************************
*对遍历获取到的结点进行处理,在此为测试仅仅输出结果
**********************************************/
void visit(NODE *node)
{
printf("value=%d/n", node->data);
}