
数据结构 第五章 二叉树-2
发布日期:2021-05-07 18:20:30
浏览次数:16
分类:技术文章
本文共 3221 字,大约阅读时间需要 10 分钟。
BinNode模板类:
#include#define BinNodePosi(T) BinNode *#define stature(p) ((p) ? (p)->height : -1)typedef enum {RB_RED, RB_BLACk} RBColor;template struct BinNode{ T data; BinNodePosi(T) parent; BinNodePosi(T) lc; BinNodePosi(T) rc; 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) isnertAsRC( T const& ); BinNodePosi(T) succ(); template void travLevel( VST& ); template void travPre ( VST& ); template void travIn ( VST& ); template void travPost ( VST& ); bool operator< ( BinNode const& bn) { return data < bn.data; } bool operator== ( BinNode const& bn) { return data == bn.data; }};
快捷接口:
#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) )#define sibiling(p) (IsLChild(*p) ? (p)->parent->rc : (p)->parent->lc)#define uncle(x) (IsLChild(*(x)->parent) ? (x)->parent->parent->rc :(x)->parent->parent->lc)#define FromParentTo(x) (IsRoot(x) ? _root : (IsLChild(x) ? (x).parent->lc : (x).parent->rc))
size():
templateint BinNode ::size(){ int s = 1; if (lc) s += lc->size; if (rc) s += rc->size; return s;}
BinTree模板类:
#include "BinNode.h"#includetemplate class BinTree {protected: int _size; BinNodePosi(T) _root; virtual int updateHeight( BinNodePosi(T) x ); void updateHeightAbove( BinNodePosi(T) 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) x, T const& e ); BinNodePosi(T) insertAsRC( BinNodePosi(T) x, T const& e ); BinNodePosi(T) attachAsLC( BinNodePosi(T) x, BinTree * &T ); BinNodePosi(T) attachAsRC( BinNodePosi(T) x, BinTree * &T ); int remove( BinNodePosi(T) x ); BinTree * secede( BinNodePosi(T) x ); template void travLevel( VST& visit) { if(_root) _root->travLevel( visit ); } template void travPre( VST& visit) { if(_root) _root->travPre( visit ); } template void travPost( VST& visit) { if(_root) _root->travPost( visit ); } template void travIn( VST& visit) { if(_root) _root->travIn( visit ); } bool operator< (BinTree const& t) { return _root && t._root && lt( _root, t._root); } bool operator== (BinTree const& t) { return _root && t._root && (_root == t._root); } };
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年04月03日 04时12分53秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
计算几何(旁切圆) - Ex-circles - UVA 11731
2019-03-04
DP - Tickets - HDU - 1260
2019-03-04
JVM篇-结合源码分析垃圾收集器的类型
2019-03-04
Warning: The core is locked up的解决办法
2019-03-04
Spring 与使用STOMP消息
2019-03-04
Java Swing JList:列表框组件
2019-03-04
AngularJS $q
2019-03-04
jQuery中的动画
2019-03-04
Linux host命令
2019-03-04
MongoDB 查询分析
2019-03-04
编写Makefile.am
2019-03-04
狂神说MySQL01:初识MySQL
2019-03-04
5.3.2 等待一段时间:编写延时循环
2019-03-04
1.2.3 项目、项目集、项目组合以及运营管理之间的关系
2019-03-04
光环和你一起迎接改版
2019-03-04
1.12 项目和运营的区别
2019-03-04
习惯养成记打卡-第7章 项目成本管理
2019-03-04
习惯养成记打卡-第9章 项目资源管理
2019-03-04
LeetCode - 98. 验证二叉搜索树(迭代、递归)2
2019-03-04