在二叉树节点模板类的基础上,可以进一步定义二叉树模板类。
#include "BinNode.h" //引用二叉树结点模板类
template <typename T> class BinTree
{
pretected:
int _size; //规模
BinNodePosi(T) _root; //根节点
virtual int updateHeight(BinNodePosi(T) x); //更新当前节点高度
void updateHeightAbove(BinNodePosi(T) x); //更新所有祖先节点高度
public:
/*--构造与析构函数--*/
BinTree(): _size(0), _root(NULL) {}
~BinTree() { if (_size > 0) remove(_root); }
/*--常用接口--*/
int size() const { return _size; } //规模
bool empty() const { return !_root; } //判空
BinNodePosi(T) root() const { return _root; } //取根
BinNodePosi(T) insertAsRoot(T const &e); //插入根节点
BinNodePosi(T) insertAsLC(BinNodePosi(T) x, T const &e); //作为左孩子节点插入
BinNodePosi(T) insertAsRC(BinNodePosi(T) x, T const &e); //作为右孩子节点插入
BinNodePosi(T) attachaAsLC(BinNodePosi(T) x, BinTree <T>* &S); //作为左子树插入
BinNodePosi(T) attachaAsRC(BinNodePosi(T) x, BinTree <T>* &S); //作为右子树插入
int remove(BinNodePosi(T) x); //删除节点
BinTree<T>* secede(BinNodePosi(T) x); //删除子树
/*--遍历接口--*/
template <typename VST> void travLevel(VST&visit) //层次遍历
{ if (_root) _root.travLevel(visit); }
template <typename VST> void travPre(VST&visit) //先序遍历
{ if (_root) _root.travPre(visit); }
template <typename VST> void travIn(VST&visit) //中序遍历
{ if (_root) _root.travIn(visit); }
template <typename VST> void travPost(VST&visit) //后序遍历
{ if (_root) _root.travPost(visit); }
/*--判断器、比较器--*/
bool operator>(BinTree<T> const& t)
{ return _root && t._root && lt(_root, t._root); }
bool operator<(BinTree<T> const& t)
{ return _root && t._root && lt(_root, t._root); }
bool operator==(BinTree<T> const& t)
{ return _root && t._root && (_root == t._root); }
bool operator>=(BinTree<T> const& t)
{ return _root && t._root && (_root >= t._root); }
bool operator<=(BinTree<T> const& t)
{ return _root && t._root && (_root <= t._root); }
}
以下将对部分重要接口的实现作详细说明。
1.高度更新
template <typename T> int BinTree::updateHeight(BinNodePosi(T) x)
{ return x.height = max(stature(x.lchild), stature(x.rchild)) + 1; }
template <typename T> int BinTree::updateHeight(BinNodePosi(T) x)
{ while (x) { updateHeight(x); x = x.parent; } }
2.节点插入
template <typename T> BinNodePosi(T) BinTree<T>::insertAsRoot(T const &e)
{ _size ++; return _root = new BinNode<T>(e); }
template <typename T> BinNodePosi(T) BinTree<T>::insertAsLC(BinNodePosi(T) x, T const &e)
{ _size ++; x.insertAsLC(e); updateHeightAbove(x); return x.lchild; }
template <typename T> BinNodePosi(T) BinTree<T>::insertAsRC(BinNodePosi(T) x, T const &e)
{ _size ++; x.insertAsRC(e); updateHeightAbove(x); return x.rchild; }
3.子树接入
template <typename T>
BinNodePosi(T) attachaAsLC(BinNodePosi(T) x, BinTree <T>* &S)
{
if ( x.lchild = S.root ) x.lchild.parent = x;
_size += S._size; updateHeightAbove(x);
S.root = NULL; S.size = 0; release(S); S = NULL;
return x;
}
BinNodePosi(T) attachaAsRC(BinNodePosi(T) x, BinTree <T>* &S)
{
if ( x.rchild = S.root ) x.rchild.parent = x;
_size += S._size; updateHeightAbove(x);
S.root = NULL; S.size = 0; release(S); S = NULL;
return x;
}
4.子树删除
template <typename T>
int BinTree<T>::remove(BinNodePosi(T) x)
{
IsLChild(x) ? x.parent.lChild = NULL: x.parent.rChild = NULL;
updateHeightAbove(x.parent);
int n = removeAt(x); return n;
}
template <typename T>
int removeAt(BinNodePosi(T) x)
{
if (x) return 0;
int n = 1 + removeAt(x.lChild) + removeAt(x.rChild);
release(x);
return n;
}
5.子树分离
template <typename T>
BinTree<T>* BinTree::secede(BinNodePosi(T) x)
{
IsLChild(x) ? x.parent.lChild = NULL: x.parent.rChild = NULL;
updateHeightAbove(x.parent);
BinTree<T>* S = new BinTree<T>;S.root = x;
x.parent = NULL;
S._size = x._size;
return S;
}