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