// 红黑树2.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//
//
//#include
//
//int main()
//{
// std::cout << “Hello World!\n”;
//}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#include
using namespace std;
//map 和set
template
struct RBNode
{
typedef bool color;
RBNode* _parent;
RBNode* _left;
RBNode* _right;
//key-value
//键值对出现的。
v _val;
//颜色
COLOR _color;
RBNode(const v& val = v())
:_parent(nullptr)
, _left(nullptr)
, _right(nullptr)
, _kv(kv)
, _color(RED)
{}
};
template <class k, class v,class keyofvalue>
class RBTree
{
typedef RBTree Node;
RBTree()
:_header(new Node)
{
//创建头结点。
//创建的就是结构的方便是为了解决的是迭代器的实现
//创建空树
//类似于之前带头的循环链表的结构。
_header->_left = _header->_right = _header;
}
//红黑树的插入的时候就是主要是红的不能连续。
bool insert(const v& v)
{
//1.搜索树的插入
//空树:_header->_parent:nullptr
if (_header->_parent == nullptr)
{
//创建根节点
Node* root = new Node(v);
_header->_parent = root;
root->_parent = _header;
_header->_left = _header->_right = root;
//设置根节点是黑色
root->_color = BLACK;
return true;
}
//从根节点开始搜索
Node* cur = _header->_parent;
Node* parent = nullptr;
keyofvalue kov;
while (cur)
{
parent = cur;
//和key值进行比较
//if (cur->_val.first == val.first)
if(kov(cur->_val)==kov(val))
{
//key 是不能重复的
return false;
}
//else if (cur->_val.first > val.first)
else if(kov(cur->val)>kov(cur->val))
{
cur = cur->_left;
}
else
{
cur = cur->_right;
}
}
//创建待插入的节点
cur = new Node(val);
if (parent->_val.first > cur->_val.first)
parent->_left = cur;
else
parent->_right = cur;
cur->_parent = parent;
//2.修改颜色或者调整结构
//是否有红色连续的节点
while (cur != _header->_parent&&cur->_parent->_color == RED)
{
parent = cur->_parent;
Node* gfather = parent->_parent;
if (gfather->_left == parent)
{
Node* uncle = gfather->_right;
//1.uncle存在的,并且是红色的
if (uncle&&uncle->_color == RED)
{
parent->_color = uncle->_color = BLACK;
gfather->_color = RED;
//继续更新
cur = gfather;
}
}
else
{
//判断是否是双旋场景
if (cur == parent->_right)
{
//左旋
RotateL(parent)
//交换cur,parent指向,退化成右旋的场景
swap(cur, parent);
}
//右旋
RotateR(gfather);
parent->_color = BLACK;
gfather->_color = RED;
break;
}
}
else
{
//gfather->right=parent
//判断是否是双旋场景
//如果是那就是右左双旋
Node* uncle = gfather->_left;
if (uncle&&uncle->_color == RED)
{
parent->_color = uncle->_color = BLACK;
gfather->_color = RED;
cur = gfather;
}
else
{
if (cur == parent->_left)
{
//右旋
RotateR(parent)
//交换cur,parent指向,退化成右旋的场景
swap(cur, parent);
}
//左旋
RotateL(parent);
parent->_color = BLACK;
gfather->_color = RED;
break;
}
}
//根节点的颜色改成黑色
_header->_parent->_color = BLACK;
//更新header的左右指向
_header->_left = leftMost();
_header->_right = rightMost();
}
//左旋转
//parent
// subR
// subRL
void RotateL(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* pparent = parent->_parent;
if (pparent->_left = parent)
pparent->_left = subR;
else
pparent->_right = subR;
subR->_parent = pparent;
}
parent->_parent = subR;
}
//右旋转
//位置关系
// parent
//subL
// subLR
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
subL->_right = subLR;
parent->_left = subL;
if (subLR)
subLR->_parent = parent;
//判断根
if (parent == _header->_parent)
{
_header->_parent = subL;
subL->_parent = _header;
}
else
{
Node* pparent = parent->_parent;
if (pparent->_left == parent)
pparent->_left = subL;
else
pparent->_right == subL;
subL->_parent = pparent;
}
parent->_parent = subL;
}
void inorder()
{
_inorder(_header->_parent)
cout << endl;
}
void _inorder(Node* root)
{
if (root)
{
_inorder(root->_left);
cout << root->_kv.first << " ";
_inorder(root->_right);
}
}
Node* leftMost()
{
Node* cur = _header->_parent;
while (cur&&cur->_right)
{
cur = cur->_right;
}
return cur;
}
//红黑树:
//1.根:黑色
//2.每条路径黑色个数相同
//3.红色不能相同
//成员:header
bool isBalance()
{
if (_header->_parent == nullptr)
return true;
Node* root = _header->_parent;
if (root->_color == RED)
return false;
//统计一条路径上的黑色结点的个数
int bCount = 0;
Node* cur = root;
while (cur)
{
if (cur->_color == BLACK)
++bCount;
cur = cur->_left;
}
//遍历每一条路径
int curBCount = 0;
return isBalance(root, bCount, curBCount);
}
bool _isBalance(Node* root, int& bCount, int curBCount)
{
//当root为空时,一条路径遍历结束
if (root == nullptr)
{
//判断和黑色结点个数是否相同
if (curBCount != bCount)
return false;
else
return true;
}
//判断当前节点是否是黑色
if (root->_color == BLACK)
++curBCount;
//判断是否有红色连续的节点
if (root->_parent&&root->_color == RED && root->_parent->_color == RED)
{
cout << "data:" << root->_kv.first << endl;
return false;
}
return _isBalance(root->_left, bCount, curBCount) && _isBalance(root->_right, bCount, curBCount);
}
private:
Node* _header;
};
//map实现
template <class k,class T>
class Map
{
struct MapkeyofValue
{
const K& operator() (const pair<k,v>& val)
};
public:
bool insert(const pair<k, T>& kv)
{
_rbt.insert(kv);
}
v& operator[](const pair<k, v>& kv)
{
bool
}
private:
typedef RBTree<k, pair<k, T>> rbt;
rbt _rbt;
};
//set 实现
template
class set
{
public:
bool insert(const pair<k, T>& kv)
{
_rbt.insert(kv);
}
private:
typedef RBTree<k, pair<k, T>> rbt;
rbt _rbt;
};
void test()
{
Map<int, int> m;
m.insert(make_pair(1, 1));
m.insert(make_pair(1, 1));
m.insert(make_pair(1, 1));
}
int main()
{
test();
return 0;
}
//红黑树
//双旋就是表示的是高度差是不在一边的操作。例如是左边的右边高之类的。
//每条路径上黑色个数是相同的。
#include
#include
using namespace std;
//使用的是内置类型颜色
enum COLOR
{
BLACK,
RED
}
template <class k, class v>
struct RBNode
{
typedef bool color;
RBNode<k, v>* _parent;
RBNode<k, v>* _left;
RBNode<k, v>* _right;
//key-value
//键值对出现的。
pair<k,v> _kv;
//颜色
COLOR _color;
RBNode(const pair<k, v>& kv = pair<k, v>())
:_parent(nullptr)
, _left(nullptr)
, _right(nullptr)
, _kv(kv)
, _color(RED)
{}
};
template <class k,class v>
class RBTree
{
typedef RBTree<k, v> Node;
RBTree()
:_header(new Node)
{
//创建头结点。
//创建的就是结构的方便是为了解决的是迭代器的实现
//创建空树
//类似于之前带头的循环链表的结构。
_header->_left = _header->_right = _header;
}
//红黑树的插入的时候就是主要是红的不能连续。
bool insert(const pair<k, v>& kv)
{
//1.搜索树的插入
//空树:_header->_parent:nullptr
if (_header->_parent == nullptr)
{
//创建根节点
Node* root = new Node(kv);
_header->_parent = root;
root->_parent = _header;
_header->_left = _header->_right = root;
//设置根节点是黑色
root->_color = BLACK;
return true;
}
//从根节点开始搜索
Node* cur = _header->_parent;
Node* parent = nullptr;
while (cur)
{
parent = cur;
//和key值进行比较
if (cur->_kv.first == kv.first)
{
//key 是不能重复的
return false;
}
else if (cur->_kv.first > kv.first)
{
cur = cur->_left;
}
else
{
cur = cur->_right;
}
}
//创建待插入的节点
cur = new Node(kv);
if (parent->_kv.first > cur->_kv.first)
parent->_left = cur;
else
parent->_right = cur;
cur->_parent = parent;
//2.修改颜色或者调整结构
//是否有红色连续的节点
while (cur != _header->_parent&&cur->_parent->_color == RED)
{
parent = cur->_parent;
Node* gfather = parent->_parent;
if (gfather->_left == parent)
{
Node* uncle = gfather->_right;
//1.uncle存在的,并且是红色的
if (uncle&&uncle->_color == RED)
{
parent->_color = uncle->_color = BLACK;
gfather->_color = RED;
//继续更新
cur = gfather;
}
}
else
{
//判断是否是双旋场景
if (cur == parent->_right)
{
//左旋
RotateL(parent)
//交换cur,parent指向,退化成右旋的场景
swap(cur, parent);
}
//右旋
RotateR(gfather);
parent->_color = BLACK;
gfather->_color = RED;
break;
}
}
else
{
//gfather->right=parent
//判断是否是双旋场景
//如果是那就是右左双旋
Node* uncle = gfather->_left;
if (uncle&&uncle->_color == RED)
{
parent->_color = uncle->_color = BLACK;
gfather->_color = RED;
cur = gfather;
}
else
{
if (cur == parent->_left)
{
//右旋
RotateR(parent)
//交换cur,parent指向,退化成右旋的场景
swap(cur, parent);
}
//左旋
RotateL(parent);
parent->_color = BLACK;
gfather->_color = RED;
break;
}
}
//根节点的颜色改成黑色
_header->_parent->_color = BLACK;
//更新header的左右指向
_header->_left = leftMost();
_header->_right = rightMost();
}
//左旋转
//parent
// subR
// subRL
void RotateL(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* pparent = parent->_parent;
if (pparent->_left = parent)
pparent->_left = subR;
else
pparent->_right = subR;
subR->_parent = pparent;
}
parent->_parent = subR;
}
//右旋转
//位置关系
// parent
//subL
// subLR
void RotateR(Node* parent)
{
Node* subL = parent->_left;
Node* subLR = subL->_right;
subL->_right = subLR;
parent->_left = subL;
if (subLR)
subLR->_parent = parent;
//判断根
if (parent == _header->_parent)
{
_header->_parent = subL;
subL->_parent = _header;
}
else
{
Node* pparent = parent->_parent;
if (pparent->_left == parent)
pparent->_left = subL;
else
pparent->_right == subL;
subL->_parent = pparent;
}
parent->_parent = subL;
}
void inorder()
{
_inorder(_header->_parent)
cout << endl;
}
void _inorder(Node* root)
{
if (root)
{
_inorder(root->_left);
cout << root->_kv.first << " ";
_inorder(root->_right);
}
}
Node* leftMost()
{
Node* cur = _header->_parent;
while (cur&&cur->_right)
{
cur = cur->_right;
}
return cur;
}
//红黑树:
//1.根:黑色
//2.每条路径黑色个数相同
//3.红色不能相同
//成员:header
bool isBalance()
{
if (_header->_parent == nullptr)
return true;
Node* root = _header->_parent;
if (root->_color == RED)
return false;
//统计一条路径上的黑色结点的个数
int bCount = 0;
Node* cur = root;
while (cur)
{
if (cur->_color == BLACK)
++bCount;
cur = cur->_left;
}
//遍历每一条路径
int curBCount = 0;
return isBalance(root, bCount, curBCount);
}
bool _isBalance(Node* root, int& bCount, int curBCount)
{
//当root为空时,一条路径遍历结束
if (root == nullptr)
{
//判断和黑色结点个数是否相同
if (curBCount != bCount)
return false;
else
return true;
}
//判断当前节点是否是黑色
if (root->_color == BLACK)
++curBCount;
//判断是否有红色连续的节点
if (root->_parent&&root->_color == RED && root->_parent->_color == RED)
{
cout << "data:" << root->_kv.first << endl;
return false;
}
return _isBalance(root->_left, bCount, curBCount) && _isBalance(root->_right, bCount, curBCount);
}
private:
Node* _header;
};
void test()
{
RBTree<int, int> rbt;
rbt.insert(make_pair(10, 10));
rbt.insert(make_pair(15, 15));
rbt.insert(make_pair(5, 5));
rbt.insert(make_pair(2, 2));
}
void test1()
{
RBTree<int, int> rbt;
int n;
cin >> n;
for (int i = n; i > 0; --i)
{
rbt.insert(make_pair(i, i));
}
rbt.inorder();
cout << rbt.isBalance() << endl;
}
int main()
{
test();
test1();
return 0;
}