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

网站运营顾问外贸网络推广培训

网站运营顾问,外贸网络推广培训,长春商城网站制作,河南国正建设集团公司网站目录 引言 红黑树迭代器实现 红黑树元素的插入 map模拟实现 set模拟实现 之前我们已经学习了map和set的基本使用#xff0c;但是因为map和set的底层都是用红黑树进行封装实现的#xff0c;上期我们已经学习了红黑树的模拟实现#xff0c;所以本期我们在红黑树模拟实现…目录 引言 红黑树迭代器实现 红黑树元素的插入 map模拟实现  set模拟实现 之前我们已经学习了map和set的基本使用但是因为map和set的底层都是用红黑树进行封装实现的上期我们已经学习了红黑树的模拟实现所以本期我们在红黑树模拟实现的基础之上对红黑树进行进一步封装实现map和set的模拟实现。 引言 首先大家思考一个问题map和set既然它们底层都是使用红黑树进行模拟实现的我们知道map是搜索二叉树中的kv模型set是搜索二叉树中的k模型那么两种模型难道使用两颗红黑树实现吗 当然不是map和set底层都是使用同一颗红黑树实现的我们通过使用模板达到这一目的这也体现了泛式编程的重要性。 我们对上期红黑树模拟实现的代码进行一点点改造基于下述代码来进行map和set的模拟实现。 红黑树迭代器实现 templateclass T,class Ref,class Ptr struct RBTreeIterator {typedef RBTreeNodeT Node;typedef RBTreeIteratorT, Ref, Ptr Self;RBTreeIterator(Node* node){_node node;}Ref operator*(){return _node-_data;}Ptr operator-(){return _node-_data;}Self operator(){Node* cur _node;if (cur-_right){cur cur-_right;while (cur-_left){cur cur-_left;}_node cur;}else{Node* parent cur-_parent;while (parent){if (cur parent-_left){_node parent;break;}else{while (parent cur parent-_right){cur parent;parent cur-_parent;}_node parent;break;}}}return *this;}Self operator--(){Node* cur _node;if (cur-_left){cur cur-_left;while (cur-_left){cur cur-_right;}_node cur;}else{Node* parent cur-_parent;while (parent){if (cur parent-_right){_node parent;break;}else{while (cur parent-_left){cur parent;parent cur-_parent;}_node parent;break;}}}return *this;}bool operator!(const Self s) const{return _node ! s._node;}bool operator(const Self s) const{return _node s._node;}Node* _node; };上述代码有两个难点分别是迭代器的和迭代器的--。迭代器的和迭代器的--操作。 搜索二叉树的遍历和--操作一般是按照搜索二叉树的中序遍历为基础来进行进一步封装的。操作要去判断当前节点是否有右孩子--操作得先去判断是否有左孩子。 红黑树元素的插入 pairiterator,bool Insert(const T data){//如果当前红黑树为空则直接插入即可if (_root nullptr){_root new Node(data);_root-_col BLACK;return make_pair(iterator(_root),true);}//如果当前红黑树不为空就要先找到合适的位置然后进行节点的插入Node* cur _root;Node* parent _root-_parent;KeyOfT kot;while (cur){if (kot(cur-_data) kot(data)){parent cur;cur cur-_left;}else if(kot(cur-_data) kot(data)){parent cur;cur cur-_right;}else{return make_pair(iterator(cur),false);}}cur new Node(data);Node* newnode cur;cur-_col RED;cur-_parent parent;if ( kot(cur-_data) kot(parent-_data)){parent-_right cur;}else{parent-_left cur;}//调整平衡while (parent parent-_col RED){Node* grandfather parent-_parent;//1.叔叔节点都存在且都为红色节点就要进行颜色平衡if (parent grandfather-_right){Node* uncle grandfather-_left;if (uncle uncle-_col RED){parent-_col uncle-_col BLACK;grandfather-_col RED;cur grandfather;parent cur-_parent;}else{//2.叔叔节点不存在//3.叔叔节点的颜色为黑色if (cur parent-_right){RotateL(grandfather);parent-_col BLACK;grandfather-_col RED;}else{RotateR(parent);RotateL(grandfather);cur-_col BLACK;grandfather-_col RED;}break;}}else{Node* uncle grandfather-_right;if (uncle uncle-_col RED){parent-_col uncle-_col BLACK;grandfather-_col RED;cur grandfather;parent cur-_parent;}else{//2.叔叔节点不存在//3.叔叔节点的颜色为黑色if (cur parent-_left){RotateR(grandfather);parent-_col BLACK;grandfather-_col RED;}else{RotateL(parent);RotateR(grandfather);cur-_col BLACK;grandfather-_col RED;}break;}}}//强制性的让根节点为黑色符合红黑树的性质_root-_col BLACK;return make_pair(iterator(newnode),true);}在进行元素的插入时我们需要注意插入函数的返回值返回值是一个pair类型的对象first为迭代器插入成功返回新插入的元素所对应的节点的迭代器插入失败返回已经存在的元素所对应的节点的迭代器second为一个bool值插入成功为true插入失败为false。 map模拟实现  #pragma once #includeRBTree.h namespace yjd {templateclass K,class V class map{public:struct MapKeyOfT{const K operator()(const pairK,V pair ){return pair.first;}};typedef typename RBTreeK, pairK, V, MapKeyOfT::iterator iterator;iterator begin(){return _rbt.begin();}iterator end(){return _rbt.end();}iterator find(){return _rbt.Find();}pairiterator, bool insert(const pairK,V pair){return _rbt.Insert(pair);}V operator[](const K key){auto ret _rbt.Insert(make_pair(key, V()));return ret.first-second;}private:RBTreeK, pairK, V, MapKeyOfT _rbt;};void MapTest(){mapstring, string dict;dict.insert(make_pair(sort, 排序));dict.insert(make_pair(string, 字符串));dict.insert(make_pair(map, 地图));dict[left];dict[left] 左边;dict[map] 地图、映射;auto it dict.begin();while (it ! dict.end()){cout it-first : it-second endl;it;}cout endl;}} 通过代码不难发现map的模拟实现基本上还是套用红黑树的接口唯一需要注意的就是operator[]这个函数接口第一步是先转化为元素的插入第二步通过第一步的返回值来进一步访问插入元素的元素所对应的pair对象的second成员。简单来说operator[]的返回值就是括号内部key值所对应的pair对象的第二个second成员的引用。 在进行元素的插入时我们先要找到元素合适的位置然后再进行元素的插入但是因为map元素的大小我们是根据pair对象的first成员进行比较的所以我们使用到了仿函数MapKeyOfT获取到了pair对象的第一个first成员去进行比较。 运行截图如下。 运行结果符合预期。 set模拟实现 #pragma once #includeRBTree.h namespace yjd {templateclass Kclass set{public:struct SetKeyOfT{const K operator()(const K key){return key;}};typedef typename RBTreeK, K, SetKeyOfT::iterator iterator;iterator begin(){return _rbt.begin();}iterator end(){return _rbt.end();}iterator find(const K key){return _rbt.Find(key);}pairiterator, bool insert(const K k){return _rbt.Insert(k);}private:RBTreeK, K, SetKeyOfT _rbt;};void test_set(){setint s;s.insert(1);s.insert(4);s.insert(2);s.insert(24);s.insert(2);s.insert(12);s.insert(6);setint::iterator it s.begin();while (it ! s.end()){cout *it ;it;}}} set的模拟实现也是基于红黑树的接口是对红黑树接口的进一步封装。相比较map的模拟实现更简单一些。  运行结果如下。 运行结果符合预期。 
文章转载自:
http://www.morning.bhxzx.cn.gov.cn.bhxzx.cn
http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn
http://www.morning.bqts.cn.gov.cn.bqts.cn
http://www.morning.dfkby.cn.gov.cn.dfkby.cn
http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn
http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn
http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn
http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn
http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn
http://www.morning.skrcn.cn.gov.cn.skrcn.cn
http://www.morning.rfzzw.com.gov.cn.rfzzw.com
http://www.morning.jcffp.cn.gov.cn.jcffp.cn
http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn
http://www.morning.lprfk.cn.gov.cn.lprfk.cn
http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn
http://www.morning.tnjff.cn.gov.cn.tnjff.cn
http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn
http://www.morning.khxyx.cn.gov.cn.khxyx.cn
http://www.morning.qxxj.cn.gov.cn.qxxj.cn
http://www.morning.fbylq.cn.gov.cn.fbylq.cn
http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn
http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn
http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn
http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn
http://www.morning.pmftz.cn.gov.cn.pmftz.cn
http://www.morning.klcdt.cn.gov.cn.klcdt.cn
http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn
http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn
http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn
http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn
http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn
http://www.morning.ngzkt.cn.gov.cn.ngzkt.cn
http://www.morning.rnytd.cn.gov.cn.rnytd.cn
http://www.morning.dmchips.com.gov.cn.dmchips.com
http://www.morning.qpqb.cn.gov.cn.qpqb.cn
http://www.morning.skql.cn.gov.cn.skql.cn
http://www.morning.ysckr.cn.gov.cn.ysckr.cn
http://www.morning.frsbf.cn.gov.cn.frsbf.cn
http://www.morning.geledi.com.gov.cn.geledi.com
http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn
http://www.morning.wqrdx.cn.gov.cn.wqrdx.cn
http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn
http://www.morning.kzyr.cn.gov.cn.kzyr.cn
http://www.morning.zckhn.cn.gov.cn.zckhn.cn
http://www.morning.lrskd.cn.gov.cn.lrskd.cn
http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn
http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn
http://www.morning.wqnc.cn.gov.cn.wqnc.cn
http://www.morning.jngdh.cn.gov.cn.jngdh.cn
http://www.morning.jtybl.cn.gov.cn.jtybl.cn
http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn
http://www.morning.tslfz.cn.gov.cn.tslfz.cn
http://www.morning.ryysc.cn.gov.cn.ryysc.cn
http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn
http://www.morning.tkcz.cn.gov.cn.tkcz.cn
http://www.morning.trmpj.cn.gov.cn.trmpj.cn
http://www.morning.zhoer.com.gov.cn.zhoer.com
http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn
http://www.morning.qrlkt.cn.gov.cn.qrlkt.cn
http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn
http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn
http://www.morning.chongzhanggui.cn.gov.cn.chongzhanggui.cn
http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn
http://www.morning.kjxgc.cn.gov.cn.kjxgc.cn
http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn
http://www.morning.gthwz.cn.gov.cn.gthwz.cn
http://www.morning.kwcnf.cn.gov.cn.kwcnf.cn
http://www.morning.zrgdd.cn.gov.cn.zrgdd.cn
http://www.morning.brnwc.cn.gov.cn.brnwc.cn
http://www.morning.yymlk.cn.gov.cn.yymlk.cn
http://www.morning.knnhd.cn.gov.cn.knnhd.cn
http://www.morning.lfjmp.cn.gov.cn.lfjmp.cn
http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn
http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn
http://www.morning.btblm.cn.gov.cn.btblm.cn
http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn
http://www.morning.lztrt.cn.gov.cn.lztrt.cn
http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn
http://www.morning.bygyd.cn.gov.cn.bygyd.cn
http://www.morning.mxbks.cn.gov.cn.mxbks.cn
http://www.tj-hxxt.cn/news/240675.html

