#include<stdio.h>
#include<stdlib.h>
#define flag int
#define L 0
#define R 1
typedef struct tree{
struct tree *lchild;
int data;
struct tree *rchild;
} TREE;
TREE * Init_tree(int data)
{
TREE *root;
root=(TREE *)malloc( sizeof(TREE));
root->data=data;
root->lchild=NULL;
root->rchild=NULL;
return root;
}
void Destroy_tree(TREE *root)
{
TREE *lchild, *rchild;
lchild=root->lchild;
rchild=root->rchild;
if(root)
{
free(root);
Destroy_tree(lchild);
Destroy_tree(rchild);
}
}
void insert(TREE *T,int data,flag LR) //L=0,R=1
{
TREE *p=(TREE *)malloc( sizeof(TREE));
p->data=data;
p->lchild=NULL;
p->rchild=NULL;
if(LR==0)
T->lchild=p;
else if(LR==1)
T->rchild=p;
else
{
printf("Error/n");
exit(0);
}
}
TREE * function(void)
{
TREE *root;
root=Init_tree(1);
insert(root,2,L);
insert(root,3,R);
insert(root->lchild,4,L);
insert(root->lchild,5,R);
insert(root->rchild,6,L);
insert(root->rchild,7,R);
insert(root->lchild->lchild,8,L);
insert(root->lchild->lchild,9,R);
insert(root->rchild->lchild,10,L);
insert(root->rchild->rchild,11,R);
return root;
}
TREE * Creat_tree(TREE * (*recall)(void) )
{
TREE *root=recall();
return root;
}
void visit(int num)
{
printf("%d/t",num);
}
void xian_xu(TREE *root,void (*myvisit)(int))
{
if(root)
{
myvisit(root->data);
xian_xu(root->lchild,myvisit);
xian_xu(root->rchild,myvisit);
}
}
void zhong_xu(TREE *root,void (*myvisit)(int))
{
if(root)
{
zhong_xu(root->lchild,myvisit);
myvisit(root->data);
zhong_xu(root->rchild,myvisit);
}
}
void hou_xu(TREE *root,void (*myvisit)(int))
{
if(root)
{
hou_xu(root->lchild,myvisit);
hou_xu(root->rchild,myvisit);
myvisit(root->data);
}
}
int main()
{
TREE *mytree=NULL;
mytree=Creat_tree(function);
hou_xu(mytree,visit);
return 0;
}