#include<iostream> #include<stack> #include<queue> #include<stdlib.h> #include<stdio.h> #include<malloc.h> #include<string.h> using namespace std; typedef struct Node { char data; struct Node *LChild; struct Node *RChild; }BinTNode,*BinTree; void CreateBinTree(BinTree &bt); void PreOrder(BinTree bt); void InOrder(BinTree bt); void PostOrder(BinTree bt); //结构体初始化 /*void init(BinTree &bt) { bt=new BinTNode; bt->LChild=NULL; bt->RChild=NULL; }*/ int main() { BinTree bt; //init(bt); cout<<"二叉树序列:" ; CreateBinTree(bt); cout<<"先序序列:"; PreOrder(bt); cout<<endl; cout<<"中序序列:"; InOrder(bt); cout<<endl; cout<<"后续序列:"; PostOrder(bt); cout<<endl; } //建立二叉树 void CreateBinTree(BinTree &bt) { char str; cin>>str; if(str=='#') { bt=NULL;} else { bt=new BinTNode; bt->data=str; CreateBinTree(bt->LChild); CreateBinTree(bt->RChild); } } //先序遍历二叉树 void PreOrder(BinTree bt) { if(bt!=NULL) { cout<<bt->data<<' '; PreOrder(bt->LChild); PreOrder(bt->RChild); } } //中序遍历二叉树 void InOrder(BinTree bt) { if(bt!=NULL) { InOrder(bt->LChild); cout<<bt->data<<' '; InOrder(bt->RChild); } } //后序遍历二叉树 void PostOrder(BinTree bt) { if(bt!=NULL) { PostOrder(bt->LChild); PostOrder(bt->RChild); cout<<bt->data<<' '; } }