#ifndef __RED_BLACK_TREE_H__
#define __RED_BLACK_TREE_H__ namespace lxf {
template <typename Key, typename Value>
class RedBlackTree {
struct Node {
enum Color { RED, BLACK };
Key key;
Value value;
Node *left;
Node *right;
Node *parent;
Color color;
Node(Key pkey, Value pvalue, Color pcolor) : key(pkey), value(pvalue), left(nullptr), right(nullptr), parent(nullptr), color(pcolor) {}
};
Node *root;
void rotateLeft(Node *x);
void rotateRight(Node *x); Node* deleteMin(Node* head);
Node* min(Node *head);
public:
void insert(Key key, Value value);
void erase(Key key);
RedBlackTree():root(nullptr) {}
}; /*
* 对红黑树的节点(x)进行左旋转
*
* 左旋示意图(对节点x进行左旋):
* px px
* / /
* x y
* / \ --(左旋)--> / \ #
* lx y x ry
* / \ / \
* ly ry lx ly
*
*
*/
template<typename Key, typename Value>
inline void RedBlackTree<Key, Value>::rotateLeft(Node * x)
{
Node *y = x->right; x->right = y->left;
if (y->left != nullptr)
y->left->parent = x; y->parent = x->parent;
if (x->parent == nullptr)
root = y;
else {
if (x->parent->left == x)
x->parent->left = y;
else
x->parent->right = y;
}
y->left = x;
x->parent = y;
} /*
* 对红黑树的节点(y)进行右旋转
*
* 右旋示意图(对节点y进行左旋):
* py py
* / /
* y x
* / \ --(右旋)--> / \ #
* x ry lx y
* / \ / \ #
* lx rx rx ry
*
*/
template<typename Key, typename Value>
inline void RedBlackTree<Key, Value>::rotateRight(Node * x)
{
Node *x = y->left;
y->left = x->right;
if (x->right != nullptr)
x->right->parent = y;
x->parent = y->parent;
if (y->parent == nullptr)
root = x;
else {
if (y == y->parent->right)
y->parent->right = x;
else
y->parent->left = x;
}
x->right = y;
y->parent = x;
} template<typename Key, typename Value>
inline typename RedBlackTree<Key, Value>::Node * RedBlackTree<Key, Value>::deleteMin(Node * head)
{
if (head == nullptr)
return nullptr;
Node *node = head;
Node *lastNode = nullptr;
while (node->left != nullptr)
{
lastNode = node;
node = node->left;
}
lastNode->left = node->right;
if (node->right != nullptr)
node->right->parent = lastNode;
return head;
} template<typename Key, typename Value>
inline typename RedBlackTree<Key, Value>::Node * RedBlackTree<Key, Value>::min(Node * head)
{
if (head == nullptr)
return nullptr;
Node *node = head;
while (node->left != nullptr)
node = node->left;
return node;
} template<typename Key, typename Value>
inline void RedBlackTree<Key, Value>::insert(Key key, Value value)
{
Node *parent = nullptr;
Node **ppNode = &root;
while (*ppNode != nullptr)
{
parent = *ppNode;
if (key < (*ppNode)->key)
ppNode = &((*ppNode)->left);
else
ppNode = &((*ppNode)->right);
}
if (*ppNode == nullptr) {
*ppNode = new Node(key, value, RED);
(*ppNode)->parent = parent;
} // 结点是根
if (parent == nullptr) {
root->color = Node::BLACK;
return;
} Node *node = *ppNode;
// 调整
while (node->parent->color == Node::RED)
{
Node *parent = node->parent;
Node *gparent = node->parent->parent;
if (parent == gparent->left)
{
Node *uncle = gparent->right;
if (uncle->color == RED)
{
parent->color = BLACK;
uncle->color = BLACK;
gparent->color = RED;
node = gparent;
}
else if (uncle->color == BLACK)
{
if (node == parent->right)
{
node = parent;
rotateLeft(node);
}
else
{
parent->color = BLACK;
gparent->color = RED;
rotateRight(gparent);
}
}
}
else if (parent == gparent->right)
{
Node *uncle = gparent->left;
if (uncle->color == RED)
{
parent->color = BLACK;
uncle->color = BLACK;
gparent->color = RED;
node = gparent;
}
else if (uncle->color == BLACK)
{
if (node == parent->left)
{
node = parent;
rotateRight(node);
}
else
{
parent->color = BLACK;
gparent->color = RED;
rotateLeft(gparent);
}
}
}
}
} template<typename Key, typename Value>
inline void RedBlackTree<Key, Value>::erase(Key key)
{
Node *lastNode = nullptr;
Node *node = root;
while (node != nullptr)
{
if (key < node->key) {
lastNode = node;
node = node->left;
}
else if (key > node->key) {
lastNode = node;
node = node->right;
}
else {
Node **plastNode = nullptr;
// 注意树根
if (lastNode == nullptr)
plastNode = &root;
else
plastNode = &lastNode;
// 无节点的情况
if (node->left == nullptr && node->right == nullptr)
{
if ((*plastNode)->left == node)
{
(*plastNode)->left = nullptr;
delete node;
}
else if ((*plastNode)->right == node)
{
(*plastNode)->right = nullptr;
delete node;
}
}
// 两个节点的情况
else if (node->left != nullptr && node->right != nullptr) {
Node *star = min(node->right);
star->right = deleteMin(node->right);
if (star->right != nullptr)
star->right->parent = star;
star->left = node->left;
if (star->left != nullptr)
star->left->parent = star;
delete node;
}
// 只有一个节点的情况
else if (node->left == nullptr) {
if ((*plastNode)->left == node) {
(*plastNode)->left = node->right;
if (node->right != nullptr)
node->right->parent = *plastNode;
delete node;
}
else if ((*plastNode)->right == node) {
(*plastNode)->right = node->right;
if (node->right != nullptr)
node->right->parent = *plastNode;
delete node;
}
}
else if (node->right == nullptr) {
if ((*plastNode)->right == node) {
(*plastNode)->right = node->left;
if (node->left != nullptr)
node->left->parent = *plastNode;
delete node;
}
else if ((*plastNode)->left == node) {
(*plastNode)->left = node->left;
if (node->left != nullptr)
node->left->parent = *plastNode;
delete node;
}
} }
} while (node != root && node->color == BLACK)
{
if (node == node->parent->left)
{
Node* brother = node->parent->right;
if (brother->color == RED)
{
brother->color = BLACK;
node->parent->color = RED;
rotateLeft(node->parent);
}
else
{
if (brother->left->color == BLACK && brother->right->color == BLACK)
{
brother->color = RED;
node = node->parent;
}
else if (brother->right->color == BLACK)
{
brother->color = RED;
brother->left->color = BLACK;
RotateRight(brother);
}
else if (brother->right->color == RED)
{
brother->color = node->parent->color;
node->parent->color = BLACK;
brother->right->color = BLACK;
rotateLeft(node->parent);
node = root;
}
}
}
else
{
Node* brother = node->parent->left;
if (brother->color == RED)
{
brother->color = BLACK;
node->parent->color = RED;
rotateRight(node->parent);
}
else
{
if (brother->left->color == BLACK && brother->right->color == BLACK)
{
brother->color = RED;
node = node->parent;
}
else if (brother->left->color == BLACK)
{
brother->color = RED;
brother->right->color = BLACK;
rotateLeft(brother);
}
else if (brother->left->color == RED)
{
brother->color = node->parent->color;
node->parent->color = BLACK;
brother->left->color = BLACK;
rotateRight(node->parent);
node = root;
}
}
}
}
node->parent = root; //最后将node置为根结点,
node->color = BLACK; //并改为黑色。
}
} #endif /*__RED_BLACK_TREE_H__*/

