红黑树实现map,set封装
创始人
2025-05-30 13:07:51

目录

红黑树改造

 红黑树正向迭代器

 结构

operator*和operator-> 

operator++ 

operator-- 

operator!=和==

红黑树反向迭代器 

红黑树封装源码

map封装源码

set封装源码


红黑树改造

为了实现复用,我们将红黑树模板参数进行改造。 

T表示节点中所存储的数据类型 

template
struct RBNode
{RBNode* _left;RBNode* _right;RBNode* _parent;T _data;Color _col;RBNode(const T& data):_left(nullptr),_right(nullptr),_parent(nullptr),_data(data),_col(RED){}
};

K表示key的类型,而T表示节点中所存储的数据类型 

template
class RBTree
{typedef RBNode Node;
private:Node* _root;
};

那么第三个模板参数又是什么呢?

我们通过KeyofT实现仿函数,对其operator()运算符进行重载

struct MapKeyofT{const K& operator()(const pair& kv){return kv.first;}};
struct SetKeyofT{const K& operator()(const K& k){return k;}};

 

如果是set,kot(data)取出来的就是key

如果是map,kot(data)取出来的就是pair.first,也就是key的值

通过它我们可以在红黑树中实现key值的比较

map结构:

private:RBTree, MapKeyofT> _t;

 

set结构: 

private:
RBTree _t;

 红黑树正向迭代器

 结构

template
struct RBTreeIterator
{typedef RBNode Node;typedef RBTreeIterator self;Node* _node;RBTreeIterator(Node* node):_node(node){}
};

operator*和operator-> 

 

Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}

operator++ 

++就是要寻找中序遍历的下一个值,如果寻找呢?

如果右子树为空,那么就找孩子是父亲左的那个祖先节点

如果右子树不为空,那么就找右子树的最左节点 

