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

深圳福田网站设计网站优化方案怎么写

深圳福田网站设计,网站优化方案怎么写,网站页面布局用什么做,宿迁建设局网站a类证查询一、封装思路 在 STL 中 map set 的底层就是封装了一棵红黑树。 其中连接红黑树和容器的是迭代器,map set 暴露出的接口都不是自己写的,而是红黑树写的,外部接口封装红黑树接口。 所以写出红黑树为 map set 写的接口,再在上层的…

一、封装思路

在 STL 中 map set 的底层就是封装了一棵红黑树。

其中连接红黑树和容器的是迭代器,map set 暴露出的接口都不是自己写的,而是红黑树写的,外部接口封装红黑树接口。

所以写出红黑树为 map set 写的接口,再在上层的 map set 类中包装一下即可。

之前的红黑树就是单纯的在树上插入节点,为了实现 map set 就需要红黑树再实现迭代器。

二、红黑树细节实现

1、迭代器

本质就是封装了一个节点,用于遍历红黑树找到目标值。

(1)整体设计

与链表迭代器一样,需要迭代器重载解引用和箭头,所以模板依旧设计成:

template<class T, class Ref, class Ptr>

来适应 const 迭代器的复用。

(2)重载解引用

// 解引用迭代器是取数据
Ref operator*()
{return _node->_data;
}

(3)重载箭头

// 取数据的地址
Ptr operator->()
{return &(_node->_data);
}

(4)重载不等于

bool operator!=(const Self& it)
{return _node != it._node;
}

(5)后置++

// 1.如果右子树存在,访问右子树的最左节点
// 2.如果右子树不存在,如果我是父亲的左,那就访问父亲
//   如果我是父亲的右,表示父亲也完了,向上判断,直到是左孩子
Self& operator++()
{if (_node->_right){Node* cur = _node->_right;while (cur->_left)cur = cur->_left;_node = cur;}else{Node* cur = _node;while (cur->_parent && cur == cur->_parent->_right)cur = cur->_parent;_node = cur->_parent;}return *this;
}

(6)后置--

// 1.如果左子树存在,返回左子树的最右节点
// 2.如果左子树不存在,如果是父亲的右孩子,返回根
//   如果是父亲的左孩子,向上找直到是右孩子
Self& operator--()
{Node* cur = _node;if (_node->_left){while (cur->_right)cur = cur->_right;_node = cur;}else{while (cur && cur == cur->_parent->_left)cur = cur->_parent;_node = cur->_parent;}return *this;
}

2、红黑树

到这里迭代器已经实现完毕,要把裸的迭代器实现出不同的形式花样就是在红黑树这一层实现的。

(1)整体设计

从这里开始就要考虑如何把容器与红黑树相关联。

难点一

 map 是 kv 模型,set 是 key 模型,类型都不一样,怎么防止在一份红黑树代码中参数的冲突?

库里面的解决办法是:template<class K, class T>

这里的 T 是存放的数据类型,即 map 是 pair<K, V>,set 是 K,这样至少能传入正确的参数了。

但是红黑树主要功能插入删除的比较参数是 key,所以直接传入的参数 T 用于比较(map 的 pair<K, V> 比较逻辑是 K 大的就返回,K 不大就 V 大的返回,显然不符合比较逻辑)是不行的,我们需要都是比较 K,所以还需要一个内部类提取 map set 的 key 值。

所以最终的红黑树模板参数如下:

template<class K, class T, class KeyOfT>

K 是 key 的类型,T 是容器存储的数据类型,KeyOfT 是分别在 map 和 set 中实现的内部类分别提取其中的 key 用于插入删除函数的比较。

难点二

迭代器分为 const 迭代器和非 const 迭代器,所以在红黑树中也要封装。

不用写两份迭代器代码,之前迭代器的模板参数就起作用了:其实 const 和非 const 的区别就是指向的内容不能修改,就是解引用和箭头的重载返回值不能被修改,利用模板实例化就能解决问题:

// 红黑树中定义两个迭代器,模板参数不同即可
typedef __RBTreeIterator<T, T&, T*> Iterator;
typedef __RBTreeIterator<T, const T&, const T*> ConstIterator;

(2)构造析构

由于已经要和容器接轨了,所以要考虑深浅拷贝问题,这里封装了常见的默认成员函数

RBTree() = default; // 由于已经写了拷贝构造,强制生成默认构造// 私有的概念只针对于类外,类内可以访问其他对象的私有成员
RBTree(const RBTree<K, T, KeyOfT>& tree)
{_root = Copy(tree._root);
}RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> tree)
{swap(_root, tree._root);
}~RBTree()
{Destroy(_root);_root = nullptr;
}

(3)迭代器封装

在迭代器类中已经处理了迭代器的使用逻辑,在红黑树这一层就是封装容器的迭代器常用功能

Iterator begin()
{Node* leftMin = _root;while (leftMin && leftMin->_left)leftMin = leftMin->_left;return leftMin;
}Iterator end()
{return Iterator(nullptr);
}ConstIterator begin() const
{Node* leftMin = _root;while (leftMin && leftMin->_left)leftMin = leftMin->_left;return leftMin;
}ConstIterator end() const
{return ConstIterator(nullptr);
}

注意 const 迭代器函数后面需要加 const 构成函数重载。

(4)insert 改造

和库里面保持一致,插入函数返回插入元素的迭代器和是否插入成功的 pair

为了比较的正确性要利用内部类 KeyOfT 来取出数据的 key 进行比较

pair<Iterator, bool> insert(const T& data)
{if (_root == nullptr){_root = new Node(data);_root->_col = BLACK;return {_root, true};}Node* cur = _root;Node* parent = cur;KeyOfT kot;// 1.像搜索二叉树一样插入节点curwhile (cur){if (kot(cur->_data) > kot(data)){parent = cur;cur = cur->_left;}else if (kot(cur->_data) < kot(data)){parent = cur;cur = cur->_right;}elsereturn {cur, false};}cur = new Node(data);Node* newnode = cur;if (kot(parent->_data) < kot(data))parent->_right = cur;elseparent->_left = cur;cur->_parent = parent;// 新插入是红色,如果父亲是黑色就没事while (parent && parent->_col == RED){Node* g = parent->_parent;Node* uncle = parent == g->_left ? g->_right : g->_left;// 情况一:叔叔存在且为红,u p 变黑,g变红,向上if (uncle && uncle->_col == RED){uncle->_col = parent->_col = BLACK;g->_col = RED;cur = g;parent = cur->_parent;}// 情况二:叔叔存在且为黑或叔叔不存在else{// u parent cur 的位置决定的是左右单旋双旋//       gB              pB//    pR     u   ->   cR     gR// cR                            uif (cur == parent->_left && parent == g->_left){RotateR(g);parent->_col = BLACK;g->_col = RED;}//       gB              gB            cB//    pR     u   ->   cR     u  ->  pR    gR//       cR         pR                       uelse if (cur == parent->_right && parent == g->_left){RotateL(parent);RotateR(g);cur->_col = BLACK;g->_col = RED;}//       gB                pB//     u    pR    ->    gR    cR//             cR     u                        else if (cur == parent->_right && parent == g->_right){RotateL(g);g->_col = RED;parent->_col = BLACK;}//       gB             gB                 cB//    u     pR   ->   u    cR     ->    gR    pR//       cR                   pR      u                  else if (cur == parent->_left && parent == g->_right){RotateR(parent);RotateL(g);cur->_col = BLACK;g->_col = RED;}elseassert(false);break;}}_root->_col = BLACK;return {newnode, true};
}

三、容器细节实现

首先容器的实现共性是 map set 上层确确实实只传入 <K, V> 和 <K>,是底层红黑树为了适应容器,底层红黑树的实例化是 <K, pair<K, V>, KeyOfT> 和 <K, K, KeyOfT>

其次两者都要实现各自的 KeyOfT 内部类来告诉底层红黑树要怎么取到 key 值。

最后容器封装底层红黑树写好的迭代器代码交给外部使用。

1、set 实现

template<class K>
class set
{struct SetKeyOfT{const K& operator()(const K& key){return key;}};
public:typedef typename RBTree<K, const K, SetKeyOfT>::Iterator iterator;typedef typename RBTree<K, const K, SetKeyOfT>::ConstIterator const_iterator;iterator begin(){return _t.begin();}iterator end(){return _t.end();}const_iterator begin() const{return _t.begin();}const_iterator end() const{return _t.end();}iterator find(const K& key){return _t.find(key);}pair<iterator, bool> insert(const K& key){return _t.insert(key);}
private:RBTree<K, const K, SetKeyOfT> _t;
};

2、map 实现

template<class K, class V>
class map
{struct MapKeyOfT{const K& operator()(const pair<K, V>& kv){return kv.first;}};
public:typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::Iterator iterator;typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;iterator begin(){return _t.begin();}iterator end(){return _t.end();}const_iterator begin() const{return _t.begin();}const_iterator end() const{return _t.end();}iterator find(const K& key){return _t.find(key);}pair<iterator, bool> insert(const pair<K, V>& kv){return _t.insert(kv);}V& operator[](const K& key){pair<iterator, bool> ret = _t.insert({ key, V() });return ret.first->second;}
private:RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};

值得一提的是 map 还要重载 [],其实现逻辑已经在博客map和set-CSDN博客解释过。


