二叉树
#include<deque> #include<cmath> #include<vector> #include<iostream> using namespace std; template<class T> struct BiNode { T data; BiNode<T>* lchild; BiNode<T>* rchild; }; template<class T> class BiTree { private: void Create(BiNode<T>* &R, T data[], int i, int n); void Release(BiNode<T>* R); public: BiNode<T> *root; BiTree(T data[], int n); void PreOrder(BiNode<T> *R);//就当是为了可以从某个结点开始遍历而设计的接口 void InOrder(BiNode<T> *R); void PostOrder(BiNode<T> *R); void LevelOrder(BiNode<T> *R); int getAmountOfNodes(BiNode<T>* R); int getDepth(BiNode<T> *R); void findPath(BiNode<T> *R, T value, vector<T> &v); ~BiTree(); int depth = 0; int amountOfNodes = 0; }; template<class T> void BiTree<T>::Create(BiNode<T>*&R, T data[], int i, int n) { if (i <= n && data[i - 1]) { R = new BiNode<T>; R->data = data[i - 1]; Create(R->lchild, data, 2 * i, n); Create(R->rchild, data, 2 * i + 1, n); } else R = NULL; } template<class T> BiTree<T>::BiTree(T data[], int n) { Create(root, data, 1, n); amountOfNodes = getAmountOfNodes(root); depth = getDepth(root); } template<class T> void BiTree<T>::PreOrder(BiNode<T> *R) { if (R) { cout << R->data<<" "; PreOrder(R->lchild); PreOrder(R->rchild); } } template<class T> void BiTree<T>::InOrder(BiNode<T> *R) { if (R) { InOrder(R->lchild); cout << R->data<<" "; InOrder(R->rchild); } } template<class T> void BiTree<T>::PostOrder(BiNode<T> *R) { if (R) { PostOrder(R->lchild); PostOrder(R->rchild); cout << R->data<<" "; } } template<class T>//对某一层的结点访问完毕后,再按照它们的访问次序对各个结点的左孩子和右孩子顺序访问 void BiTree<T>::LevelOrder(BiNode<T> *R) { deque<BiNode<T>*> q; q.push_back(R); while (q.front()!=NULL) {//用指针会搞进来空指针,所以要判断是否为空 BiNode<T>* temp = q.front(); q.pop_front(); cout << temp->data << " "; q.push_back(temp->lchild); q.push_back(temp->rchild); } } template<class T> void BiTree<T>::Release(BiNode<T> * R) { if(R) { Release(R->lchild);//不存在则返回上一级调用栈 Release(R->rchild); delete R; } } template<class T> BiTree<T>::~BiTree(){ Release(root); } template<class T> int BiTree<T>::getAmountOfNodes(BiNode<T>* R) { if (!R) return 0; int m = getAmountOfNodes(R->lchild); int n = getAmountOfNodes(R->rchild); return m + n + 1; } template<class T> int BiTree<T>::getDepth(BiNode<T>* R) { if (R == NULL) return 0; int m = getDepth(R->lchild); int n = getDepth(R->rchild); return ( m>n?m:n )+1; } template<class T> void BiTree<T>::findPath(BiNode<T> *R , T value , vector<T> &v) { if (R == NULL) return; v.push_back(R->data); if (R->data == value) { for (int i = 0; i < v.size()-1; i++) cout<<(v[i])<<"->"; cout << v[v.size() - 1]; return; } findPath(R->lchild, value, v); findPath(R->rchild, value, v); v.pop_back(); }
测试函数
#include"tree.h" int main() { int a[11] = {1,2,3,4,5,6,7,8,9,10,11}; vector<int> ivec; BiTree<int> tree(a, 11); tree.PreOrder(tree.root); cout << endl; tree.InOrder(tree.root); cout << endl; tree.PostOrder(tree.root); cout << endl; tree.LevelOrder(tree.root); cout << endl; cout << tree.depth << endl; cout <<tree.amountOfNodes<<endl; tree.findPath(tree.root,6,ivec); cout << endl; return 0; }
如有不足,恳请指正。