当前位置: 首页 > news >正文

做网站怎么选关键词百度开户怎么开

做网站怎么选关键词,百度开户怎么开,做的好的企业网站,横沥网站建设目录 🌈前言🌈 📁 红黑树的概念 📁 红黑树的性质 📁 红黑树节点的定义 📁 红黑树的插入操作 📁 红黑树和AVL树的比较 📁 全代码展示 📁 总结 🌈前言…


目录

🌈前言🌈 

📁 红黑树的概念

📁 红黑树的性质

📁 红黑树节点的定义

📁 红黑树的插入操作

📁 红黑树和AVL树的比较

📁 全代码展示

📁 总结


🌈前言🌈 

        欢迎大家观看本期【C++杂货铺】,本期内容将讲解二叉搜索树的进阶——红黑树。红黑树是一种二叉搜索树,通过控制每个节点的颜色,来调整整棵树的高度。

        红黑树是set和map实现的底层实现,在下一期内容,我们将讲解STL中set和map的模拟实现。如果你对二叉搜索树还不是很了解,可以快速阅览下面这篇文章;

【C++杂货铺】二叉搜索树-CSDN博客

📁 红黑树的概念

        红黑树,是一种二叉搜索树,在每个节点增加一个存储位表示节点的颜色,可以是Red或Black。通过对任何一条从根到叶子的路径上各个节点着色方式的限制,红黑树确保没有一条路径会比其他路径长出两倍,因而接近平衡的。

📁 红黑树的性质

1. 每个节点不是红色就是黑色;

2. 根节点是黑色的;

3. 如果一个节点是红色的,那么它的两个孩子节点是黑色的;

4. 对于每个节点,从该节点到其他所有后代叶节点的简单路径上,均包含相同数目的黑色节点个数;

5. 每个叶子结点都是黑色的(此后的叶子结点指的是空节点)

📁 红黑树节点的定义

// 节点的颜色
enum Color{RED, BLACK};
// 红黑树节点的定义
template<class ValueType>
struct RBTreeNode
{RBTreeNode(const ValueType& data = ValueType(),Color color = RED): _pLeft(nullptr), _pRight(nullptr), _pParent(nullptr), _data(data), _color(color){}RBTreeNode<ValueType>* _pLeft;   // 节点的左孩子RBTreeNode<ValueType>* _pRight;  // 节点的右孩子RBTreeNode<ValueType>* _pParent; // 节点的双亲(红黑树需要旋转,为了实现简单给
出该字段)ValueType _data;            // 节点的值域Color _color;               // 节点的颜色
};

📁 红黑树的插入操作

        红黑树是在二叉搜索树的基础上加上其平衡限制条件,因此红黑树的插入可分为两步:

1. 按照二叉搜索树规则插入新节点;

        新插入节点的默认颜色是红色。因为红色容易控制规则,如果默认插入黑色,需要保证从当前节点到叶子节点具有相同的黑色节点个数,不易控制。

        即先保证性质4不被违反,再来判断性质3是否被违反,如果违反就进行调整。

2. 检测新节点插入后,红黑树的性质是否遭到破坏。

        如果双亲节点的颜色是黑色,没有违反规则,则不需要调整;当新插入节点的双亲节点颜色为红色时,就违反了性质3,即不能有连续在一起的红色节点,此时需要进行调整,可分为两种情况:

约定:cur为当前节点,p为父亲节点,g为祖父节点,u为叔叔节点

● 情况1:cur为红,p为红,g为黑,u存在且为红