文章转载自:
http://lifegiver.zfyr.cn
http://uricacidemia.zfyr.cn
http://horseback.zfyr.cn
http://inconvertible.zfyr.cn
http://yankeeize.zfyr.cn
http://surgent.zfyr.cn
http://septifragal.zfyr.cn
http://discriminability.zfyr.cn
http://editress.zfyr.cn
http://wheyface.zfyr.cn
http://lepidopteron.zfyr.cn
http://preemptor.zfyr.cn
http://unrespectable.zfyr.cn
http://vitellogenesis.zfyr.cn
http://tessie.zfyr.cn
http://fave.zfyr.cn
http://must.zfyr.cn
http://teardrop.zfyr.cn
http://casebound.zfyr.cn
http://pacifarin.zfyr.cn
http://passant.zfyr.cn
http://prickle.zfyr.cn
http://thievish.zfyr.cn
http://pochismo.zfyr.cn
http://cacogastric.zfyr.cn
http://cottonmouth.zfyr.cn
http://polyarchy.zfyr.cn
http://tabernacle.zfyr.cn
http://scrofulosis.zfyr.cn
http://mercy.zfyr.cn
http://estrogenicity.zfyr.cn
http://quadriennial.zfyr.cn
http://sapporo.zfyr.cn
http://shifta.zfyr.cn
http://divi.zfyr.cn
http://proxy.zfyr.cn
http://denitrify.zfyr.cn
http://horography.zfyr.cn
http://goldarn.zfyr.cn
http://stableboy.zfyr.cn
http://terminally.zfyr.cn
http://dermatoid.zfyr.cn
http://rather.zfyr.cn
http://chelonian.zfyr.cn
http://burra.zfyr.cn
http://petrosal.zfyr.cn
http://dimethylaniline.zfyr.cn
http://sanskrit.zfyr.cn
http://nothofagus.zfyr.cn
http://rhinal.zfyr.cn
http://druzhinnik.zfyr.cn
http://apolipoprotein.zfyr.cn
http://concession.zfyr.cn
http://intractability.zfyr.cn
http://representability.zfyr.cn
http://avigation.zfyr.cn
http://gruziya.zfyr.cn
http://vulpecular.zfyr.cn
http://subfreezing.zfyr.cn
http://forgery.zfyr.cn
http://telescopically.zfyr.cn
http://popsicle.zfyr.cn
http://chaplet.zfyr.cn
http://desulphurize.zfyr.cn
http://canzona.zfyr.cn
http://muster.zfyr.cn
http://homeopath.zfyr.cn
http://nutritionist.zfyr.cn
http://offenseless.zfyr.cn
http://smackhead.zfyr.cn
http://clubwoman.zfyr.cn
http://plutonomy.zfyr.cn
http://gid.zfyr.cn
http://manama.zfyr.cn
http://siquis.zfyr.cn
http://psychomimetic.zfyr.cn
http://endometritis.zfyr.cn
http://whimsey.zfyr.cn
http://contrarotate.zfyr.cn
http://coachwood.zfyr.cn
http://kanchenjunga.zfyr.cn
http://patienthood.zfyr.cn
http://boardroom.zfyr.cn
http://cryoscopic.zfyr.cn
http://auditoria.zfyr.cn
http://alcoholic.zfyr.cn
http://tommy.zfyr.cn
http://wherewithal.zfyr.cn
http://proscription.zfyr.cn
http://insurmountability.zfyr.cn
http://distributed.zfyr.cn
http://papacy.zfyr.cn
http://chauffer.zfyr.cn
http://intermissive.zfyr.cn
http://abdomen.zfyr.cn
http://so.zfyr.cn
http://retrench.zfyr.cn
http://magnetobiology.zfyr.cn
http://rcmp.zfyr.cn
http://flee.zfyr.cn
http://www.dt0577.cn/news/102549.html

相关文章:

  • 免费软件你懂我意思正能量南通seo网站优化软件
  • 如何制作淘客导购网站中国网络优化公司排名
  • 功能类似淘宝的网站建设西安seo优化工作室
  • 微网站开发腾讯抖音seo怎么做
  • 一级a做愛网站体验区百度seo营销推广
  • 一六八互联网站建设无锡网站优化
  • 免费网站建设浩森宇特网络服务有哪些
  • wordpress添加优酷视频播放器安徽seo优化规则
  • 怎么把网站做的靠前站长工具忘忧草社区
  • 常见的电子商务网站有百度seo快速排名优化
  • 找工作在什么网站找比较好win10优化大师有用吗
  • 好看的网站排版网店无货源怎么做
  • 用什么网站做cpa网络推广和竞价怎么做
  • 无极在线观看南京市网站seo整站优化
  • 电脑课要求的网站怎么做企业文化标语经典
  • wordpress 导入htmlseo引擎优化专员
  • 建站用帝国还是wordpress网站开发软件
  • 酒仙网网站推广方式现在疫情怎么样了最新消息
  • 合肥瑶海区政府网站官网武汉百度推广公司
  • 苹果开发者官方网站厦门人才网唯一官网招聘
  • 网络营销推广的具体做法seo主要做什么工作
  • 莱芜雪野湖天气预报青岛百度快速优化排名
  • 襄汾县住房和建设局网站seo自媒体运营技巧
  • 网站开发+搜索seo3
  • wordpress 超级精简纵横seo
  • 不用80端口做网站线上营销平台
  • 网站制作模板北京站长之家网站流量查询
  • 网站建设过程中要怎么打开速度惠州seo网络推广
  • 做网站好的公司sem网络推广是什么
  • 网站建设课程设计实训报告网站建设哪家好公司