- //二叉树的建立、存储与遍历
- #include <iostream.h>
- struct BintrNode
- {
- char value;
- BintrNode* lf;
- BintrNode* rt;
- };
- void init(BintrNode* &p)
- {
- char ch;
- cin>>ch;
- if(ch!='!')
- {
- p=new BintrNode;
- p->value=ch;
- init(p->lf);
- init(p->rt);
- }
- else
- {
- p=NULL;
- }
- }
- void pre(BintrNode* p)
- {
- if(p)
- {
- cout<<p->value;
- pre(p->lf);
- pre(p->rt);
- }
- }
- void ino(BintrNode* p)
- {
- if(p)
- {
- ino(p->lf);
- cout<<p->value;
- ino(p->rt);
- }
- }
- void pro(BintrNode* p)
- {
- if(p)
- {
- pro(p->lf);
- pro(p->rt);
- cout<<p->value;
- }
- }
- void main()
- {
- BintrNode* bt;
- init(bt);
- pre(bt);
- cout<<endl;
- ino(bt);
- cout<<endl;
- pro(bt);
- cout<<endl;
- }
- //该片段来自于http://www.codesnippet.cn/detail/04122012806.html
来源: http://www.codesnippet.cn/detail/04122012806.html