目录
1. 头文件
1.1 二叉树节点文件
#include"Stack.h"
#include"Queue.h"
//BinNode状态与性质的判断
#define IsRoot(x) (!((x).parent))
#define IsLChild(x) (!IsRoot(x) && (&(x) == (x).parent->lc))
#define IsRChild(x) (!IsRoot(x) && (&(x) == (x).parent->rc))
#define HasParent(x) (!IsRoot(x))
#define HasLChild(x) ((x).lc)
#define HasRChild(x) ((x).rc)
#define HasChild(x) (HasLChild(x) || HasRChild(x)) //至少拥有一个孩子
#define HasBothChild(x) (HasLChild(x) && HasRChild(x)) //同时拥有两个孩子
#define IsLeaf(x) (!HasChild(x))
//与BinNode具有特定关系的节点及指针
#define sibling( p ) ( IsLChild( * (p) ) ? (p)->parent->rc : (p)->parent->lc ) /*兄弟*/
#define uncle(x) (sibling((x)->parent)) /*叔叔*/
#define FromParentTo( x ) /*来自父亲的引用*/ \
( IsRoot(x) ? _root : ( IsLChild(x) ? (x).parent->lc : (x).parent->rc ) )
#define BinNodePosi(T) BinNode<T>* //节点位置
#define stature(p) ((p)?(p->height):-1)//节点高度,空树高度为-1
typedef enum{RB_RED=0,RB_BLACK}RBColor;//节点颜色
//节点类
template <typename T> struct BinNode {
BinNodePosi(T) parent;//父亲
BinNodePosi(T) lc;//左孩子
BinNodePosi(T) rc; //右孩子
T data; //数据
int height; //高度(通用)
int npl;//左式堆
RBColor color;
//构造函数
BinNode() :
parent(NULL), lc(NULL), rc(NULL), height(0), npl(1), color(RB_RED) { }
BinNode(T e, BinNodePosi(T) p = NULL, BinNodePosi(T) lc = NULL, BinNodePosi(T) rc = NULL,
int h = 0, int l = 1, RBColor c = RB_RED) :
data(e), parent(p), lc(lc), rc(rc), height(h), npl(l), color(c) { }
//操作接口
int size(); //统计当前节点后代总数,亦即以其为根的子树的规模
BinNodePosi(T) insertAsLC(T const&); //作为当前节点的左孩子插入新节点
BinNodePosi(T) insertAsRC(T const&); //作为当前节点的右孩子插入新节点
BinNodePosi(T) succ(); //取当前节点的直接后继
template <typename VST> void travLevel(VST&); //子树层次遍历
template <typename VST> void travPre(VST&); //子树先序遍历
template <typename VST> void travIn(VST&); //子树中序遍历
template <typename VST> void travPost(VST&); //子树后序遍历
// 比较器、判等器
bool operator> (BinNode const& bn) { return data > bn.data; } //大于
bool operator< (BinNode const& bn) { return data < bn.data; } //小于
bool operator== (BinNode const& bn){ return data == bn.data; } //等于
bool operator!= (BinNode const& bn){ return data != bn.data; } //不等于于
};
//统计当前节点后代总数,即以其为根的子树规模
template <typename T> int BinNode<T>::size() {
int s = 1; //计入本身
if (lc) s += lc->size(); //递归计入左子树规模
if (rc) s += rc->size(); //递归计入右子树规模
return s;
}
//将e作为当前节点的左孩子插入二叉树
template <typename T> BinNodePosi(T) BinNode<T>::insertAsLC(T const& e)
{ return lc = new BinNode(e, this); }
//将e作为当前节点的右孩子插入二叉树
template <typename T> BinNodePosi(T) BinNode<T>::insertAsRC(T const& e)
{ return rc = new BinNode(e, this); }
//定位节点v的直接后继
template <typename T> BinNodePosi(T) BinNode<T>::succ() {
BinNodePosi(T) s = this; //记录后继的临时变量
if (rc) { //若有右孩子,则直接后继必在右子树中,具体地就是
s = rc; //右子树中
while (HasLChild(*s)) s = s->lc; //最靠左(最小)的节点
}
else { //否则,直接后继应是“将当前节点包含于其左子树中的最低祖先”,具体地就是
while (IsRChild(*s)) s = s->parent; //逆向地沿右向分支,不断朝左上方移动
s = s->parent; //最后再朝右上方移动一步,即抵达直接后继(如果存在)
}
return s;
}
1.2 二叉树文件
注:要现在当前头文件之前引入二叉树节点头文件
#include "BinNode.h"
template <typename T> class BinTree {
protected:
int _size; BinNodePosi(T) _root; //规模、根节点
virtual int updateHeight(BinNodePosi(T) x); //更新节点x的高度
void updateHeightAbove(BinNodePosi(T) x); //更新节点x及其祖先的高度
public:
BinTree() : _size(0), _root(NULL) { } //构造函数
~BinTree() { if (0 < _size) 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),T const& e ); //插入左孩子
BinNodePosi(T) insertAsRC(BinNodePosi(T),T const& e); //插入右孩子
BinNodePosi(T) attachAsLC(BinNodePosi(T),BinTree<T> *&T); //接入左子树
BinNodePosi(T) attachAsRC(BinNodePosi(T),BinTree<