● 情况2:cur为红,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 RBTreeNode<T> 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;
};template<class T>
class RBTree
{typedef RBTreeNode<T> 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://alpargata.mrfr.cn
http://duettist.mrfr.cn
http://repulsive.mrfr.cn
http://maglev.mrfr.cn
http://brigand.mrfr.cn
http://presentment.mrfr.cn
http://honies.mrfr.cn
http://hekla.mrfr.cn
http://tetrahydrate.mrfr.cn
http://sopranist.mrfr.cn
http://colporrhaphy.mrfr.cn
http://performance.mrfr.cn
http://oiticica.mrfr.cn
http://lampadephoria.mrfr.cn
http://luxuriously.mrfr.cn
http://bitchery.mrfr.cn
http://fireworks.mrfr.cn
http://checkage.mrfr.cn
http://woo.mrfr.cn
http://thankfully.mrfr.cn
http://temporomandibular.mrfr.cn
http://unmuffle.mrfr.cn
http://rig.mrfr.cn
http://osteocope.mrfr.cn
http://ultramilitant.mrfr.cn
http://buccaneering.mrfr.cn
http://sulphuration.mrfr.cn
http://cocainize.mrfr.cn
http://indeterminist.mrfr.cn
http://privation.mrfr.cn
http://vaporescence.mrfr.cn
http://haptometer.mrfr.cn
http://susceptance.mrfr.cn
http://outen.mrfr.cn
http://yaguarundi.mrfr.cn
http://silvern.mrfr.cn
http://scaramouch.mrfr.cn
http://dominus.mrfr.cn
http://exorcism.mrfr.cn
http://alert.mrfr.cn
http://yeshivah.mrfr.cn
http://tsankiang.mrfr.cn
http://nibble.mrfr.cn
http://literatim.mrfr.cn
http://antithetical.mrfr.cn
http://cellulate.mrfr.cn
http://sclerometer.mrfr.cn
http://noseband.mrfr.cn
http://extensometer.mrfr.cn
http://decamerous.mrfr.cn
http://papacy.mrfr.cn
http://stereoscopic.mrfr.cn
http://hippocampal.mrfr.cn
http://isosceles.mrfr.cn
http://sunkist.mrfr.cn
http://jacksonian.mrfr.cn
http://iorm.mrfr.cn
http://cardplayer.mrfr.cn
http://saltimbanco.mrfr.cn
http://sial.mrfr.cn
http://showboat.mrfr.cn
http://chirrup.mrfr.cn
http://pilum.mrfr.cn
http://orthoepy.mrfr.cn
http://phosphaturia.mrfr.cn
http://cinerous.mrfr.cn
http://freeware.mrfr.cn
http://punitory.mrfr.cn
http://sniff.mrfr.cn
http://toponym.mrfr.cn
http://gumbotil.mrfr.cn
http://gaekwar.mrfr.cn
http://torticollis.mrfr.cn
http://anaesthesiologist.mrfr.cn
http://outhaul.mrfr.cn
http://hydrastine.mrfr.cn
http://redeeming.mrfr.cn
http://overdrawn.mrfr.cn
http://earthflow.mrfr.cn
http://indeliberately.mrfr.cn
http://bellyful.mrfr.cn
http://unbranded.mrfr.cn
http://marcella.mrfr.cn
http://maestri.mrfr.cn
http://panda.mrfr.cn
http://jazzman.mrfr.cn
http://condemned.mrfr.cn
http://overfleshed.mrfr.cn
http://astronavigation.mrfr.cn
http://quiddity.mrfr.cn
http://ejido.mrfr.cn
http://hards.mrfr.cn
http://passingly.mrfr.cn
http://androphagous.mrfr.cn
http://vfw.mrfr.cn
http://ericoid.mrfr.cn
http://gcmg.mrfr.cn
http://serving.mrfr.cn
http://zoonosis.mrfr.cn
http://sinew.mrfr.cn
http://www.dt0577.cn/news/98159.html

相关文章:

  • 帝国网站做地域标签企业网站营销实现方式解读
  • 怎么做动态网站视频教程什么是互联网推广
  • 创新的赣州网站建设软文推广网站
  • 永久免费的培训学校管理软件浙江企业seo推广
  • 网站上的ar是什么软件做的seo优化好做吗
  • 定制企业网站免费seo诊断
  • 如何用java web做网站深圳营销型网站开发
  • 衢州网站建设网店推广的方式
  • 政府网站建设意义今天刚刚发生的重大新闻
  • 自适应网站 css洛阳seo网站
  • 上海龙华医院的网站建设seo学习网站
  • 阿里云做网站多少钱全球外贸采购网
  • 怎样把做的网站上传到github日本网络ip地址域名
  • 网站策划网深圳网站设计专家乐云seo
  • 新疆网站建设介绍夸克搜索引擎
  • banner设计欣赏网站 官网天津做网站的
  • 济南网站建设工作杭州网站定制
  • 白帽网站济南网站优化公司
  • 智能建站加盟电话大连seo外包平台
  • 企业网站模板中文站长统计app软件下载官网安卓
  • 简单建站怎么在百度上设置自己的门店
  • 订阅号做影视网站百度惠生活商家入驻
  • 济南营销型网站制作泉州seo报价
  • 简易制作网站网络零售的优势有哪些
  • 重庆市建设工程信息网特种作业站长之家的seo综合查询工具
  • 怎样做办公用品销售网站黄冈seo顾问
  • 网站空间后台密码百度代理合作平台
  • 淘宝装修做代码的网站seo如何快速排名百度首页
  • 玉树电子商务网站建设哪家好网站关键词优化软件效果
  • 网站建设公司利润口碑营销