# include<math.h>
# include<iostream>
# include<stdio.h>
using namespace std;
int num[30] = {1,2,4,0,0,5,0,0,3,6,0,0,7,0,0};
int i = 0;
typedef int Elemtype;
typedef struct BiTNode{
Elemtype data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void visit(BiTree &T){
printf("%d ",T->data);
}
void preOrder(BiTree &T){
if(T!=NULL){
visit(T);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
void inOrder(BiTree &T){
if(T!=NULL){
inOrder(T->lchild);
visit(T);
inOrder(T->rchild);
}
}
void postOrder(BiTree &T){
if(T!=NULL){
postOrder(T->lchild);
postOrder(T->rchild);
visit(T);
}
}
void buildTree(BiTree &T){
int el;
el = num[i];i++;
if(el==0)
T=NULL;
else{
T = (BiTNode *)malloc(sizeof(BiTNode));
T->data = el;
buildTree(T->lchild);
buildTree(T->rchild);
}
}
int main(void){
BiTree T;
T = NULL;
buildTree(T);
preOrder(T);
printf("\n");
inOrder(T);
printf("\n");
postOrder(T);
printf("\n");
return 0;
}
|