做电影资源网站有哪些,代码运行框wordpress,高端网站开发建设,北京正邦设计公司官网目录
#x1f308;前言#x1f308;
#x1f4c1; 红黑树的概念
#x1f4c1; 红黑树的性质
#x1f4c1; 红黑树节点的定义
#x1f4c1; 红黑树的插入操作
#x1f4c1; 红黑树和AVL树的比较
#x1f4c1; 全代码展示
#x1f4c1; 总结 #x1f308;前言… 目录
前言 红黑树的概念 红黑树的性质 红黑树节点的定义 红黑树的插入操作 红黑树和AVL树的比较 全代码展示 总结 前言 欢迎大家观看本期【C杂货铺】本期内容将讲解二叉搜索树的进阶——红黑树。红黑树是一种二叉搜索树通过控制每个节点的颜色来调整整棵树的高度。 红黑树是set和map实现的底层实现在下一期内容我们将讲解STL中set和map的模拟实现。如果你对二叉搜索树还不是很了解可以快速阅览下面这篇文章; 【C杂货铺】二叉搜索树-CSDN博客 红黑树的概念 红黑树是一种二叉搜索树在每个节点增加一个存储位表示节点的颜色可以是Red或Black。通过对任何一条从根到叶子的路径上各个节点着色方式的限制红黑树确保没有一条路径会比其他路径长出两倍因而接近平衡的。 红黑树的性质
1. 每个节点不是红色就是黑色
2. 根节点是黑色的
3. 如果一个节点是红色的那么它的两个孩子节点是黑色的
4. 对于每个节点从该节点到其他所有后代叶节点的简单路径上均包含相同数目的黑色节点个数
5. 每个叶子结点都是黑色的(此后的叶子结点指的是空节点) 红黑树节点的定义
// 节点的颜色
enum Color{RED, BLACK};
// 红黑树节点的定义
templateclass ValueType
struct RBTreeNode
{RBTreeNode(const ValueType data ValueType()Color color RED): _pLeft(nullptr), _pRight(nullptr), _pParent(nullptr), _data(data), _color(color){}RBTreeNodeValueType* _pLeft; // 节点的左孩子RBTreeNodeValueType* _pRight; // 节点的右孩子RBTreeNodeValueType* _pParent; // 节点的双亲(红黑树需要旋转为了实现简单给
出该字段)ValueType _data; // 节点的值域Color _color; // 节点的颜色
}; 红黑树的插入操作 红黑树是在二叉搜索树的基础上加上其平衡限制条件因此红黑树的插入可分为两步
1. 按照二叉搜索树规则插入新节点 新插入节点的默认颜色是红色。因为红色容易控制规则如果默认插入黑色需要保证从当前节点到叶子节点具有相同的黑色节点个数不易控制。 即先保证性质4不被违反再来判断性质3是否被违反如果违反就进行调整。
2. 检测新节点插入后红黑树的性质是否遭到破坏。 如果双亲节点的颜色是黑色没有违反规则则不需要调整当新插入节点的双亲节点颜色为红色时就违反了性质3即不能有连续在一起的红色节点此时需要进行调整可分为两种情况
约定cur为当前节点p为父亲节点g为祖父节点u为叔叔节点
● 情况1cur为红p为红g为黑u存在且为红 ● 情况2cur为红p为红g为黑u存在且为黑 或者 u不存在 当如子树如下图所示时需要对红黑树进行双旋先以p为根进行左旋再以g为根进行右旋。下图是p在g的左子树的情况考虑一下p在g的右子树且cur为p的左子树的情况。 红黑树和AVL树的比较 红黑树和AVL数都是搞笑的平衡二叉树增删查改的时间复杂度都是O(log N)红黑树不追求绝对平衡其只需要保证最长路径不超过最短路径的2倍相对而言降低了插入和旋转的次数所以在经常进行增删的结构中性能比AVL数更优而且红黑树的实现比较简单所以实际应用中红黑树更多。 map和set的底层就是红黑树。 全代码展示
template class T
struct RBTreeNode
{typedef RBTreeNodeT Node;RBTreeNode(const T val T()):_left(nullptr), _right(nullptr), _parent(nullptr), _val(val), _color(RED){}Node* _left;Node* _right;Node* _parent;T _val;Color _color;
};templateclass T
class RBTree
{typedef RBTreeNodeT Node;
public:bool Insert(const T val){if (_root nullptr){_root new Node(val);_root-_color Black;return true;}Node* cur _root;Node* parent nullptr;while (cur){if (cur-_val val){parent cur;cur cur-_left;}else if (cur-_val val){parent cur;cur cur-_right;}else{return false;}}cur new Node(val);if (parent-_val val){parent-_right cur;}else{parent-_left cur;}cur-_parent parent;//调整颜色不能出现连续的红色while (parent parent-_color RED){Node* grandfather parent-_parent;if (parent grandfather-_left){//叔叔在右边//1. 叔叔存在且为红色Node* uncle grandfather-_right;if (uncle uncle-_color RED){grandfather-_color RED;uncle-_color parent-_color Black;cur grandfather;parent cur-_parent;}//2. 叔叔存在且为黑色 || 叔叔不存在else{/*gp uc */if (cur parent-_left){RotateR(grandfather);parent-_color Black;grandfather-_color RED;}/*gp uc*/else{RotateL(parent);RotateR(grandfather);cur-_color Black;grandfather-_color RED;}break;}}else{//叔叔在左边//1. 叔叔存在且为红色Node* uncle grandfather-_left;if (uncle uncle-_color RED){grandfather-_color RED;parent-_color uncle-_color Black;cur grandfather;parent cur-_parent;}//2. 叔叔存在且为黑色 || 叔叔不存在else{/*gu pc*/if (cur parent-_right){RotateL(grandfather);parent-_color Black;grandfather-_color RED;}/*gu pc */else{RotateR(parent);RotateL(grandfather);cur-_color Black;grandfather-_color RED;}break;}}}_root-_color Black;return true;}//遍历
void InOrder()
{_InOrder(_root);cout endl;
}bool isBalance()
{if (_root nullptr){return true;}int BlackNum 0;Node* cur _root;while (cur){if (cur-_color Black)BlackNum;cur cur-_right;}return _isBalance(_root,0,BlackNum);
}protected:bool _isBalance(Node* root,int count , const int BlackNum){if(root nullptr){if (count ! BlackNum)return false;return true;}if (root-_color RED root-_parent-_color RED){cout red endl;return false;}if (root-_color Black)count;return _isBalance(root-_left,count,BlackNum) _isBalance(root-_right,count,BlackNum);}void _InOrder(Node* root){if (root nullptr){return;}_InOrder(root-_left);cout root-_val endl;_InOrder(root-_right);}//左单旋void RotateL(Node* parent){Node* subR parent-_right;Node* subRL subR-_left;parent-_right subRL;if (subRL)subRL-_parent parent;subR-_left parent;Node* pparent parent-_parent;parent-_parent subR;if (parent _root){_root subR;_root-_parent nullptr;}else{if (parent pparent-_right){pparent-_right subR;}else{pparent-_left subR;}subR-_parent pparent;}}//右单旋void RotateR(Node* parent){Node* subL parent-_left;Node* subLR subL-_right;parent-_left subLR;if (subLR)subLR-_parent parent;subL-_right parent;Node* pparent parent-_parent;parent-_parent subL;if (parent _root){_root subL;_root-_parent nullptr;}else{if (parent pparent-_right){pparent-_right subL;}else{pparent-_left subL;}subL-_parent pparent;}}
private:Node* _root nullptr;
}; 总结 以上就是本期【C杂货铺】的主要内容了讲解了红黑树如果优化二叉搜索树红黑树的概念红黑树实现插入以及红黑树的高度平衡调整此外模拟实现了红黑树。 如果感觉本期内容对你有帮助欢迎点赞收藏关注Thanks♪(ω)
文章转载自: http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.kgjyy.cn.gov.cn.kgjyy.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.frpb.cn.gov.cn.frpb.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.lfjmp.cn.gov.cn.lfjmp.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.sffwz.cn.gov.cn.sffwz.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn