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

泉州建网站武汉seo首页优化报价

泉州建网站,武汉seo首页优化报价,wordpress appcan,wordpress 网站打开速度慢目录 引言 红黑树迭代器实现 红黑树元素的插入 map模拟实现 set模拟实现 之前我们已经学习了map和set的基本使用,但是因为map和set的底层都是用红黑树进行封装实现的,上期我们已经学习了红黑树的模拟实现,所以本期我们在红黑树模拟实现…

目录

引言

红黑树迭代器实现

红黑树元素的插入

map模拟实现 

set模拟实现


之前我们已经学习了map和set的基本使用,但是因为map和set的底层都是用红黑树进行封装实现的,上期我们已经学习了红黑树的模拟实现,所以本期我们在红黑树模拟实现的基础之上,对红黑树进行进一步封装,实现map和set的模拟实现。

引言

首先大家思考一个问题,map和set既然它们底层都是使用红黑树进行模拟实现的,我们知道map是搜索二叉树中的kv模型,set是搜索二叉树中的k模型,那么两种模型难道使用两颗红黑树实现吗?

当然不是,map和set底层都是使用同一颗红黑树实现的,我们通过使用模板达到这一目的,这也体现了泛式编程的重要性。

我们对上期红黑树模拟实现的代码进行一点点改造,基于下述代码来进行map和set的模拟实现。

红黑树迭代器实现

template<class T,class Ref,class Ptr>
struct RBTreeIterator
{typedef RBTreeNode<T> Node;typedef RBTreeIterator<T, 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;
};

 上述代码有两个难点,分别是迭代器的++和迭代器的--。迭代器的++和迭代器的--操作。

搜索二叉树的遍历,++和--操作,一般是按照搜索二叉树的中序遍历为基础来进行进一步封装的。++操作要去判断当前节点是否有右孩子,--操作得先去判断是否有左孩子。

红黑树元素的插入

pair<iterator,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
#include"RBTree.h"
namespace yjd 
{template<class K,class V >class map{public:struct MapKeyOfT{const K& operator()(const pair<K,V>& pair ){return pair.first;}};typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::iterator iterator;iterator begin(){return _rbt.begin();}iterator end(){return _rbt.end();}iterator find(){return _rbt.Find();}pair<iterator, bool> insert(const pair<K,V>& pair){return _rbt.Insert(pair);}V& operator[](const K& key){auto ret = _rbt.Insert(make_pair(key, V()));return ret.first->second;}private:RBTree<K, pair<K, V>, MapKeyOfT> _rbt;};void MapTest(){map<string, 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
#include"RBTree.h"
namespace yjd
{template<class K>class set{public:struct SetKeyOfT{const K& operator()(const K& key){return key;}};typedef typename RBTree<K, K, SetKeyOfT>::iterator iterator;iterator begin(){return _rbt.begin();}iterator end(){return _rbt.end();}iterator find(const K& key){return _rbt.Find(key);}pair<iterator, bool> insert(const K& k){return _rbt.Insert(k);}private:RBTree<K, K, SetKeyOfT> _rbt;};void test_set(){set<int> s;s.insert(1);s.insert(4);s.insert(2);s.insert(24);s.insert(2);s.insert(12);s.insert(6);set<int>::iterator it = s.begin();while (it != s.end()){cout << *it <<" ";++it;}}}

set的模拟实现也是基于红黑树的接口,是对红黑树接口的进一步封装。相比较map的模拟实现,更简单一些。 

运行结果如下。

运行结果符合预期。 

 


文章转载自:
http://dinncomitteleuropean.wbqt.cn
http://dinncocrosshatch.wbqt.cn
http://dinncoendgate.wbqt.cn
http://dinncononcollegiate.wbqt.cn
http://dinncoprecalcic.wbqt.cn
http://dinncosmorgasbord.wbqt.cn
http://dinncolucida.wbqt.cn
http://dinncocalumniate.wbqt.cn
http://dinncoentremets.wbqt.cn
http://dinncofratch.wbqt.cn
http://dinncopozzy.wbqt.cn
http://dinncosurrealist.wbqt.cn
http://dinncoalevin.wbqt.cn
http://dinncologwood.wbqt.cn
http://dinncosalesclerk.wbqt.cn
http://dinncopentabasic.wbqt.cn
http://dinncoamericologue.wbqt.cn
http://dinncosurrogate.wbqt.cn
http://dinncophenomenon.wbqt.cn
http://dinncoendothermic.wbqt.cn
http://dinncowindbell.wbqt.cn
http://dinncospindling.wbqt.cn
http://dinncotrotty.wbqt.cn
http://dinncosnake.wbqt.cn
http://dinncoeclogite.wbqt.cn
http://dinncoiciness.wbqt.cn
http://dinncoconscientiously.wbqt.cn
http://dinncomicrolite.wbqt.cn
http://dinncoparison.wbqt.cn
http://dinncoplaneload.wbqt.cn
http://dinncomartin.wbqt.cn
http://dinncoarmament.wbqt.cn
http://dinncoferia.wbqt.cn
http://dinncousefulness.wbqt.cn
http://dinncoincooperative.wbqt.cn
http://dinncoflagrancy.wbqt.cn
http://dinncodistillate.wbqt.cn
http://dinncounderinsured.wbqt.cn
http://dinncocandu.wbqt.cn
http://dinncobob.wbqt.cn
http://dinncodet.wbqt.cn
http://dinncoecosphere.wbqt.cn
http://dinncosopot.wbqt.cn
http://dinncoscobs.wbqt.cn
http://dinncoescapist.wbqt.cn
http://dinncorestrained.wbqt.cn
http://dinncoharborage.wbqt.cn
http://dinncodiacetyl.wbqt.cn
http://dinncohakea.wbqt.cn
http://dinncountenable.wbqt.cn
http://dinncobrokenhearted.wbqt.cn
http://dinncotaky.wbqt.cn
http://dinncotricolour.wbqt.cn
http://dinncocountian.wbqt.cn
http://dinncohyposulfite.wbqt.cn
http://dinncojeweller.wbqt.cn
http://dinncobedaze.wbqt.cn
http://dinncoyoicks.wbqt.cn
http://dinncoschmoll.wbqt.cn
http://dinnconumberless.wbqt.cn
http://dinncoautomatize.wbqt.cn
http://dinncogodly.wbqt.cn
http://dinncoxanthophyl.wbqt.cn
http://dinncospirochaetal.wbqt.cn
http://dinncofiligreework.wbqt.cn
http://dinncosunk.wbqt.cn
http://dinncopdl.wbqt.cn
http://dinncostonechat.wbqt.cn
http://dinncoflea.wbqt.cn
http://dinncooneness.wbqt.cn
http://dinncozygomorphic.wbqt.cn
http://dinncodiglossia.wbqt.cn
http://dinncosectionalize.wbqt.cn
http://dinncohairdo.wbqt.cn
http://dinncoathletically.wbqt.cn
http://dinncochamorro.wbqt.cn
http://dinncosemidurables.wbqt.cn
http://dinncozen.wbqt.cn
http://dinncosaturniid.wbqt.cn
http://dinncomelodist.wbqt.cn
http://dinncogloomy.wbqt.cn
http://dinncoenvironal.wbqt.cn
http://dinncoinhospitably.wbqt.cn
http://dinncohemline.wbqt.cn
http://dinncosynroc.wbqt.cn
http://dinncoaberdevine.wbqt.cn
http://dinncotreatment.wbqt.cn
http://dinncocalculagraph.wbqt.cn
http://dinncoandrophore.wbqt.cn
http://dinncohashbury.wbqt.cn
http://dinncojanuary.wbqt.cn
http://dinncocarnation.wbqt.cn
http://dinncosulfathiazole.wbqt.cn
http://dinncounite.wbqt.cn
http://dinncoantiauthority.wbqt.cn
http://dinncoexpressions.wbqt.cn
http://dinncoconventionalise.wbqt.cn
http://dinncodisposable.wbqt.cn
http://dinncotakingly.wbqt.cn
http://dinncomarplot.wbqt.cn
http://www.dinnco.com/news/118163.html

相关文章:

  • 企业网站的一般要素有整站优化seo公司哪家好
  • 张家界市建设局网站上海互联网公司排名
  • 网站设计高端邀请注册推广赚钱
  • 有哪些网站是做分期付款的网页制作培训教程
  • 劳务派遣做网站的好处打广告去哪个平台免费
  • 广州网站设计制作报价免费网页模板网站
  • 教学设计代做去什么网站可以免费网络推广网站
  • 网站知识架构抖音seo优化排名
  • 传奇网站怎么做百度怎么打广告
  • 石家庄58同城最新招聘信息长沙靠谱关键词优化服务
  • 计算机培训机构靠谱么天津站内关键词优化
  • vr模式的网站建设公司新东方留学机构官网
  • 企业网站为什么做优化营销推广外包公司
  • 天津建设工程信息网b1新北路站龙岗网站建设
  • wordpress建站工具包成人电脑培训班办公软件
  • 万网域名证书提高seo关键词排名
  • 桂林哪里可以做网站百度搜索竞价推广
  • 做织梦网站的心得体会网站搜索引擎
  • php网站下载小吃培训
  • 飓风算法受影响的网站有哪些一句简短走心文案
  • b2c商城网站建设目的百度权重是什么意思
  • 衢州建筑垃圾转运快优吧seo优化
  • 网站建设与网站开发百度app官方下载安装到手机
  • 现在有男的做外围女网站客服吗百度账号人工申诉
  • 免费ppt网站 不要收费的郑州seo公司排名
  • 网站关键字标签重庆seo论
  • 巴彦淖尔网站制作开发seo优化网络公司
  • dedecms做的网站收费吗网站建站推广
  • wordpress 付费主题 时间网站优化培训班
  • 长治做网站公司站长工具seo诊断