红黑树实现map和set

#pragma once
#include
#include
using namespace std;
enum Color
{
BLACK, RED
};
template
struct RBNode
{
V _val;
Color color;
RBNode* _left;
RBNode* _right;
RBNode* _parent;
RBNode(const V& val = V())
:_val(val),
color(RED),
_left(nullptr),
_right(nullptr),
_parent(nullptr)
{}
};
template
struct RBTIterator
{
typedef RBTIterator Self;
typedef RBNode Node;
Node* _node;
RBTIterator(Node* node)
:_node(node)
{}
V& operator*()
{
return _node->_val;
}
V* operator->()
{
return &_node->_val;
}
bool operator !=(const Self& it)
{
return _node != it._node;
}
bool operator (const Self& it)
{
return _node == it._node;
}
Self& operator ++ ()
{
if (_node->_right)
{
_node = _node->_right;
while (_node && _node->_left)
{
_node = _node->_left;
}
}
else
{
Node* parent = _node->_parent;
while (_node
parent->_right)
{
_node = parent;
parent = parent->_parent;
}
if (_node->_right != parent)
_node = parent;
}
return this;
}
};
//建立红黑树
template<class K, class V, class KeyOfValue>
class RBTree
{
public:
typedef RBNode Node;
typedef RBTIterator iterator;
iterator begin()
{
return iterator(_header->_left);
}
iterator end()
{
return iterator(_header);
}
RBTree()
:_header(new Node)
{
_header->_left = _header->_right = _header;
}
pair<iterator,bool> insert(const V& val)
{
//如果根节点不存在,创建根节点
Node
cur = _header->_parent;
if (cur == nullptr)
{
cur = new Node(val);
_header->_left = _header->_right = cur;
cur->_parent = _header;
_header->_parent = cur;
cur->color = BLACK;
return make_pair(iterator(cur), true);
}
Node* parent = nullptr;
KeyOfValue kov;
while (cur)
{
parent = cur;
if (kov(cur->_val) == kov(val))
{
return make_pair(iterator(cur), false);
}
else if (kov(cur->_val) > kov(val))
{
cur = cur->_left;
}
else
{
cur = cur->_right;
}
}
cur = new Node(val);
Node* r = cur;
if (kov(cur->_val) > kov(parent->_val))
{
parent->_right = cur;
}
else
{
parent->_left = cur;
}
cur->_parent = parent;
while (cur != _header->_parent && cur->_parent->color == RED)
{
Node* p = cur->_parent;
Node* g = p->_parent;
if (g->_left == p)
{
Node* u = g->_right;
if (u && u->color == RED)
{
u->color = p->color = BLACK;
g->color = RED;
cur = g;
}
else
{
if (p->_right == cur)
{
rorateLeft§;
swap(cur, p);
}
rorateRight(g);
g->color = RED;
p->color = BLACK;
break;
}
}
else
{
Node* u = g->_left;
if (u && u->color == RED)
{
u->color = p->color = BLACK;
g->color = RED;
cur = g;
}
else
{
if (p->_left == cur)
{
rorateRight§;
swap(p, cur);
}
rorateLeft(g);
g->color = RED;
p->color = BLACK;
break;
}
}
}
_header->_right = MostRight();
_header->_left = MostLeft();
_header->_parent->color = BLACK;
return make_pair(iterator®, true);
}
void rorateLeft(Node* parent)
{
Node* SubR = parent->_right;
Node* SubRL = SubR->_left;
SubR->_left = parent;
parent->_right = SubRL;
if (SubRL)
SubRL->_parent = parent;
if (parent == _header->_parent)
{
_header->_parent = SubR;
SubR->_parent = _header;
}
else
{
Node* g = parent->_parent;
SubR->_parent = g;
if (g->_left == parent)
{
g->_left = SubR;
}
else
{
g->_right = SubR;
}
}
parent->_parent = SubR;
}
void rorateRight(Node* parent)
{
Node* SubL = parent->_left;
Node* SubLR = SubL->_right;
SubL->_right = parent;
parent->_left = SubLR;
if (SubLR)
SubLR->_parent = parent;
if (_header->_parent == parent)
{
_header->_parent = SubL;
SubL->_parent = _header->_parent;
}
else
{
Node* g = parent->_parent;
SubL->_parent = g;
if (g->_left == parent)
{
g->_left = SubL;
}
else
{
g->_right = SubL;
}
}
parent->_parent = SubL;
}
Node* MostRight()
{
Node* root = _header->_parent;
while (root && root->_right)
{
root = root->_right;
}
return root;
}
Node* MostLeft()
{
Node* root = _header->_parent;
while (root && root->_left)
{
root = root->_left;
}
return root;
}
bool _isRBTree(Node* root, int total, int curCount)
{
if (root == nullptr)
{
if (curCount == total)
{
return true;
}
else
return false;
}
if (root->color == BLACK)
++curCount;
Node* parent = root->_parent;
if (parent && parent->color == RED && root->color == RED)
{
cout << “error\n” << endl;
return false;
}
return _isRBTree(root->_left, total, curCount) &&
_isRBTree(root->_right, total, curCount);
}
bool isRBTree()
{
Node* root = _header->_parent;
Node* cur = root;
if (root == nullptr)
return true;
if (root->color == RED)
return false;
int cnt = 0;
while (cur)
{
if (cur->color == BLACK)
cnt++;
cur = cur->_left;
}
_isRBTree(root, cnt, 0);
}
void inorder()
{
_inorder(_header->_parent);
}
void _inorder(Node* root)
{
if (root == nullptr)
{
return;
}
_inorder(root->_left);
cout << root->_val << " “;
_inorder(root->_right);
}
private:
Node* _header;
};
#include"RBTree.hpp”
#include
using namespace std;
template <class K, class V>
class Map
{
struct MapKeyOfValue
{
const K& operator()(const pair<K, V>& value)
{
return value.first;
}
};
public:
typedef typename RBTree<K, pair<K, V>, MapKeyOfValue>::iterator iterator;
iterator begin()
{
return _rbt.begin();
}
iterator end()
{
return _rbt.end();
}
pair<iterator,bool> insert(const pair<K, V>& value)
{
return _rbt.insert(value);
}
V& operator[](const K& key)
{
pair<iterator, bool> ret = _rbt.insert(make_pair(key, V()));
return ret.first->second;
}
private:
RBTree<K, pair<K, V>, MapKeyOfValue> _rbt;
};
template
class Set
{
struct SetKeyOfValue
{
const K& operator()(const K& value)
{
return value;
}
};
public:
typedef typename RBTree<K, K, SetKeyOfValue>::iterator iterator;
iterator begin()
{
return _rbt.begin();
}
iterator end()
{
return _rbt.end();
}
pair<iterator, bool>insert(const K& value)
{
return _rbt.insert(value);
}
private:
RBTree<K, K,SetKeyOfValue> _rbt;
};
void test() {
Map<int, int> mp;
mp.insert(make_pair(1, 1));
mp.insert(make_pair(7, 7));
mp.insert(make_pair(6, 6));
mp.insert(make_pair(2, 2));
mp.insert(make_pair(3, 3));
mp[1] = 1000;
mp[2] = 100;
Map<int, int>::iterator it = mp.begin();
while (it != mp.end())
{
cout << it->first << “----->” << it->second << endl;
++it;
}
}
void test2()
{
 Set st;
 st.insert(1);
 st.insert(2);
 st.insert(3);
 st.insert(4);
 st.insert(5);
 Set::iterator p = st.begin();
 while (p != st.end())
 {
  cout << *p << endl;
  ++p;
 }
}
int main()
{
 test2();
 system(“pause”);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值