二叉树BinaryTree类模板C++实现(功能较全)

来源:互联网 发布:php上传进度条 编辑:程序博客网 时间:2024/05/21 04:43
#ifndef MYBINARYTREE_H#define MYBINARYTREE_Htemplate <class T>class BinaryTree{protected:struct TNode{T val;TNode* parent;TNode* left;TNode* right;TNode(T t, TNode* p = 0, TNode* l = 0, TNode* r = 0):val(t), parent(p), left(l), right(r) {if (!parent) parent = this;}};private:TNode* _root;public:class TIterator{BinaryTree* tree;TNode* node;public:friend class BinaryTree;TIterator():tree(0), node(0) {}TIterator(const TIterator& it):tree(it.tree), node(it.node) {}TIterator(BinaryTree* t, TNode* n = 0):tree(t), node(n) {}TIterator& operator = (const TIterator& it){tree = it.tree;node = it.node;return *this;}bool operator == (const TIterator& it) {return (tree == it.tree && node == it.node);}bool operator != (const TIterator& it) {return !(*this == it);}TIterator& operator ++ () {node = preorderSuccessor(node);return *this;}TIterator operator ++ (int){TIterator it(*this);node = preorderSuccessor(node);return it;}T& operator *() {return node->val;}TNode* operator ->() {return node;}const T& operator *() const {return node->val;}const TNode* operator ->() const {return node;}bool operator !() {return node == 0;}protected:};BinaryTree():_root(0) {}BinaryTree(const BinaryTree& b){_root = clone(b._root, 0);}BinaryTree(const T& t):_root(new TNode(t)) {_root->parent = _root;}BinaryTree(const T& t, const BinaryTree& l, const BinaryTree& r);~BinaryTree();BinaryTree& operator = (const BinaryTree&);static void clear(TNode*);                //按递归式后序遍历清除static TNode* preorderSuccessor(TNode*);  //按前序遍历返回下一节点static TNode* clone(TNode*, TNode*);      //按递归式前序遍历复制TIterator begin();TIterator end();bool empty() const;int size() const;             //返回树大小static int getSize(TNode*);   //递归求大小int leaves() const;           //返回叶数量static int getLeaf(TNode*);   //递归求叶数int height() const;           //返回树高度static int getHeight(TNode*); //递归求高度int level(TIterator it) const;//返回当前层数static int getLevel(TNode*);  //递归求层数void reflect();                    //交换所有节点的左右子节点void defoliate();                  //落叶,删除所有叶节点static void deleteNode(TNode* n);  //递归删除叶节点T& getRoot() const;static bool isRoot(TIterator);         //判断当前迭代器是否为根节点static bool isLeaf(TIterator);         //判断当前迭代器是否为叶节点static TIterator getParent(TIterator); //返回当前迭代器的父节点           static TIterator leftChild(TIterator); //返回当前迭代器的左子节点static TIterator rightChild(TIterator);//返回当前迭代器的右子节点static TIterator find(TIterator, TIterator, const T&); //在前后迭代器中查找 friend class TIterator;};template <class T>BinaryTree<T>::BinaryTree(const T& t, const BinaryTree& lTree, const BinaryTree& rTree){_root = new TNode(t);_root->left = clone(lTree._root, _root);_root->right = clone(rTree._root, _root);}template <class T>BinaryTree<T>::~BinaryTree(){if (_root != 0)delete _root;}template <class T>BinaryTree<T>& BinaryTree<T>::operator = (const BinaryTree& t){clear(_root);BinaryTree* temp = new BinaryTree(t);_root = temp->_root;return *this;}template <class T>void BinaryTree<T>::clear(TNode* n) //递归式后序遍历{if (n == 0) return;clear(n->left);clear(n->right);delete n;}template <class T>typename BinaryTree<T>::TNode* BinaryTree<T>::preorderSuccessor(TNode* n) //前序遍历{if (n == 0) return n;if (n->left) return n->left;if (n->right) return n->right;while (n->parent != n && (n->parent->right == n ||n->parent->right == 0))n = n->parent;if (n->parent == n) return 0;return n->parent->right;}template <class T>typename BinaryTree<T>::TNode* BinaryTree<T>::clone(TNode* root, TNode* parent) //递归式前序遍历{if (!root) return 0;TNode* temp = new TNode(root->val,parent);temp->left = clone(root->left, temp);temp->right = clone(root->right, temp);return temp;}template <class T>typename BinaryTree<T>::TIterator BinaryTree<T>::begin(){return TIterator(this, _root);}template <class T>typename BinaryTree<T>::TIterator BinaryTree<T>::end(){return TIterator(this, 0);}template <class T>bool BinaryTree<T>::empty() const{return _root == 0;}template <class T>int BinaryTree<T>::getSize(TNode* n) //递归求树大小{if (!n) return 0;int sizeL = getSize(n->left);int sizeR = getSize(n->right);return 1+sizeL+sizeR;}template <class T>int BinaryTree<T>::size() const{return getSize(_root);}template <class T>int BinaryTree<T>::getLeaf(TNode* n) //递归求叶数{if (!n) return 0;if (n->left == 0 && n->right == 0) return 1;return getLeaf(n->left)+getLeaf(n->right);}template <class T>int BinaryTree<T>::leaves() const{return getLeaf(_root);}template <class T>int BinaryTree<T>::getHeight(TNode* node) //递归求高度{if (!node) return -1;int hl = getHeight(node->left);int hr = getHeight(node->right);int sum = (hl > hr ? hl : hr);return 1+sum;}template <class T>int BinaryTree<T>::height() const{return getHeight(_root);}template <class T>int BinaryTree<T>::getLevel(TNode* node) //递归求层数{if(node == node->parent) return 0;int sum = getLevel(node->parent);return sum+1;}template <class T>int BinaryTree<T>::level(TIterator it) const{return getLevel(it.node);}template <class T>void BinaryTree<T>::reflect()  //交换所有节点的左右子节点{for (TIterator it = begin(); it != end(); it ++){if (it.node->left && it.node->right){T temp = it.node->left->val;it.node->left->val = it.node->right->val;it.node->right->val = temp;}}}template <class T>void BinaryTree<T>::deleteNode(TNode* n) //递归删除叶节点{TNode* nodeL = n->left;if (nodeL && (nodeL->left || nodeL->right)) deleteNode(nodeL);else{delete nodeL;n->left = 0;}TNode* nodeR = n->right;if (nodeR && (nodeR->left || nodeR->right)) deleteNode(nodeR);else{delete nodeR;n->right = 0;}}template <class T>void BinaryTree<T>::defoliate()  //落叶,删除所有叶节点{if (!_root) return ;if (_root->left || _root->right) deleteNode(_root);else clear(_root);}template <class T>T& BinaryTree<T>::getRoot() const{if (!_root) return T();return _root->val;}template <class T>bool BinaryTree<T>::isRoot(TIterator it) //判断当前迭代器是否是根节点{return it.node == it.node->parent;}template <class T>bool BinaryTree<T>::isLeaf(TIterator it) //判断当前迭代器是否是叶节点{return !(it.node->left || it.node->right);}template <class T>typename BinaryTree<T>::TIterator BinaryTree<T>::getParent(TIterator it) //返回当前迭代器的父节点{if (!it.node->parent) return TIterator(it.tree, 0);return TIterator(it.tree, it.node->parent);}template <class T>typename BinaryTree<T>::TIterator BinaryTree<T>::leftChild(TIterator it) //返回当前迭代器的左子节点{if (!it.node->left) return TIterator(it.tree, 0);return TIterator(it.tree, it.node->left);}template <class T>typename BinaryTree<T>::TIterator BinaryTree<T>::rightChild(TIterator it) //返回当前迭代器的右子节点{if (!it.node->right) return TIterator(it.tree, 0);return TIterator(it.tree, it.node->right);}template <class T>typename BinaryTree<T>::TIterator BinaryTree<T>::find(TIterator first, TIterator last, const T& t) //在前后迭代器范围内查找{for (TIterator it(first); it != last; it ++){if (t == it.node->val){return it;}}return TIterator(first.tree, 0);}#endif


原创粉丝点击