红黑树 C++实现的更多相关文章

  1. 红黑树——算法导论(15)

    1. 什么是红黑树 (1) 简介     上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...

  2. jdk源码分析红黑树——插入篇

    红黑树是自平衡的排序树,自平衡的优点是减少遍历的节点,所以效率会高.如果是非平衡的二叉树,当顺序或逆序插入的时候,查找动作很可能会遍历n个节点 红黑树的规则很容易理解,但是维护这个规则难. 一.规则 ...

  3. 谈c++ pb_ds库(二) 红黑树大法好

    厉害了,没想到翻翻pb_ds库看到这么多好东西,封装好的.现成的splay.红黑树.avl... 即使不能在考场上使用也可以用来对拍哦 声明/头文件 #include <ext/pb_ds/tr ...

  4. 定时器管理:nginx的红黑树和libevent的堆

    libevent 发生超时后, while循环一次从堆顶del timer——直到最新调整的最小堆顶不是超时事件为止,(实际是del event),但是会稍后把这个timeout的 event放到ac ...

  5. 从2-3-4树到红黑树(下) Java与C的实现

    欢迎探讨,如有错误敬请指正 如需转载,请注明出处   http://www.cnblogs.com/nullzx/ 相关博客: 从2-3-4树到红黑树(上) 从2-3-4树到红黑树(中) 1. 实现技 ...

  6. 红黑树/B+树/AVL树

    RB Tree 红黑树  :http://blog.csdn.net/very_2/article/details/5722682 Nginx的RBTree实现   :http://blog.csdn ...

  7. 论AVL树与红黑树

    首先讲解一下AVL树: 例如,我们要输入这样一串数字,10,9,8,7,15,20这样一串数字来建立AVL树 1,首先输入10,得到一个根结点10 2,然后输入9, 得到10这个根结点一个左孩子结点9 ...

  8. DataStructure——红黑树学习笔记

    1.前言 本文伪码和解释参考: http://blog.csdn.net/v_JULY_v/article/details/6105630 C实现的源码本文未贴出,请见: http://blog.cs ...

  9. 红黑树(Red-Black tree)

    红黑树又称红-黑二叉树,它首先是一颗二叉树,它具体二叉树所有的特性.同时红黑树更是一颗自平衡的排序二叉树.我们知道一颗基本的二叉树他们都需要满足一个基本性质–即树中的任何节点的值大于它的左子节点,且小 ...

  10. map,hash_map, hash_table, 红黑树 的原理和使用

    在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...

