目录
[一. 源码及框架分析](#一. 源码及框架分析)
[二. 模拟实现set/map](#二. 模拟实现set/map)
[2.1 实现出复用红黑树的框架](#2.1 实现出复用红黑树的框架)
[2.1.1 实现仿函数KeyOfT](#2.1.1 实现仿函数KeyOfT)
[2.1.2 调整insert](#2.1.2 调整insert)
[2.2 迭代器的实现](#2.2 迭代器的实现)
[2.2.1 iterator实现思路分析](#2.2.1 iterator实现思路分析)
[2.2.2 Iterator代码实现](#2.2.2 Iterator代码实现)
[2.3 map中的operator[ ]](#2.3 map中的operator[ ])
[2.4 set的完整封装实现](#2.4 set的完整封装实现)
[2.5 map的完整封装实现](#2.5 map的完整封装实现)
[2.6 完整的红黑树模板类](#2.6 完整的红黑树模板类)
引言
在C++标准模板库(STL)中,std::set 和 std::map 是两个常用的关联容器,它们提供了高效的元素存储、查找和插入操作。这些容器的底层实现通常基于红黑树 (Red-Black Tree),一种自平衡的二叉搜索树,能够保证操作的时间复杂度为O(log N)。
一. 源码及框架分析
SGI-STL的map和set源代码主要分布在stl_map.h、stl_set.h和stl_tree.h等头文件中。核心框架如下:
- set 和 map 都复用了一个红黑树模板类 rb_tree。
- 对于 set,它存储的元素是键(Key),因此实例化 rb_tree 时,第二个模板参数为 Key,使用 identity 仿函数提取键。
- 对于 map,它存储键值对(pair<Key, T>),第二个模板参数为 pair<const Key, T>,使用 select1st 仿函数提取键。
源码片段:
cpp
// stl_set.h
template <class Key, class Compare = less<Key>, class Alloc = alloc>
class set {
public:
typedef Key key_type;
typedef Key value_type;
private:
typedef rb_tree<key_type, value_type, identity<value_type>, Compare, Alloc> rep_type;
rep_type t; // red-black tree representing set
};
// stl_map.h
template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>
class map {
public:
typedef Key key_type;
typedef T mapped_type;
typedef pair<const Key, T> value_type;
private:
typedef rb_tree<key_type, value_type, select1st<value_type>, Compare, Alloc> rep_type;
rep_type t; // red-black tree representing map
};
// stl_tree.h
struct __rb_tree_node_base
{
typedef __rb_tree_color_type color_type;
typedef __rb_tree_node_base* base_ptr;
color_type color;
base_ptr parent;
base_ptr left;
base_ptr right;
};
// stl_tree.h
template <class Key, class Value, class KeyOfValue, class Compare, class Alloc
= alloc>
class rb_tree {
protected:
typedef void* void_pointer;
typedef __rb_tree_node_base* base_ptr;
typedef __rb_tree_node<Value> rb_tree_node;
typedef rb_tree_node* link_type;
typedef Key key_type;
typedef Value value_type;
public:
// insert⽤的是第⼆个模板参数左形参
pair<iterator,bool> insert_unique(const value_type& x);
// erase和find⽤第⼀个模板参数做形参
size_type erase(const key_type& x);
iterator find(const key_type& x);
protected:
size_type node_count; // keeps track of size of tree
link_type header;
};
template <class Value>
struct __rb_tree_node : public __rb_tree_node_base
{
typedef __rb_tree_node<Value>* link_type;
Value value_field;
};
- rb_tree 的模板参数包括 Key(用于find/erase的参数)、Value(结点存储的数据类型)、KeyOfValue(键提取仿函数)。
- insert 使用 Value 类型参数,find 和 erase 使用 Key 类型参数。
- 这种设计允许红黑树既支持纯键搜索(set),也支持键值对搜索(map)。
- 通过分析,我们可以看到STL使用泛型编程巧妙地实现了代码复用。Value 决定了结点存储类型:对于set是Key,对于map是pair<const Key, T>。
问:rb_tree第二个模板参数Value已经控制了红黑树结点中存储的数据类型,为什么还要传第一个模板参数Key呢?尤其是set,两个模板参数是一样的
答:对于 map和set,find/erase时的函数参数都是Key,所以第一个模板参数是传给find/erase等函数做形参的类型的。对于set而言两个参数是一样的,但是对于map而言就完全不一样了,map insert的是pair对象,但是find和ease的是Key对象。
这样设计的意义:
- 将"用于查找的关键字类型"与"实际存储的数据类型"分离
- 使得同一个红黑树模板可以同时适配 set(T=K)和 map(T=pair<K,V>)
- 提供类型安全的查找接口
- 通过下图对框架的分析,我们可以看到源码中rb_tree用了一个巧妙的泛型思想实现,rb_tree是实 现key的搜索场景,还是key/value的搜索场景不是直接写死的,而是由第二个模板参数Value决定 _rb_tree_node中存储的数据类型。
- set实例化rb_tree时第二个模板参数给的是key,map实例化rb_tree时第二个模板参数给的是 pair,这样一颗红黑树既可以实现key搜索场景的set,也可以实现key/value搜索场景的map。

二. 模拟实现set/map
set/map模拟实现主要以下面的几个步骤完成:
- 实现红黑树(注:在上一篇文章中已经详细实现了红黑树,如有需要可查看红黑树的实现)
- 封装set/map框架,解决KeyOfT
- 迭代器Iterator的实现
- cosnt_iterator的实现
- key不支持修改的问题
- operator[ ]
2.1 实现出复用红黑树的框架
- 参考源码框架,map和set复用之前实现的红黑树。
- 这里相比源码调整一下,key参数就用K,value参数就用V,红黑树中的数据类型使用T。
- 其次因为RBTree实现了泛型不知道T参数导致是K,还是pair,那么insert内部进行插入逻辑比较时,就没办法进行比较,因为pair的默认支持的是key和value一起参与比较,我们需要的是任何时候只比较key,所以我们在map和set层分别实现⼀个MapKeyOfT和SetKeyOfT的仿函数传给 RBTree的KeyOfT,然后RBTree中通过KeyOfT仿函数取出T类型对象中的key,再进行比较,具体细节参考如下代码实现。
2.1.1 实现仿函数KeyOfT
cpp
namespace MySet
{
template<class K>
class set
{
// 内部类(把key取出来)
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
private:
// key的迭代器不能支持修改,把第二个模板参数改为const K
RBTree<K, const K, SetKeyOfT> _t;
};
}
cpp
namespace MyMap
{
template<class K,class V>
class map
{
// 内部类(把key取出来)
struct MapKeyOfT
{
const K& operator()(const pair<K, V>& kv)
{
return kv.first;
}
};
public:
pair<iterator, bool> insert(const pair<K, V>& kv)
{
return _t.Insert(kv);
}
private:
RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};
}
2.1.2 调整insert
cpp
enum Color
{
RED,
BLACK
};
template<class T>
struct RBTreeNode
{
T _data;
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
Color _col;
RBTreeNode(const T& data)
:_data(data)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _col(RED)
{}
};
// RBTree<K, K> _t; // set
// RBTree<K, pair<K, V>> _t; // map
template<class K, class T, class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
~RBTree()
{
Destroy(_root);
_root = nullptr;
}
pair<Iterator, bool> Insert(const T& data)
{
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK; // 根节点必须为黑色
return { Iterator(_root), true }; // 插入成功
}
KeyOfT kot; // 通过kot,把T类型的数据中的key取出来,然后在下面比较大小
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (kot(cur->_data) < kot(data)) // 如果data是key就用key比较大小可以;如果data是pair,用pair比较大小,不一定是我们想要的
{
parent = cur;
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else
{
return { Iterator(cur), false }; // 已经有这个值,插入失败
}
}
// 新增节点必须为红色
cur = new Node(data);
Node* newnode = cur; // cur在旋转时可能会变,用newnode保存一下
cur->_col = RED;
if (kot(parent->_data) < kot(data))
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (grandfather->_left == parent)
{
// g
// p u
//c
Node* uncle = grandfather->_right;
// 叔叔存在且为红
if (uncle && uncle->_col == RED)
{
// 变色+继续往上处理
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在或者叔叔存在且为黑
{
// g
// p u
//c
// 单旋+变色
if (cur == parent->_left)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// p u
// c
// 双旋+变色
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
// g
// p u
// c
Node* uncle = grandfather->_left;
// 叔叔存在且为红
if (uncle && uncle->_col == RED)
{
// 变色+继续往上处理
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在或者叔叔存在且为黑
{
// g
// p u
// c
// 单旋+变色
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// g
// p u
// c
// 双旋+变色
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
// 无论最后根节点是否为黑,就直接置为黑
_root->_col = BLACK;
return { Iterator(newnode), true };
}
private:
void Destroy(Node* root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
private:
Node* _root = nullptr;
};
2.2 迭代器的实现
2.2.1 iterator实现思路分析
- iterator实现的大框架跟list的iterator思路是一致的,用一个类型封装结点的指针,再通过重载运算符实现,迭代器像指针一样访问的行为。
- 这里的难点是operator++和operator--的实现。之前使用部分,我们分析了,map和set的迭代器走的是中序遍历,即左子树->根结点->右子树,那么begin()会返回中序第一个结点的iterator也就是10 所在结点的迭代器。
- 迭代器++的核心逻辑就是不看全局,只看局部,只考虑当前中序局部要访问的下一个结点。
- 迭代器++时,如果it指向的结点的子树不为空,代表当前结点已经访问完了,要访问下一个结点是右子树的中序第一个,一棵树中序第一个是最左结点,所以直接找右子树的最左结点即可。
- 迭代器++时,如果it指向的结点的右子树空,代表当前结点已经访问完了且当前结点所在的子树也 访问完了,要访问的下一个结点在当前结点的祖先里面,所以要沿着当前结点到根的祖先路径向上 找。
- 如果当前结点是父亲的左,根据中序左子树->根结点->右子树,那么下一个访问的结点就是当前结 点的父亲;如下图:it指向25,25右为空,25是30的左,所以下一个访问的结点就是30。
- 如果当前结点是父亲的右,根据中序左子树->根结点->右子树,当前当前结点所在的子树访问完 了,当前结点所在父亲的子树也访问完了,那么下一个访问的需要继续往根的祖先中去找,直到找 到孩子是父亲左的那个祖先就是中序要遍历的下一个结点。如下图:it指向15,15右为空,15是10 的右,15所在⼦树话访问完了,10所在子树也访问完了,继续往上找,10是18的左,那么下⼀个 访问的结点就是18。
- end()如何表示呢?如下图:当it指向50时,++it时,50是40的右,40是30的右,30是18的右,18 到根没有父亲,没有找到孩子是父亲左的那个祖先,这时父亲为空了,那我们就把it中的结点指针 置为nullptr,我们用nullptr去充当end。需要注意的是stl源码空,红黑树增加了一个哨兵位头结点 做为end(),这哨兵位头结点和根互为父亲,左指向最左结点,右指向最右结点。相比我们用nullptr作为end(),差别不大,他能实现的,我们也能实现。只是--end()判断到结点时空,特殊处理一下,让迭代器结点指向最右结点。具体参考迭代器--实现。
- 迭代器--的实现跟++的思路完全类似,逻辑正好反过来即可,因为他访问顺序是右子树->根结点-> 左子树,具体参考下面代码实现。
- set的iterator也不支持修改,我们把set的第二个模板参数改成const K即可, RBTree<K,const K, SetKeyOfT> _t;
- map的iterator不支持修改key但是可以修改value,我们把map的第二个模板参数pair的第一个参数改成const K即可, RBTree<K,pair<const K,V>, MapKeyOfT> _t;

2.2.2 Iterator代码实现
红黑树底层封装:
cpp
// 迭代器
template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef RBTreeIterator<T, Ref, Ptr> Self;
Node* _node;
RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
// map常用->访问数据
Ptr operator->()
{
return &_node->_data;
}
// 返回类型依旧是迭代器
Self& operator++()
{
// 右孩子不为空,找右孩子的最左节点
if (_node->_right)
{
Node* minRight = _node->_right;
while (minRight->_left)
{
minRight = minRight->_left;
}
_node = minRight;
}
// 右为空,找孩子是父亲左的祖先
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operator==(const Self& s)
{
return _node == s._node;
}
};
set封装:
cpp
namespace MySet
{
template<class K>
class set
{
public:
// 没有实例化,编译器无法确定这是类型还是静态成员变量
// 前面要加一个typename,告诉编译器这时一个类型
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();
}
private:
// key的迭代器不能支持修改,把第二个模板参数改为const K
RBTree<K, const K, SetKeyOfT> _t;
};
}
map封装:
cpp
namespace MyMap
{
template<class K,class V>
class map
{
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();
}
private:
RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};
}
2.3 map中的operator[ ]
map 支持下标访问 dict["key"],其底层逻辑非常巧妙。它依赖于 insert 函数的返回值 。
insert 返回的是 pair<iterator, bool>:
-
如果 Key 存在,返回
{指向该元素的迭代器, false}。 -
如果 Key 不存在,插入新节点,返回
{指向新节点的迭代器, true}。
利用这个特性,operator[] 的实现如下 :
cpp
V& operator[](const K& key) {
// 1. 调用 insert,无论 key 是否存在,ret.first 都会指向该节点
// pair<iterator, bool> ret = insert(make_pair(key, V()));
auto[it, flag] = _t.Insert({ key, V() }); // 结构化绑定 C++17
// 2. 返回该节点 value 的引用,以便用户读写
return ret.first->second;
}
2.4 set的完整封装实现
set的特点
- 存储的是Key值,用于快速查找
- 不允许重复元素
- 底层是红黑树,按Key有序存储
cpp
namespace MySet
{
template<class K>
class set
{
// 内部类(把key取出来)
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
public:
// 没有实例化,编译器无法确定这是类型还是静态成员变量
// 前面要加一个typename,告诉编译器这时一个类型
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();
}
pair<iterator, bool> insert(const K& key)
{
return _t.Insert(key);
}
iterator find(const K& key)
{
return _t.Find(key);
}
private:
// key的迭代器不能支持修改,把第二个模板参数改为const K
RBTree<K, const K, SetKeyOfT> _t;
};
}
2.5 map的完整封装实现
map的特点
- 存储键值对(Key-Value)
- 通过Key快速查找Value
- Key不允许重复,Value可以重复
- 支持通过operator[]访问元素
cpp
namespace MyMap
{
template<class K,class V>
class map
{
// 内部类(把key取出来)
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();
}
pair<iterator, bool> insert(const pair<K, V>& kv)
{
return _t.Insert(kv);
}
iterator find(const K& key)
{
return _t.Find(key);
}
// 插入+修改(因为返回的是引用,所以可以修改)
V& operator[](const K& key)
{
// pair<iterator, bool> ret = _t.Insert({ key, V() });
auto[it, flag] = _t.Insert({ key, V() });
return it->second;
}
private:
RBTree<K, pair<const K, V>, MapKeyOfT> _t;
};
}
2.6 完整的红黑树模板类
cpp
enum Color
{
RED,
BLACK
};
template<class T>
struct RBTreeNode
{
T _data;
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
Color _col;
RBTreeNode(const T& data)
:_data(data)
, _left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _col(RED)
{}
};
// RBTree<K, K> _t; // set
// RBTree<K, pair<K, V>> _t; // map
// 迭代器
template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
typedef RBTreeNode<T> Node;
typedef RBTreeIterator<T, Ref, Ptr> Self;
Node* _node;
RBTreeIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
// map常用->访问数据
Ptr operator->()
{
return &_node->_data;
}
// 返回类型依旧是迭代器
Self& operator++()
{
// 右孩子不为空,找右孩子的最左节点
if (_node->_right)
{
Node* minRight = _node->_right;
while (minRight->_left)
{
minRight = minRight->_left;
}
_node = minRight;
}
// 右为空,找孩子是父亲左的祖先
else
{
Node* cur = _node;
Node* parent = cur->_parent;
while (parent && cur == parent->_right)
{
cur = parent;
parent = parent->_parent;
}
_node = parent;
}
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
bool operator==(const Self& s)
{
return _node == s._node;
}
};
template<class K, class T, class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
typedef RBTreeIterator<T, T&, T*> Iterator;
typedef RBTreeIterator<T, const T&, const T*> ConstIterator;
~RBTree()
{
Destroy(_root);
_root = nullptr;
}
Iterator Begin()
{
// 最左节点
Node* minLeft = _root;
while (minLeft && minLeft->_left)
{
minLeft = minLeft->_left;
}
return Iterator(minLeft); // 用minLeft构造了一个迭代器
}
Iterator End()
{
return Iterator(nullptr); // 用nullptr充当end
}
ConstIterator Begin() const
{
// 最左节点
Node* minLeft = _root;
while (minLeft && minLeft->_left)
{
minLeft = minLeft->_left;
}
return ConstIterator(minLeft); // 用minLeft构造了一个迭代器
}
ConstIterator End() const
{
return ConstIterator(nullptr); // 用nullptr充当end
}
pair<Iterator, bool> Insert(const T& data)
{
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK; // 根节点必须为黑色
return { Iterator(_root), true }; // 插入成功
}
KeyOfT kot; // 通过kot,把T类型的数据中的key取出来,然后在下面比较大小
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (kot(cur->_data) < kot(data)) // 如果data是key就用key比较大小可以;如果data是pair,用pair比较大小,不一定是我们想要的
{
parent = cur;
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else
{
return { Iterator(cur), false }; // 已经有这个值,插入失败
}
}
// 新增节点必须为红色
cur = new Node(data);
Node* newnode = cur; // cur在旋转时可能会变,用newnode保存一下
cur->_col = RED;
if (kot(parent->_data) < kot(data))
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
while (parent && parent->_col == RED)
{
Node* grandfather = parent->_parent;
if (grandfather->_left == parent)
{
Node* uncle = grandfather->_right;
// 叔叔存在且为红
if (uncle && uncle->_col == RED)
{
// 变色+继续往上处理
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在或者叔叔存在且为黑
{
// 单旋+变色
if (cur == parent->_left)
{
RotateR(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// 双旋+变色
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
Node* uncle = grandfather->_left;
// 叔叔存在且为红
if (uncle && uncle->_col == RED)
{
// 变色+继续往上处理
parent->_col = uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
else // 叔叔不存在或者叔叔存在且为黑
{
// 单旋+变色
if (cur == parent->_right)
{
RotateL(grandfather);
parent->_col = BLACK;
grandfather->_col = RED;
}
else
{
// 双旋+变色
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
// 无论最后根节点是否为黑,就直接置为黑
_root->_col = BLACK;
return { Iterator(newnode), true };
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
parent->_left = subLR;
if (subLR)
subLR->_parent = parent;
Node* parentParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
else
{
if (parentParent->_left == parent)
{
parentParent->_left = subL;
}
else
{
parentParent->_right = subL;
}
subL->_parent = parentParent;
}
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
parent->_right = subRL;
if (subRL)
subRL->_parent = parent;
Node* parentParent = parent->_parent;
subR->_left = parent;
parent->_parent = subR;
if (parentParent == nullptr)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (parent == parentParent->_left)
{
parentParent->_left = subR;
}
else
{
parentParent->_right = subR;
}
subR->_parent = parentParent;
}
}
// 返回类型为迭代器,既可以查找也可以修改
Iterator Find(const K& key)
{
KeyOfT kot;
Node* cur = _root;
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();
}
private:
void Destroy(Node* root)
{
if (root == nullptr)
return;
Destroy(root->_left);
Destroy(root->_right);
delete root;
}
private:
Node* _root = nullptr;
};
结语
如有不足或改进之处,欢迎大家在评论区积极讨论,后续我也会持续更新C++相关的知识。文章制作不易,如果文章对你有帮助,就点赞收藏关注支持一下作者吧,让我们一起努力,共同进步!