0001 #include "BinNode.h" //引入二叉树节点类 0002 template <typename T> class BinTree { //二叉树模板类 0003 protected: 0004 Rank _size; BinNodePosi<T> _root; //规模、根节点 0005 public: 0006 BinTree() : _size( 0 ), _root( NULL ) {} //构造函数 0007 ~BinTree() { if ( 0 < _size ) remove( _root ); } //析构函数 0008 Rank size() const { return _size; } //规模 0009 bool empty() const { return !_root; } //判空 0010 BinNodePosi<T> root() const { return _root; } //树根 0011 BinNodePosi<T> insert( T const& ); //插入根节点 0012 BinNodePosi<T> insert( T const&, BinNodePosi<T> ); //插入左孩子 0013 BinNodePosi<T> insert( BinNodePosi<T>, T const& ); //插入右孩子 0014 BinNodePosi<T> attach( BinTree<T>*&, BinNodePosi<T> ); //接入左子树 0015 BinNodePosi<T> attach( BinNodePosi<T>, BinTree<T>*& ); //接入右子树 0016 Rank remove ( BinNodePosi<T> ); //子树删除 0017 BinTree<T>* secede ( BinNodePosi<T> ); //子树分离 0018 template <typename VST> //操作器 0019 void travLevel( VST& visit ) { if ( _root ) _root->travLevel( visit ); } //层次遍历 0020 template <typename VST> //操作器 0021 void travPre( VST& visit ) { if ( _root ) _root->travPre( visit ); } //先序遍历 0022 template <typename VST> //操作器 0023 void travIn( VST& visit ) { if ( _root ) _root->travIn( visit ); } //中序遍历 0024 template <typename VST> //操作器 0025 void travPost( VST& visit ) { if ( _root ) _root->travPost( visit ); } //后序遍历 0026 bool operator<( BinTree<T> const& t ) //比较器(其余自行补充) 0027 { return _root && t._root && lt( _root, t._root ); } 0028 bool operator==( BinTree<T> const& t ) //判等器 0029 { return _root && t._root && ( _root == t._root ); } 0030 }; //BinTree