随机推荐

  1. Visual Studio 2015的安装与基本使用

    为什么要使用Visual Studio 2015? 它是中文的.界面友好.自动补全.实时语法错误提示(上图中波浪线部分).单步调试……最重要的社区版是免费的!所以你不必再使用破解的.老旧的的不兼容现代 ...

  2. 一步一步Asp.Net MVC系列_权限管理设计

    http://www.cnblogs.com/mysweet/archive/2012/07/26/2610793.html

  3. 无法删除MySql数据库,报错1010 error dropping

    环境:MySQL.Navicat 8 操作:Drop database testDB 报错:1010 error dropping database 解决方法:          ps -ef | g ...

  4. 关于angularjs过滤器的小尝试

    最近的项目中用到了angularjs,相比传统的jquery直接操作Dom, 开发web项目,angularjs在操作表格数据时的数据绑定,操作让我不禁直呼过瘾,好方便啊, 从后台接口传一个json过 ...

  5. HDU1175(dfs)

    连连看 Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description ...

  6. Cms 总结(转)

    提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,.net和java的高端的cms系统也逐渐 ...

  7. ubuntu 笔记一

    注:ubuntu14.04 64位 1.刚安装的ubuntu无法在终端使用su 原因:root没有默认密码,需要手动设定. 解决方法:以具有sudo权限的用户登录 给root用户设置密码:打开一个te ...

  8. list与数组转换

    1.数组转换list (1) List myList = new ArrayList(); String[] myStringArray = new String[] {"Java" ...

  9. PHP变量处理之serialize

    官方定义: string serialize ( mixed $value ) serialize() 返回字符串,此字符串包含了表示 value 的字节流,可以存储于任何地方.这有利于存储或传递 P ...

  10. java基础知识点---equal,==,hashcode

    1.==比较对象之间的地址是否相同 student a=new student(1); student b=new student(1); a==b   false b=a; a==b   true ...