#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. 使用(Drawable)资源——图片资源

    图片资源是最简单的Drawable资源,只要把*.png.*.jpg.*.gif等格式的图片放入/res/drawble-xxx目录下,Android SDK就会在编译应用中自动加载该图片,并在R资源 ...

  2. Velocity教程

    Velocity 语法(转) 一.基本语法 1."#"用来标识Velocity的脚本语句,包括#set.#if .#else.#end.#foreach.#end.#iinclud ...

  3. 手机淘宝中的那些Web技术-使用了类似PhoneGap的实现

    Native APP与Web APP的技术融合已经逐渐成为一种趋势,使用标准的Web技术来开发应用中的某些功能,不仅可以降低开发成本,同时还可以方便的进行功能迭代更新.但是如何保证Web APP的流畅 ...

  4. 使用jQuery的时候,js文件代码位置规范

    /** * 页面加载之后执行的函数:===================================== */$(function() { }); //如果需要同步执行需要同时编写在这里: $( ...

  5. 开箱即用 - jwt 无状态分布式授权

    基于JWT(Json Web Token)的授权方式 JWT 是JSON风格轻量级的授权和身份认证规范,可实现无状态.分布式的Web应用授权: 从客户端请求服务器获取token, 用该token 去访 ...

  6. GreenDao 3.2.0 的基本使用

    前言 Android开发中我们或多或少都会接触到数据库.Android中提供了一个占用内存极小的关系型数据库-SQLite.虽然Android系统中提供了许多操作SQLite的API,但是我们还是需要 ...

  7. ubuntu 下安装 cudnn

    安装cudnn前后,GPU跑一个算法的速度分别是139ms和26ms ! 1. 在以下网址选择  cuDNN v5.1 Library for Linux 下载 https://developer.n ...

  8. 【js 编程艺术】小制作四

    1. html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  9. maven常用命令介绍(持续更新)

    一.Maven的基本概念 主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1.项目构建 项目构建过程包括[清理项目]→[编译项目]→[测试项目]→[生成测试报告]→[打包项目]→[ ...

  10. 列存储段消除(ColumnStore Segment Elimination)

    列存储索引是好的!对于数据仓库和报表工作量,它们是真正的性能加速器.与聚集列存储结合,你会在常规行存储索引(聚集索引,非聚集索引)上获得巨大的压缩好处.而且创建聚集列存储索引非常简单: CREATE ...