目录
前言
本文的代码是基于前一篇文章的红黑树的代码来封装map、set。
一、map、set的框架搭建
代码:
map:
namespace Ting
{
template<class K,class V>
class map
{
public:
struct KeyOfT
{
K& operator()(const pair<K,V>& p)
{
return p.first;
}
};
private:
RBTree<K, pair<K,V>, KeyOfT> _t;
};
}
set:
namespace Ting
{
template<class K>
class set
{
public:
struct KeyOfT
{
K& operator()(const K& key)
{
return key;
}
};
private:
RBTree<K, K,KeyOfT> _t;
};
}
二、map、set的迭代器的封装
2.1、map、set的迭代器的初步封装
首先先对树的迭代器进行代码编写
template<class T>
class __TreeIterator
{
typedef __TreeIterator<T> self;
public:
typedef RBTreeNode<T> Node;
__TreeIterator(Node* node)
:_node(node)
{}
self& operator++()
{
if (_node->_right)
{
//找node右子树的最左结点
Node* cur = _node->_right;
while (cur->_left)
{
cur = cur->_left;
}
_node = cur;
}
else
{
Node* cur = _node;
Node* parent = _node->_parent;
while (parent)
{
if (cur == parent->_left)
{
break;
}
else
{
cur = parent;
parent = parent->_parent;
}
}
_node = parent;
}
return *this;
}
T* operator->()
{
return &_node->_date;
}
T& operator*()
{
return _node->_date;
}
bool operator!=(const self& s)
{
return _node != s._node;
}
private:
Node* _node;
};
template<class K,class T,class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
typedef __TreeIterator<T> iterator;
RBTree()
:_root(nullptr)
{}
iterator begin()
{
Node* cur = _root;
while (cur&&cur->_left)
{
cur = cur->_left;
}
return iterator(cur);
}
iterator end()
{
return iterator(nullptr);
}
};
map
#pragma once
#include"RBTree.h"
namespace Ting
{
template<class K,class V>
class map
{
public:
struct KeyOfT
{
K operator()(const pair<K,V>& p)
{
return p.first;
}
};
typedef typename RBTree<K, pair<K, V>, KeyOfT>::iterator iterator;
iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
bool insert(const pair<K, V>& p)
{
return _t.insert(p);
}
private:
RBTree<K, pair<K,V>, KeyOfT> _t;
};
}
set
#pragma once
#include"RBTree.h"
namespace Ting
{
template<class K>
class set
{
public:
struct KeyOfT
{
K operator()(const K& key)
{
return key;
}
};
typedef typename RBTree<K, K, KeyOfT>::iterator iterator;
iterator begin()
{
return _t.begin();
}
iterator end()
{
return _t.end();
}
bool insert(const K& key)
{
return _t.insert(key);
}
private:
RBTree<K, K,KeyOfT> _t;
};
}
2.2、map、set的const迭代器的封装
2.2.1、set的const迭代器的封装
目标:set的key不能被修改
第一步:先利用模板参数设计出树的const的迭代器
template<class T,class Ptr,class Ref>
class __TreeIterator
{
typedef __TreeIterator<T,Ptr,Ref> self;
public:
typedef RBTreeNode<T> Node;
__TreeIterator(Node* node)
:_node(node)
{}
self& operator++()
{
if (_node->_right)
{
//找node右子树的最左结点
Node* cur = _node->_right;
while (cur->_left)
{
cur = cur->_left;
}
_node = cur;
}
else
{
Node* cur = _node;
Node* parent = _node->_parent;
while (parent)
{
if (cur == parent->_left)
{
break;
}
else
{
cur = parent;
parent = parent->_parent;
}
}
_node = parent;
}
return *this;
}
Ptr operator->()
{
return &_node->_date;
}
Ref operator*()
{
return _node->_date;
}
bool operator!=(const self& s)
{
return _node != s._node;
}
private:
Node* _node;
};
第二步: begin()、end()
typedef __TreeIterator<T, T*, T&> iterator;
typedef __TreeIterator<T, const T*, const T&> const_iterator;
RBTree()
:_root(nullptr)
{}
iterator begin()
{
Node* cur = _root;
while (cur&&cur->_left)
{
cur = cur->_left;
}
return iterator(cur);
}
iterator end()
{
return iterator(nullptr);
}
const_iterator begin() const
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return const_iterator(cur);
}
const_iterator end() const
{
return const_iterator(nullptr);
}
第三步: 很关键也很巧妙
2.2.2、map的const迭代器的封装
#pragma once
#include"RBTree.h"
namespace Ting
{
template<class K,class V>
class map
{
public:
struct KeyOfT
{
K operator()(const pair<K,V>& p)
{
return p.first;
}
};
typedef typename RBTree<K, pair<const K, V>, KeyOfT>::iterator iterator;
typedef typename RBTree<K, pair<const K, V>, KeyOfT>::const_iterator 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>& p)
{
return _t.insert(p);
}
private:
RBTree<K, pair<const K,V>, KeyOfT> _t;
};
}
三、operator[ ]
**第一步:**对RBTree中插入的返回值类型进行修改
pair<iterator,bool> insert(const T& date)
{
if (_root == nullptr)
{
_root = new Node(date);
_root->_col = BLACK;
return make_pair(_root,true);
}
KeyOfT kt;
Node* parent = _root;
Node* cur = _root;
while (cur)
{
if (kt(cur->_date)> kt(date))
{
parent = cur;
cur = cur->_left;
}
else if ((kt(cur->_date) < kt(date)))
{
parent = cur;
cur = cur->_right;
}
else
{
return make_pair(cur, false);
}
}
cur = new Node(date);
Node* newnode = cur;
if (kt(parent->_date) > kt(date))
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
while (parent&&parent->_col==RED)
{
Node* grandfather = parent->_parent;
//叔叔在左
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
//叔叔存在且叔叔为红色
if (uncle && uncle->_col == RED)
{
//变色加向上调整
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
//叔叔不存在或者叔叔是黑色
else
{
if (cur == parent->_left)
{
RotateR(grandfather);
grandfather->_col = RED;
parent->_col = BLACK;
}
else
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
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 make_pair(newnode,true);
}
然后该完后会发现编译报错。如图:
原因是:
**解决办法:**给迭代器写个构造函数让迭代器支持普通迭代器构造const迭代器
V& operator[](const K& key)
{
pair < iterator, bool > ret= _t.insert(make_pair(key, V()));
return ret.first->second;
}
四、代码实现
MySet.h
#pragma once
#include"RBTree.h"
namespace Ting
{
template<class K>
class set
{
public:
struct KeyOfT
{
K operator()(const K& key)
{
return key;
}
};
typedef typename RBTree<K, K, KeyOfT>::const_iterator iterator;
typedef typename RBTree<K, K, KeyOfT>::const_iterator const_iterator;
iterator begin() const
{
return _t.begin();
}
iterator end() const
{
return _t.end();
}
pair<iterator,bool> insert(const K& key)
{
pair<typename RBTree<K, K, KeyOfT>::iterator, bool> ret = _t.insert(key);
return make_pair(ret.first, ret.second);
}
private:
RBTree<K, K,KeyOfT> _t;
};
}
MyMap.h
#pragma once
#include"RBTree.h"
namespace Ting
{
template<class K,class V>
class map
{
public:
struct KeyOfT
{
K operator()(const pair<K,V>& p)
{
return p.first;
}
};
typedef typename RBTree<K, pair<const K, V>, KeyOfT>::iterator iterator;
typedef typename RBTree<K, pair<const K, V>, KeyOfT>::const_iterator 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>& p)
{
return _t.insert(p);
}
V& operator[](const K& key)
{
pair < iterator, bool > ret= _t.insert(make_pair(key, V()));
return ret.first->second;
}
private:
RBTree<K, pair<const K,V>, KeyOfT> _t;
};
}
RBTree.h
#pragma once
#include<iostream>
using namespace std;
enum Colour
{
RED,
BLACK
};
template<class T>
struct RBTreeNode
{
RBTreeNode<T>* _left;
RBTreeNode<T>* _right;
RBTreeNode<T>* _parent;
T _date;
Colour _col;
RBTreeNode(const T& date)
:_left(nullptr)
, _right(nullptr)
, _parent(nullptr)
, _date(date)
, _col(RED)
{}
};
template<class T,class Ptr,class Ref>
struct __TreeIterator
{
typedef __TreeIterator<T, Ptr, Ref> self;
typedef __TreeIterator<T, T*, T&> Iterator;
typedef RBTreeNode<T> Node;
__TreeIterator(const Iterator& it)
:_node(it._node)
{}
__TreeIterator(Node* node)
:_node(node)
{}
self& operator++()
{
if (_node->_right)
{
//找node右子树的最左结点
Node* cur = _node->_right;
while (cur->_left)
{
cur = cur->_left;
}
_node = cur;
}
else
{
Node* cur = _node;
Node* parent = _node->_parent;
while (parent)
{
if (cur == parent->_left)
{
break;
}
else
{
cur = parent;
parent = parent->_parent;
}
}
_node = parent;
}
return *this;
}
Ptr operator->()
{
return &_node->_date;
}
Ref operator*()
{
return _node->_date;
}
bool operator!=(const self& s)
{
return _node != s._node;
}
Node* _node;
};
template<class K,class T,class KeyOfT>
class RBTree
{
typedef RBTreeNode<T> Node;
public:
typedef __TreeIterator<T, T*, T&> iterator;
typedef __TreeIterator<T, const T*, const T&> const_iterator;
RBTree()
:_root(nullptr)
{}
iterator begin()
{
Node* cur = _root;
while (cur&&cur->_left)
{
cur = cur->_left;
}
return iterator(cur);
}
iterator end()
{
return iterator(nullptr);
}
const_iterator begin() const
{
Node* cur = _root;
while (cur && cur->_left)
{
cur = cur->_left;
}
return const_iterator(cur);
}
const_iterator end() const
{
return const_iterator(nullptr);
}
pair<iterator,bool> insert(const T& date)
{
if (_root == nullptr)
{
_root = new Node(date);
_root->_col = BLACK;
return make_pair(_root,true);
}
KeyOfT kt;
Node* parent = _root;
Node* cur = _root;
while (cur)
{
if (kt(cur->_date)> kt(date))
{
parent = cur;
cur = cur->_left;
}
else if ((kt(cur->_date) < kt(date)))
{
parent = cur;
cur = cur->_right;
}
else
{
return make_pair(cur, false);
}
}
cur = new Node(date);
Node* newnode = cur;
if (kt(parent->_date) > kt(date))
{
parent->_left = cur;
}
else
{
parent->_right = cur;
}
cur->_parent = parent;
while (parent&&parent->_col==RED)
{
Node* grandfather = parent->_parent;
//叔叔在左
if (parent == grandfather->_left)
{
Node* uncle = grandfather->_right;
//叔叔存在且叔叔为红色
if (uncle && uncle->_col == RED)
{
//变色加向上调整
parent->_col = BLACK;
uncle->_col = BLACK;
grandfather->_col = RED;
cur = grandfather;
parent = cur->_parent;
}
//叔叔不存在或者叔叔是黑色
else
{
if (cur == parent->_left)
{
RotateR(grandfather);
grandfather->_col = RED;
parent->_col = BLACK;
}
else
{
RotateL(parent);
RotateR(grandfather);
cur->_col = BLACK;
grandfather->_col = RED;
}
break;
}
}
else
{
Node* uncle = grandfather->_left;
if (uncle && uncle->_col == RED)
{
parent->_col = BLACK;
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 make_pair(newnode,true);
}
bool isBalance()
{
return isBalance(_root);
}
bool checkcolour(Node* root, int basevalue,int blacknum)
{
if (root == nullptr)
{
if (basevalue != blacknum)
{
return false;
}
return true;
}
if (root->_col == RED && root->_parent && root->_parent->_col == RED)
{
return false;
}
if (root->_col == BLACK)
{
blacknum++;
}
return checkcolour(root->_left, basevalue, blacknum) &&
checkcolour(root->_right, basevalue, blacknum);
}
bool isBalance(Node* root)
{
if (root == nullptr)
{
return true;
}
if (root->_col != BLACK)
{
return false;
}
//计算最左路基中黑色结点数量
int basevalue = 0;
Node* cur = root;
while (cur)
{
if (cur->_col == BLACK)
{
basevalue++;
}
cur = cur->_left;
}
return checkcolour(root, basevalue,0);
}
void inorder()
{
inorder(_root);
}
void inorder(Node* root)
{
if (root == nullptr)
{
return;
}
inorder(root->_left);
cout << root->_p.first << endl;
inorder(root->_right);
}
private:
Node* _root;
void RotateL(Node* parent)
{
//右边高 左旋
Node* cur = parent->_right;
Node* curleft = cur->_left;
Node* ppnode = parent->_parent;
parent->_right = curleft;
if (curleft)
{
curleft->_parent = parent;
}
cur->_left = parent;
parent->_parent = cur;
if (parent == _root)
{
_root = cur;
cur->_parent = nullptr;
}
else
{
if (ppnode->_left == parent)
{
ppnode->_left = cur;
}
else
{
ppnode->_right = cur;
}
}
cur->_parent = ppnode;
}
void RotateR(Node* parent)
{
Node* cur = parent->_left;
Node* curRight = cur->_right;
Node* ppnode = parent->_parent;
parent->_left = curRight;
if (curRight)
{
curRight->_parent = parent;
}
cur->_right = parent;
parent->_parent = cur;
if (parent == _root)
{
_root = cur;
cur->_parent = nullptr;
}
else
{
if (ppnode->_left == parent)
{
ppnode->_left = cur;
}
else
{
ppnode->_right = cur;
}
cur->_parent = ppnode;
}
}
};