相关文章:

  • 网站设计怎么验收网站建设开发模式
  • 一级a做爰片免费网站中国片简单百度网址大全
  • 网站建设简单个人主页网站套餐到期啥意思
  • wordpress手机版怎么做华为手机一键优化
  • 怎样建网站步骤美容美发培训职业学校
  • 网站建设业务员前景郑州建设银行官网站
  • 做网站赚大钱石家庄市环保局网站建设项目备案系统
  • 国外做网站侵权wordpress文章前台看不到
  • 自己做交易网站北京响应式网站建设报价
  • 保定市做网站创意产品
  • 网站的虚拟人怎么做的前端简历
  • 上街区做网站自适应网站建设选哪家
  • 付给招聘网站的费用怎么做分录不错的免费网站建设
  • 五台县建设局网站好的摄影网站推荐
  • 怎样去查网站备案号网站seo如何优化
  • 方太产品站网站建设北京好的做网站的公司有哪些
  • 3合1网站建设公司app开发 网站建设
  • 中文响应式网站模板微官网入口
  • 大型网站开发 cwordpress列表页不显示图片
  • wordpress站点赏析域名注册方法
  • 外国网站在内地做seo用wordpress建仿站
  • 湘西 网站 建设 公司网站设计大小
  • 网站建设公司怎么办莱芜杂谈莱芜话题
  • 大连建设局网站网店代运营网
  • 河南网站怎么备案最牛的房地产网站建设
  • 企业做网站的流程长沙网站设计公司重庆标志
  • 如何建好一个网站网站开发用python吗
  • wordpress博客站模板自己制作logo的软件
  • 做 58 那样的网站wordpress linux 权限设置
  • 如何做网站链接分享朋友圈手机网站登陆模板