1 #include <iostream> 2 using namespace std; 3 4 typedef struct node 5 { 6 char data; 7 struct node *lchild; 8 struct node *rchild; 9 }BiTreeNode,*BiTree; 10 11 void createBiTree(BiTree &T) 12 { 13 char c; 14 cin>>c; 15 if(c=='#') T=NULL; 16 else{ 17 T=new BiTreeNode; 18 T->data=c; 19 createBiTree(T->lchild); 20 createBiTree(T->rchild); 21 } 22 } 23 24 void preTraverse(BiTree T) 25 { 26 if(T){ 27 cout<<T->data<<" "; 28 preTraverse(T->lchild); 29 preTraverse(T->rchild); 30 } 31 } 32 33 void midTraverse(BiTree T) 34 { 35 if(T){ 36 midTraverse(T->lchild); 37 cout<<T->data<<" "; 38 midTraverse(T->rchild); 39 } 40 } 41 42 void postTraverse(BiTree T) 43 { 44 if(T){ 45 postTraverse(T->lchild); 46 postTraverse(T->rchild); 47 cout<<T->data<<" "; 48 } 49 } 50 51 int main() 52 { 53 BiTree T; 54 createBiTree(T); 55 cout<<"二叉树构造成功!"<<endl; 56 cout<<"先序遍历二叉树:"<<endl; 57 preTraverse(T); 58 cout<<endl; 59 cout<<"中序遍历二叉树:"<<endl; 60 midTraverse(T); 61 cout<<endl; 62 cout<<"后序遍历二叉树:"<<endl; 63 postTraverse(T); 64 return 0; 65 }