c++ 二叉树实现
参考书籍 <<c++数据结构>> 邓俊辉
#include <cstdlib>
#include "Queue.h"
#define stature(p) ((p) ? (p) -> height : -1) //返回一个节点的高度
typedef enum {RB_RED, RB_BLACK} RBColor;
template <typename T>
struct BinNode
{
T data;
BinNode<T>* parent;
BinNode<T>* lChild;
BinNode<T>* rChild;
int height; //节点的高度
int npl;
RBColor color;
BinNode():parent(NULL), lChild(NULL), rChild(NULL), height(0), npl(1), color(RB_RED) {} //构造函数
BinNode(T e, BinNode<T>* p = NULL, BinNode<T>* lc = NULL, BinNode<T>* rc = NULL, int h = 0, int l = 1, RBColor c = RB_RED):data(e), parent(p), lChild(lc), rChild(rc), height(h), npl(l), color(c) {}
int size();
BinNode<T>* insertAsLC(T const& e)
{
return lChild = new BinNode(e, this);
}
BinNode<T>* insertAsRC(T const& e)
{
return rChild = new BinNode(e, this);
}
BinNode<T> succ()
{
//中序遍历的后继
BinNode<T>* s = this;
if(rChild) //存在右子树, 进入右子树, 遍历左孩子
{
s = rChild;
while(HasLChild(*s))
s = s -> lChild;
}
else
{
while(IsRChild(*s)) //该节点为某一个左子树的最后一个有孩子 其后继为该左子树的最低祖先
s = s -> parent; //到达左子树的根, 该节点为左孩子
s = s -> parent;
}
return s;
}
template <typename VST> void travLevel(VST& );
{
Queue<BinNode<T>*> Q;
Q.enqueue(this);
while(!Q.empty())
{
BinNode<T>* x = Q.dequeue();
visit(x -> data);
if(HasLChild(*x)) Q.enqueue(x -> lChild);
if(HasRChild(*x)) Q.enqueue(x -> rChild);
}
}
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; }
}
#include "BinNode.h"7yt5
# define IsRoor(x) (!((x).parent))
# define IsLChild (!IsRoot(x) && (&(x) == (x).parent -> lChild))
# define IsRChild (!IsRoot(x) && (&(x) == (x).parent -> rChild))
# define HasParent (!IsRoot(x))
# define HasLChild ((x).lChild)
# define HasRChild ((x).rChild)
# define HasChild (HasLChild(x) || HasChild(x))
# define HasBothChild (HasLChild(x) && HasRChild(x))
# define IsLeaf(x) (!HasChild(x))
//兄弟节点
# define sibling(p) (IsLChild(*(p)) ? (p) -> parent -> rChild : (p) -> parent -> lChild)
//叔叔节点
# define uncle(x) (IsLChild(*((x) -> parent)) ? (x) -> parent -> parent -> rChild : (x) -> parent -> parent -> lChild)
# define FromPaentTo(x) (IsRoot(x) ? _root: (IsLChild(x) ? (x).parent -> lChild : (x).parent -> rChild))
template <typename T>
class BinTree
{
protected:
int _size;
BinNode<T>* _root;
virtual int updateHeigth(BinNode<T>* x);
void updateHeightAbove(BinNode<T>* x);
public:
BinTree(): _size(0), _root(NULL) {}
~BinTree() {if (0 < _size) remove(_root); }
int size() const { return _size; }
bool empty() const { return !_root; }
BinNode<T>* root() const { return _root; }
BinNode<T>* insertAsRoot(T const& e)
{
_size = 1;
return _root = new BinNode<T>(e);
}
BinNode<T>* insertAsLC(BinNode<T>* x, T const& e)
{
_size++;
x -> insertAsLC(e);
updateHeightAbove(x);
return x -> lChild;
}
BinNode<T>* insertAsRC(BinNode<T>* x. T const& e);
{
_size++;
x -> insertAsRC(e);
updateHeightAbove(x);
return x -> rChild;
}
BinNode<T>* attachAsLC(BinNode<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;
}
BinNode<T>* attachAsRC(BinNode<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;
}
int remove(BinNode<T>* x)
{
FromParentTo(*x) = NULL;
updateHeightAbove(x -> parent);
int n = removeAt(x);
_size -= n;
return n;
}
BinTree<T>* secede(BinNode<T>* x)
{
FromParentTo(*x) = NULL;
updateHeightAbove(x -> parent);
BinTree<T>* S = new BinTree<T>;
S -> _root = x;
x -> parent = NULL:
S -> _size = x -> size;
_size -= S -> _size;
return S;
}
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); }
};
template <typename T>
int BinTree<T>::updateHeight(BinNode<T>* x)
{
return x -> height = 1 + max(stature(x -> lChild), stature(x -> rChild)); }
}
template <typename T>
void BinTree<T>::updateHeightAbove(BinNode<T>* x)
{
while(x)
{
updateHeight(x);
x = x -> parent;
}
}
template <typename T>
static int removeAt(BinNode<T>* x)
{
if(!x) return 0;
int n = 1 + removeAt(x -> lChild) + removeAt(x -> rChild);
release(x -> data);
release(x);
return n;
}
template <typename T, typename VST>
void travPre_R(BinNode<T>* x, VST& Visit)
{
if(!x) return;
visit(x -> data);
travPre_R(x -> lChild, vist);
travPre_R(x -> rChild, vist);
}
template <typename T, typename VST>
void travPre_Post(BinNode<T>* x, VST& Visit)
{
if(!x) return;
travPre_Post(x -> lChild, vist);
travPre_Post(x -> rChild, vist);
visit(x -> data);
}
template <typename T, typename VST>
void travIn_R(BinNode<T>* x, VST& Visit)
{
if(!x) return;
travPre_In(x -> lChild, vist);
visit(x -> data);
travPre_In(x -> rChild, vist);
}