self& operator++(){if (_node->_right){//找右子树的最左节点Node* min = _node->_right;while (min->_left){min = min->_left;}_node = min;}else{//找孩子是父亲左的那个祖先节点Node* cur = _node;Node* parent = cur->_parent;//父亲可能不存在while (parent && parent->_right == cur){cur = parent;parent = parent->_parent;}_node = parent;}return *this;}self operator++(int){self tmp(*this);++(*this);return tmp;}

 

operator-- 

--的思路与++正好相反

如果左子树为空,那么就找孩子是父亲右的那个祖先节点

如果左子树不为空,那么就找左子树的最右节点 

self& operator--(){if (_node->_left){//找左子树的最右节点Node* max = _node->_left;while (max->_right){max = max->_right;}_node = max;}else{//找孩子是父亲右的那个祖先节点Node* cur = _node;Node* parent = cur->_parent;while (parent && parent->_left == cur){cur = parent;parent = parent->_parent;}_node = parent;}}self operator--(int){self tmp(*this);--(*this);return tmp;}

operator!=和==

 直接使用节点地址比较

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

红黑树反向迭代器 

template
class _reverse_iterator
{
public:typedef _reverse_iterator self;_reverse_iterator(Iterator it):_it(it){}Ref operator*(){return *_it;}Ptr operator->(){return &(operator*());}bool operator==(const _reverse_iterator& rit)const{return _it == rit._it;}bool operator!=(const _reverse_iterator& rit)const{return _it != rit._it;}self& operator++(){--_it;return *this;}self operator++(int){self tmp(*this);--_it;return tmp;}self& operator--(){_it++;return *this;}self operator--(int){self tmp(*this);_it++;return tmp;}
private:Iterator _it;
};

红黑树封装源码

template
class RBTree
{typedef RBNode Node;
public:typedef RBTreeIterator iterator;typedef RBTreeIterator const_iterator;typedef _reverse_iterator reverse_iterator;typedef _reverse_iterator const_reverse_iterator;reverse_iterator rbegin(){//寻找最右节点Node* max = _root;while (max && max->_right){max = max->_right;}return reverse_iterator(iterator(max));}reverse_iterator rend(){return reverse_iterator(iterator(nullptr));}const_reverse_iterator rbegin()const{Node* max = _root;while (max && max->_right){max = max->_right;}return const_reverse_iterator(const_iterator(max));}const_reverse_iterator rend()const{return const_reverse_iterator(const_iterator(nullptr));}iterator begin(){Node* min = _root;while (min&&min->_left){min = min->_left;}return iterator(min);}iterator end(){return iterator(nullptr);}const_iterator begin()const{Node* min = _root;while (min&&min->_left){min = min->_left;}return const_iterator(min);}const_iterator end()const{return const_iterator(nullptr);}RBTree():_root(nullptr){}RBTree(const RBTree& t){_root = Copy(t._root);}RBTree& operator=(RBTree t){swap(_root, t._root);return *this;}~RBTree(){Destory(_root);_root = nullptr;}iterator Find(const K& key){Node* cur = _root;KeyofT kot;while (cur){if (kot(cur->_data) < key){cur = cur->_right;}else if (kot(cur->_data) > key){cur = cur->_left;}else{return iterator(cur);}}return end();}const_iterator Find(const K& key)const{Node* cur = _root;KeyofT kot;while (cur){if (kot(cur->_data) < key){cur = cur->_right;}else if (kot(cur->_data) > key){cur = cur->_left;}else{return const_iterator(cur);}}return end();}pair Insert(const T& data){if (_root == nullptr){_root = new Node(data);_root->_col = BLACK;return make_pair(iterator(_root), true);}KeyofT kot;//先插入节点Node* parent = nullptr;Node* cur = _root;while (cur){if (kot(cur->_data) < kot(data)){parent = cur;cur = cur->_right;}else if(kot(cur->_data) > kot(data)){parent = cur;cur = cur->_left;}else{return make_pair(iterator(cur), false);}}cur = new Node(data);Node* ret = cur;if (kot(parent->_data) < kot(data)){parent->_right = cur;cur->_parent = parent;}else{parent->_left = cur;cur->_parent = parent;}//开始调整while (parent && parent->_col==RED)//当父亲为红才需要调整{Node* grandfather = parent->_parent;//grandfather一定存在if (grandfather->_left == parent){Node* uncle = grandfather->_right;//1.uncle存在且为红,p,u变黑,g变红,向上调整if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;cur = grandfather;parent = cur->_parent;}//2.uncle不存在或为黑,此时进行旋转else{Node* uncle = grandfather->_right;//单纯左边高//     g//  p//cur//此时对g进行右旋,p变黑,g变红if (parent->_left == cur){RoateR(grandfather);parent->_col = BLACK;grandfather->_col = RED;}//   g//p//   cur//此时先对p进行左旋,再对g进行右旋//cur变黑,g变红else{RoateL(parent);RoateR(grandfather);cur->_col = BLACK;grandfather->_col = RED;}break;}}else{Node* uncle = grandfather->_left;//1.uncle存在且为红,p,u变黑,g变红,向上调整if (uncle && uncle->_col == RED){parent->_col = uncle->_col = BLACK;grandfather->_col = RED;cur = grandfather;parent = cur->_parent;}//2.uncle不存在或为黑else{//单纯右边高//   g//      p//        cur//对g进行左旋,g变红,p变黑if (parent->_right == cur){RoateL(grandfather);parent->_col = BLACK;grandfather->_col = RED;}////   g//      p//   cur// 此时先对p进行右旋,再对g进行左旋,cur变黑,g变红else{RoateR(parent);RoateL(grandfather);cur->_col = BLACK;grandfather->_col = RED;}break;}}}_root->_col = BLACK;return make_pair(iterator(ret), true);}
private:Node* Copy(Node* root){if (root == nullptr){return nullptr;}Node* newroot = new Node(root->_data);newroot->_left = Copy(root->_left);newroot->_right = Copy(root->_right);if (newroot->_left){newroot->_left->_parent = newroot;}if (newroot->_right){newroot->_right->_parent = newroot;}return newroot;}void Destory(Node* root){if (root == nullptr){return;}Destory(root->_left);Destory(root->_right);delete root;}void RoateL(Node* parent){//更改链接关系Node* parentparent = parent->_parent;Node* subR = parent->_right;Node* subRL = subR->_left;parent->_right = subRL;if (subRL){subRL->_parent = parent;}subR->_left = parent;parent->_parent = subR;if (parentparent == nullptr){_root = subR;subR->_parent = nullptr;}else{if (parentparent->_left == parent){parentparent->_left = subR;subR->_parent = parentparent;}else{parentparent->_right = subR;subR->_parent = parentparent;}}}void RoateR(Node* parent){//更改链接关系Node* parentparent = parent->_parent;Node* subL = parent->_left;Node* subLR = subL->_right;parent->_left = subLR;if (subLR){subLR->_parent = parent;}subL->_right = parent;parent->_parent = subL;if (parentparent == nullptr){_root = subL;subL->_parent = nullptr;}else{if (parentparent->_left == parent){parentparent->_left = subL;subL->_parent = parentparent;}else{parentparent->_right = subL;subL->_parent = parentparent;}}}Node* _root;
};

map封装源码

 

templateclass map{public:struct MapKeyofT{const K& operator()(const pair& kv){return kv.first;}};typedef typename RBTree, MapKeyofT>::iterator iterator;typedef typename RBTree, MapKeyofT>::const_iterator const_iterator;typedef typename RBTree, MapKeyofT>::reverse_iterator reverse_iterator;typedef typename RBTree, MapKeyofT>::const_reverse_iterator const_reverse_iterator;reverse_iterator rbegin(){return reverse_iterator(end());}reverse_iterator rend(){return reverse_iterator(begin());}const_reverse_iterator rbegin()const{return const_reverse_iterator(end());}const_reverse_iterator rend()const{return const_reverse_iterator(begin());}iterator begin(){return _t.begin();}iterator end(){return _t.end();}const_iterator begin()const{return _t.begin();}const_iterator end()const{return _t.end();}pair insert(const pair kv){return _t.Insert(kv);}V& operator[](const K& key){auto ret = _t.Insert(make_pair(key, V()));return ret.first->second;}iterator Find(const K& key){return _t.Find(key);}const_iterator Find(const K& key)const{return _t.Find(key);}private:RBTree, MapKeyofT> _t;};

set封装源码

set的key值不能改变,所以使用const_iterator作为set的iterator 

templateclass set{public:struct SetKeyofT{const K& operator()(const K& k){return k;}};typedef typename RBTree::const_iterator iterator;typedef typename RBTree::const_reverse_iterator reverse_iterator;reverse_iterator rbegin()const{return _t.rbegin();}reverse_iterator rend()const{return _t.rend();}iterator begin()const{return _t.begin();}iterator end()const{return _t.end();}pair Insert(const K& key){auto ret= _t.Insert(key);return pair(iterator(ret.first._node), ret.second);}iterator Find(const K& key)const{return _t.Find(key);}private:RBTree _t;};

 

 

相关内容

热门资讯

数字操作方法 系列文章目录 前端系列文章——传送门 JavaScript系列文章——传送门 文章目录系列文章目录...
Cartesi 2023 年 ... 查看 Cartesi Machine、Cartesi Rollups 和 Noether 的更新正在...
JavaWeb——jsp概述入... JSP定义:  在如下一个jsp文件里面有如下的代码  <%@ page content...
一切喜怒哀乐都来自于你的认知 01 有个学子,准备出国,父母请来清华的教授宁向东。请问教授࿱...
JAVA并发编程——synch... 引言         Java语言为了解决并发编程中存在的原子性、可见性和有序性问题,...
git学习----3.21 未... 文章目录前言Git :一个分布式版本控制工具目标一、概述1.1 开发中的实际场景1.2...
Qt优秀开源项目之十七:QtP... QtPromise是Promises/A+规范的Qt/C++实现。该规范的译...
【前端八股文】JavaScri... 文章目录Set概念与arr的比较属性和方法并集、交集、差集Map概念属性和方法String用索引值和...
海康硬盘录像机接入RTSP/o... EasyNVR安防视频云服务平台可支持设备通过RTSP/Onvif协议接入平台,能提供...
在混合劳动力时代如何避免网络安... 在混合劳动力时代如何避免安全网络风险 三年多来,混合工作一直是工作生活中不可或缺的一...
2023还不懂Jmeter接口... 这里介绍的Jmeter接口测试的的实战,如果文章内容没遇看懂的话,我这边...
基于4G/5G弱网聚合的多链路... 基于4G/5G多卡聚合(弱网聚合)的智能融合通信设备技术亮点 增强带宽提供可靠连接 通过将多个有线和...
如何使用Synplify综合v... 文章目录使用Synplify综合的好处synplify的教程方法1(无效)...
2023年全国最新高校辅导员精... 百分百题库提供高校辅导员考试试题、辅导员考试预测题、高校辅导员考试真题、辅导员证考试题库等ÿ...
2022年18个值得期待的Le... 有数百个独特的LearnDash附加组件,您可能很难选择您的LearnDash LMS...
【java基础】Stream流... 文章目录基本介绍流的创建流的各种常见操作forEach方法filter方法map方法peek方法fl...
javaweb高校行政办公自动... 本课题基于我国高校管理信息化建设现状,结合在实际工作中所遇到的问题和收获,...
一款专门为自动化测试打造的集成... 你好,我是不二。 随着行业内卷越来越严重,自动化测试已成为测试工程师的...
【go-zero】golang... 一、casbin 概览 1、casbin基本了解 casbin的GitHub:https://git...
现在开发低代码平台算晚吗? 现在开发低代码平台算晚吗?作为低代码的亲戚——零代码厂商,这篇就以“厂商...
【JavaWeb】书城项目(2... 222.书城项目-第三阶段:修改所有html页面为jsp页面 改成jsp页面之后&#x...
基于jeecgboot的大屏设...      通过前面设计好数据源后,就要进行数据集的设计了。      一、还是在onl...
Linux命令小技巧:显示文件... 工作中会有很多千奇百怪的需求,比如:如何在 Linux 命令行中快速找到...
【找工作】-- 大数据工程师找... 目录 1.前言 2.找工作的理论知识 2.1 分析个人特征 2.1.1 你自身优势是什么?
C++基础算法④——排序算法(... 排序算法 1.插入排序 2.桶排序 1.插入排序 基本思想:将初始数据分为有序部分和...
nginx快速入门.跟学B站n... nginx快速入门.跟学B站nginx一小时精讲课程笔记nginx简介及环境准备nginx简介环境准...
ORACLE存过互相调用之间事... 今天在问答区看到一个问题是 假如有procedureA、procedureB和procedureC&...
基于java中Springbo... 基于java中Springboot框影视影院订票选座管理系统 开发语言:Java 框...
CVE-2018-18086 最近闲来无事,看到青少年CTF平台,感觉对新手还是比较友好的࿰...
【深度学习】基于Hough变化... 💥💥💞💞欢迎来到本博客❤️❤️&#x...