#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
int max_min=0;
typedef struct BiTNode{
int data;
struct BiTNode * lchild;
struct BiTNode * rchild;
}BiTNode,*BiTree;
int visit_output(BiTree T);
int pre_order_traverse_visit(BiTree T,int (* visit)(BiTree));
int max_num(int,int);
int min_num(int,int);
pre_order_traverse_comparison(BiTree ,int (* visit)(int,int));
int main(){
//first, we build a tree, without head-------------
BiTree a,b,c,d,e,f,g,h,i,j,k;
a=(BiTree)malloc(sizeof(BiTNode));
b=(BiTree)malloc(sizeof(BiTNode));
c=(BiTree)malloc(sizeof(BiTNode));
d=(BiTree)malloc(sizeof(BiTNode));
e=(BiTree)malloc(sizeof(BiTNode));
f=(BiTree)malloc(sizeof(BiTNode));
g=(BiTree)malloc(sizeof(BiTNode));
h=(BiTree)malloc(sizeof(BiTNode));
i=(BiTree)malloc(sizeof(BiTNode));
j=(BiTree)malloc(sizeof(BiTNode));
k=(BiTree)malloc(sizeof(BiTNode));
a->lchild=b;a->rchild=c;a->data=1;
b->lchild=d;b->rchild=e;b->data=2;
c->lchild=f;c->rchild=g;c->data=3;
d->lchild=NULL;d->rchild=NULL;d->data=4;
e->lchild=h;e->rchild=i;e->data=5;
f->lchild=NULL;f->rchild=NULL;f->data=6;
g->lchild=NULL;g->rchild=NULL;g->data=7;
h->lchild=NULL;h->rchild=NULL;h->data=8;
i->lchild=j;i->rchild=k;i->data=9;
j->lchild=NULL;j->rchild=NULL;j->data=10;
k->lchild=NULL;k->rchild=NULL;k->data=11;
/*
pre_order_traverse_print_version1(a);//the output should be 1 2 4 5 8 9 10 11 3 6 7
printf("\n");
in_order_traverse_print_version1(a);// the output should be 4 2 8 5 10 9 11 1 6 3 7
printf("\n");
last_order_traverse_print_version1(a);// the output should be 4 8 10 11 9 5 2 6 7 3 1
*/
// pre_order_traverse_visit(a,visit_output);
//---------the max number in the tree--
/* max_min=-9999;
pre_order_traverse_comparison(a,max_num);
printf("%d\n",max_min);*/
//--------the min number in the tree--
max_min=99999;
pre_order_traverse_comparison(a,min_num);
printf("%d\n",max_min);
return OK;
}
//---------this print the elements in the tree------------
int pre_order_traverse_print_version1(BiTree T){
if(T !=NULL ){
printf("%d ",T->data);
pre_order_traverse_print_version1(T->lchild);
pre_order_traverse_print_version1(T->rchild);
}
return OK;
}
int in_order_traverse_print_version1(BiTree T){
if( T!=NULL ){
in_order_traverse_print_version1(T->lchild);
printf("%d ",T->data);
in_order_traverse_print_version1(T->rchild);
}
return OK;
}
int last_order_traverse_print_version1(BiTree T){
if(T!=NULL){
last_order_traverse_print_version1(T->lchild);
last_order_traverse_print_version1(T->rchild);
printf("%d ",T->data);
}
return OK;
}
//-----------version2 uses external function------------
//注意这儿如何使用指向函数的指针作为参数
int pre_order_traverse_visit(BiTree T,int (* visit)(BiTree)){
if(T !=NULL ){
visit(T);
pre_order_traverse_visit(T->lchild,visit);
pre_order_traverse_visit(T->rchild,visit);
}
return OK;
}
int visit_output(BiTree T){
printf("%d ",T->data);
return OK;
}
//---------this print the elements in the tree------------
//------------求最大值,最小值-----------------------------
int pre_order_traverse_comparison(BiTree T,int (* visit)(int,int)){
if(T !=NULL ){
max_min=visit(max_min,T->data);
pre_order_traverse_comparison(T->lchild,visit);
pre_order_traverse_comparison(T->rchild,visit);
}
return OK;
}
int max_num(int a,int b){
return a > b? a:b;
}
int min_num(int a,int b){
return a< b ? a:b;
}
先序、中序、后序遍历二叉树 算法
最新推荐文章于 2022-12-19 13:59:12 发布