#include<cstdio> #include<cstring> struct BiNode { char data; BiNode *lchild,*rchild; }; class BiTree { public: BiTree(){root=NULL;} BiTree(BiNode *&root); // ~BiTree(); void PreOrder(BiNode *&root); void InOrder(BiNode *&root); void PostOrder(BiNode *&root); void Release(BiNode *&root); private: BiNode *root; void Creat(BiNode *&root); }; BiTree::BiTree(BiNode *&root) { Creat(root); } void BiTree::Creat(BiNode *&root) { char ch; scanf("%c",&ch); if(ch=='#') root=NULL; else { root=new BiNode; root->data=ch; Creat(root->lchild); Creat(root->rchild); } } void BiTree::PreOrder(BiNode *&root) { if(root==NULL) return; else { printf("%C",root->data); PreOrder(root->lchild); PreOrder(root->rchild); } } void BiTree::InOrder(BiNode *&root) { if(root==NULL) return; else { InOrder(root->lchild); printf("%c",root->data); InOrder(root->rchild); } } void BiTree::PostOrder(BiNode *&root) { if(root==NULL) return; else { PostOrder(root->lchild); PostOrder(root->rchild); printf("%c",root->data); } } void BiTree::Release(BiNode *&root) { if(root!=NULL) { Release(root->lchild); Release(root->rchild); delete root; } } void main() { BiNode *root=NULL; BiTree tree(root); printf("前序遍历: "); tree.PreOrder(root); printf("/n"); printf("中序遍历: "); tree.InOrder(root); printf("/n"); printf("后序遍历: "); tree.PostOrder(root); printf("/n"); tree.Release(root); }