0001 using Rank = unsigned int; 0002 0003 #if defined( DSA_REDBLACK ) //在红黑树中 0004 #define stature( p ) ( ( p ) ? ( p )->height : 0 ) //外部节点(黑)高度为0,以上递推 0005 #else //其余BST中 0006 #define stature( p ) ( (int)( ( p ) ? ( p )->height : -1 ) ) //外部节点高度为-1,以上递推 0007 #endif 0008 0009 typedef enum { RB_RED, RB_BLACK} RBColor; //节点颜色 0010 0011 template <typename T> struct BinNode; 0012 template <typename T> using BinNodePosi = BinNode<T>*; //节点位置 0013 0014 template <typename T> struct BinNode { //二叉树节点模板类 0015 // 成员(为简化描述起见统一开放,读者可根据需要进一步封装) 0016 T data; //数值 0017 BinNodePosi<T> parent, lc, rc; //父节点及左、右孩子 0018 Rank height; //高度(通用) 0019 Rank npl; //Null Path Length(左式堆,也可直接用height代替) 0020 RBColor color; //颜色(红黑树) 0021 // 构造函数 0022 BinNode() : parent( NULL ), lc( NULL ), rc( NULL ), height( 0 ), npl( 1 ), color( RB_RED ) {} 0023 BinNode( T e, BinNodePosi<T> p = NULL, BinNodePosi<T> lc = NULL, 0024 BinNodePosi<T> rc = NULL, int h = 0, int l = 1, RBColor c = RB_RED ) 0025 : data( e ), parent( p ), lc( lc ), rc( rc ), height( h ), npl( l ), color( c ) {} 0026 // 操作接口 0027 Rank size(); //统计当前节点后代总数,亦即以其为根的子树的规模 0028 Rank updateHeight(); //更新当前节点高度 0029 void updateHeightAbove(); //更新当前节点及其祖先的高度 0030 BinNodePosi<T> insertAsLC( T const& ); //作为当前节点的左孩子插入新节点 0031 BinNodePosi<T> insertAsRC( T const& ); //作为当前节点的右孩子插入新节点 0032 BinNodePosi<T> succ(); //取当前节点的直接后继 0033 template <typename VST> void travLevel( VST& ); //子树层次遍历 0034 template <typename VST> void travPre( VST& ); //子树先序遍历 0035 template <typename VST> void travIn( VST& ); //子树中序遍历 0036 template <typename VST> void travPost( VST& ); //子树后序遍历 0037 // 比较器、判等器(各列其一,其余自行补充) 0038 bool operator< ( BinNode const& bn ) { return data < bn.data; } //小于 0039 bool operator== ( BinNode const& bn ) { return data == bn.data; } //等于 0040 };