二叉树-----二叉链表-----遍历(7种)
[cpp]//file:BTtree.h#ifndef _BTTREE_H_HUMING_INCLUDE_#define _BTTREE_H_HUMING_INCLUDE_#include<iostream>#include<queue>#include<stack>#define maxlen 100using namespace std;template <class T>class treenode{public:treenode():lchild(NULL),rchild(NULL) {};treenode(const T &v,treenode<T> *left,treenode<T> *right):data(v),lchild(left),rchild(right) {};~treenode(){delete lchild;delete rchild;}treenode<T> *lchild,*rchild;T data;};template <class T>class BTtree{public:BTtree():root(NULL) {};~BTtree();void pre_create();bool Isempty();treenode<T>* Lchild(treenode<T>* t);treenode<T>* Rchild(treenode<T>* t);T element(treenode<T>* t);treenode<T>* return_root();void pre_order(treenode<T>* t);void in_order(treenode<T>* t);void post_order(treenode<T>* t);void nrec_pre_order(treenode<T>* t);void nrec_in_order(treenode<T>* t);void nrec_post_order(treenode<T>* t);void level_order(treenode<T>* t);private:treenode<T> *root;void clear(treenode<T>* t);treenode<T>*insert();};template<class T>BTtree<T>::~BTtree(){clear(root);delete root;}template <class T>void BTtree<T>::clear(treenode<T>* t){if(t==NULL) return;else{clear(t->lchild);clear(t->rchild);t=NULL;}}template <class T>void BTtree<T>::pre_create(){root=insert();}template<class T>treenode<T>* BTtree<T>::insert(){T ch;treenode<T> *t;cin >> ch;if(ch=='#') t=NULL;else{t=new treenode<T>;t->data=ch;t->lchild=insert();t->rchild=insert();}return t;}template<class T>bool BTtree<T>::Isempty(){return root?false:true;}template <class T>treenode<T>* BTtree<T>::Lchild(treenode<T>* t){return t->lchild?t->lchild:NULL;}template <class T>treenode<T>* BTtree<T>::Rchild(treenode<T>* t){return t->rchild?t->rchild:NULL;}template <class T>T BTtree<T>::element(treenode<T>* t){return t->data;;}template <class T>void BTtree<T>::pre_order(treenode<T>* t){if(t!=NULL){cout << t->data;pre_order(t->lchild);pre_order(t->rchild);}}template <class T>treenode<T>* BTtree<T>::return_root(){return root;}template <class T>void BTtree<T>::in_order(treenode<T>* t){if(t!=NULL){in_order(t->lchild);cout << t->data;in_order(t->rchild);}}template <class T>void BTtree<T>::post_order(treenode<T>* t){if(t!=NULL){post_order(t->lchild);post_order(t->rchild);cout << t->data;}}template <class T>void BTtree<T>::nrec_pre_order(treenode<T>* t){stack<treenode<T>*> s;while(t!=NULL||!s.empty()){while(t!=NULL){cout << t->data;s.push(t);t=t->lchild;}if(!s.empty()){t=s.top();s.pop();t=t->rchild;}}}template <class T>void BTtree<T>::nrec_in_order(treenode<T>* t){stack<treenode<T>*> s;while(t!=NULL||!s.empty()){ &补充:软件开发 , C++ ,
上一个:利用模板编译期计算阶乘
下一个:浮点数加法 九度oj
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