目录
一、set和map在这里只是一个壳子,去调用红黑树里的各种接口
难点总结:
1、set中一个节点放一个值,而map是Key-Value结构,因此在红黑树内部比较的时候为了能够实现泛型,要用仿函数实现set直接取出节点的值比较,而对于map直接取出Key-Value中的Key进行比较(虽然pair支持比较,但是规则是先比较Key,再比较Value,与我们需求不同)
2、迭代器的实现:核心就是改变指针的指向,需要实现的运算符重载有()、* (主要是set在使用)、->(主要是map使用) 、++、!= 、==
3、关于是否可以修改,对于set来说,每个节点的值都不可以修改;对于map来说,first的值不允许修改,second可以修改。最简单的实现方法:将set的K设置为const K,将map的pair<K, V>设置为pair<const K, V>
一、set和map在这里只是一个壳子,去调用红黑树里的各种接口
这是set
namespace wjl
{
template<class K>
class set
{
//对map进行兼容
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
public:
typedef typename RBTree<const K, SetKeyOfT>::Iterator iterator;
typedef typename RBTree<const K, SetKeyOfT>::ConstIterator const_iterator;
//typedef只能作用于类,不能作用于对象和变量
//因为RBTree在这里还没有实例化,所以编译器在这里无法区分,
// 需要手动加上typename帮助编译器进行区分
iterator begin()
{
return _t.Begin();
}
const_iterator begin() const
{
return _t.Begin();
}
iterator end()
{
return _t.End();
}
const_iterator end() const
{
return _t.End();
}
bool insert(const K& key)
{
return _t.Insert(key);
}
private:
//使key不能被修改,以及注意要修改迭代器的参数
RBTree<const K, SetKeyOfT> _t;
};
}
这是map
namespace wjl
{
template<class K, class V>
class map
{
struct MapKeyOfT
{
const K& operator()(const pair<K, V>& kv)
{
return kv.first;
}
};
public:
typedef typename RBTree<pair<const K, V>, MapKeyOfT>::Iterator iterator;
typedef typename RBTree<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();
}
bool insert(const pair<K, V>& kv)
{
return _t.Insert(kv);
}
private:
//使first不能被修改,second可以被修改,以及注意修改迭代器,不然就成了两个不相同的类
RBTree<pair<const K, V>, MapKeyOfT> _t;
};
}
二、迭代器的实现
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;
}
Ptr operator->()//返回指向节点数据的指针
{
return &_node->_data;
}
bool operator!=(const Self& s) const
{
return _node != s._node;
}
bool operator== (const Self& s)const
{
return _node == s._node;
}
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 && cur == parent->_right)
{
cur = parent;
parent = cur->_parent;
}
_node = parent;
}
return *this;
}
};
三、红黑树的各种接口
template <class T>
class RBTreeNode
{
public:
T _data;
RBTreeNode<T>* _parent;
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
Colours _col;
RBTreeNode(const T& data)
: _data(data)
, _parent(nullptr)
, _left(nullptr)
, _right(nullptr)
{
}
};
template <class T, class KeyofT>
class RBTree
{
public:
typedef RBTreeIterator<T, T&, T*> Iterator;
typedef RBTreeIterator<T, const T&, const T*> ConstIterator;
typedef RBTreeNode<T> Node;
Iterator Begin()//不存在哨兵位
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return Iterator(cur);
}
Iterator End()//为了保证访问到最后一个节点,所以end是空
{
return Iterator(nullptr);
}
ConstIterator Begin() const//不存在哨兵位
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return ConstIterator(cur);
}
ConstIterator End() const//为了保证访问到最后一个节点,所以end是空
{
return ConstIterator(nullptr);
}
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
if (subLR != nullptr)
{
subLR->_parent = parent;
}
Node* pParent = parent->_parent;
subL->_right = parent;
parent->_parent = subL;
parent->_left = subLR;
//有可能最新的parent是根节点
if (parent == _root)
{
_root = subL;
subL->_parent = nullptr;
}
//有可能旋转的是一颗子树
else
{
if (pParent->_left == parent)
{
pParent->_left = subL;
}
else
{
pParent->_right = subL;
}
subL->_parent = pParent;
}
}
void RotateL(Node* parent)
{
Node* subR = parent->_right;
Node* subRL = subR->_left;
if (subRL != nullptr)
{
subRL->_parent = parent;
}
parent->_right = subRL;
Node* pParent = parent->_parent;
parent->_parent = subR;
subR->_left = parent;
if (parent == _root)
{
_root = subR;
subR->_parent = nullptr;
}
else
{
if (pParent->_left == parent)
{
pParent->_left = subR;
}
else
{
pParent->_right = subR;
}
subR->_parent = pParent;
}
}
bool Insert(const T& data)
{
if (_root == nullptr)
{
_root = new Node(data);
_root->_col = BLACK;
return true;
}
//取pair的first或者是Key本身
KeyOfT kot;
Node* parent = nullptr;
Node* cur = _root;
while (cur)
{
if (kot(cur->_data) < kot(data))//pair支持比较first小就小,再比second
{
parent = cur;
cur = cur->_right;
}
else if (kot(cur->_data) > kot(data))
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(data);
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;
// g
// p u
if (parent == grandfather->_left)
{
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
{
// g
// u p
// c
RotateR(parent);
RotateL(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
}
_root->_col = BLACK;
return true;
}
private:
Node* _root = nullptr;
};
1212

被折叠的 条评论
为什么被折叠?



