#include <iostream> using namespace std; #include <queue> #include <stack> template<class T> struct BinaryTreeNode { BinaryTreeNode<T>* _left; BinaryTreeNode<T>* _right; T _data; BinaryTreeNode(const T& x) :_left(NULL) ,_right(NULL) ,_data(x) {} }; template<class T> class BinaryTree { public: BinaryTree() :_root(NULL) {} BinaryTree(const T* a, size_t size, const T& invalid) { size_t index = 0; _root = _CreateTree(a, size, index, invalid); } ~BinaryTree() { _Destroy(_root); _root = NULL; } BinaryTree(const BinaryTree<T>& t) { _root = _Copy(t._root); } //赋值运算符重载 BinaryTree<T>& operator=(const BinaryTree& t) { if (this != &t) { _Destroy(_root); _root = _Copy(t._root); } return *this; } //递归前序遍历 void PreOrderTraverse() { _PreOrderTraverse(_root); cout<<endl; } //递归中序遍历 void InOrderTraverse() { _InOrderTraverse(_root); cout<<endl; } //递归后序遍历 void PostOrderTraverse() { _PostOrderTraverse(_root); cout<<endl; } //层序遍历 void LevelOrderTraverse() { queue<BinaryTreeNode<T>*> q; if (_root) { q.push(_root); } while (!q.empty()) { BinaryTreeNode<T>* front = q.front(); q.pop(); cout<<front->_data<<" "; if (front->_left) { q.push(front->_left); } if (front->_right) { q.push(front->_right); } } cout<<endl; } //非递归前序遍历 void PreOrderTraverse_NonR() { stack<BinaryTreeNode<T>*> s; if (_root) { s.push(_root); } while (!s.empty()) { BinaryTreeNode<T>* top = s.top(); s.pop(); cout<<top->_data<<" "; if (top->_right) { s.push(top->_right); } if (top->_left) { s.push(top->_left); } } cout<<endl; } //非递归中序遍历 void InOrderTraverse_NonR() { stack<BinaryTreeNode<T>*> s; BinaryTreeNode<T>* cur = _root; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->_left; } if (!s.empty()) { BinaryTreeNode<T>* top = s.top(); cout<<top->_data<<" "; s.pop(); cur = top->_right; } } cout<<endl; } //非递归后序遍历 void PostOrderTraverse_NonR() { stack<BinaryTreeNode<T>*> s; BinaryTreeNode<T>* cur = _root; BinaryTreeNode<T>* preVisted = NULL; while (cur || !s.empty()) { while (cur) { s.push(cur); cur = cur->_left; } BinaryTreeNode<T>* top = s.top(); if (NULL == top->_right || preVisted == top->_right) { cout<<top->_data<<" "; preVisted = top; s.pop(); } else { cur = top->_right; } } cout<<endl; } //结点数 size_t Size() { return _Size(_root); } //深度 size_t Depth() { return _Depth(_root); } //叶子结点数 size_t LeafSize() { size_t size = 0; _GetLeafSize(_root, size); return size; } protected: //构造二叉树 BinaryTreeNode<T>* _CreateTree(const T* a, size_t size, size_t& index, const T& invalid) { BinaryTreeNode<T>* root = NULL; if (a[index] != invalid && index < size) { root = new BinaryTreeNode<T>(a[index]); root->_left = _CreateTree(a, size, ++index, invalid); root->_right = _CreateTree(a, size, ++index, invalid); } return root; } //销毁 void _Destroy(BinaryTreeNode<T>*& root) { if (NULL == root) { return; } if (NULL == root->_left && NULL == root->_right) { delete root; root = NULL; return; } _Destroy(root->_left); _Destroy(root->_right); delete root; } //拷贝 BinaryTreeNode<T>* _Copy(BinaryTreeNode<T>* root) { if (NULL == root) { return NULL; } BinaryTreeNode<T>* newRoot = new BinaryTreeNode<T>(root->_data); newRoot->_left = _Copy(root->_left); newRoot->_right = _Copy(root->_right); return newRoot; } //递归前序遍历 void _PreOrderTraverse(BinaryTreeNode<T>* root) { if (NULL == root) { return; } cout<<root->_data<<" "; _PreOrderTraverse(root->_left); _PreOrderTraverse(root->_right); } //递归中序遍历 void _InOrderTraverse(BinaryTreeNode<T>* root) { if (NULL == root) { return; } _InOrderTraverse(root->_left); cout<<root->_data<<" "; _InOrderTraverse(root->_right); } //递归后序遍历 void _PostOrderTraverse(BinaryTreeNode<T>* root) { if (NULL == root) { return; } _PostOrderTraverse(root->_left); _PostOrderTraverse(root->_right); cout<<root->_data<<" "; } //结点数 size_t _Size(BinaryTreeNode<T>* root) { if (root == NULL) { return 0; } return _Size(root->_left) + _Size(root->_right) + 1; } //深度 size_t _Depth(BinaryTreeNode<T>* root) { if (root == NULL) { return 0; } size_t leftDepth = _Depth(root->_left); size_t rightDepth = _Depth(root->_right); return leftDepth < rightDepth ? leftDepth+1 : rightDepth+1; } //叶子结点数 void _GetLeafSize(BinaryTreeNode<T>* root, size_t& size) { if (NULL == root) { return; } if (NULL == root->_left && NULL == root->_right) { ++size; return; } _GetLeafSize(root->_left, size); _GetLeafSize(root->_right, size); } protected: BinaryTreeNode<T>* _root; }; void Test() { int a[] = {1, 2, 3, ‘#‘, ‘#‘, 4, ‘#‘, ‘#‘, 5, 6}; BinaryTree<int> t(a, sizeof(a)/sizeof(a[0]), ‘#‘); cout<<"前序遍历:"; t.PreOrderTraverse(); cout<<"中序遍历:"; t.InOrderTraverse(); cout<<"后序遍历:"; t.PostOrderTraverse(); cout<<"层序遍历:"; t.LevelOrderTraverse(); cout<<"非递归前序遍历:"; t.PreOrderTraverse_NonR(); cout<<"非递归中序遍历:"; t.InOrderTraverse_NonR(); cout<<"非递归后序遍历:"; t.PostOrderTraverse_NonR(); cout<<"Size = "<<t.Size()<<endl; cout<<"Depth = "<<t.Depth()<<endl; cout<<"LeafSize = "<<t.LeafSize()<<endl; BinaryTree<int> t2(t); cout<<"t2:"; t2.PreOrderTraverse(); BinaryTree<int> t3; t3 = t2; cout<<"t3:"; t3.PreOrderTraverse(); } int main() { Test(); return 0; }
时间: 2024-11-09 02:10:32