红黑树 C++实现
#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++实现的更多相关文章
- 红黑树——算法导论(15)
1. 什么是红黑树 (1) 简介 上一篇我们介绍了基本动态集合操作时间复杂度均为O(h)的二叉搜索树.但遗憾的是,只有当二叉搜索树高度较低时,这些集合操作才会较快:即当树的高度较高(甚至一种极 ...
- jdk源码分析红黑树——插入篇
红黑树是自平衡的排序树,自平衡的优点是减少遍历的节点,所以效率会高.如果是非平衡的二叉树,当顺序或逆序插入的时候,查找动作很可能会遍历n个节点 红黑树的规则很容易理解,但是维护这个规则难. 一.规则 ...
- 谈c++ pb_ds库(二) 红黑树大法好
厉害了,没想到翻翻pb_ds库看到这么多好东西,封装好的.现成的splay.红黑树.avl... 即使不能在考场上使用也可以用来对拍哦 声明/头文件 #include <ext/pb_ds/tr ...
- 定时器管理:nginx的红黑树和libevent的堆
libevent 发生超时后, while循环一次从堆顶del timer——直到最新调整的最小堆顶不是超时事件为止,(实际是del event),但是会稍后把这个timeout的 event放到ac ...
- 从2-3-4树到红黑树(下) Java与C的实现
欢迎探讨,如有错误敬请指正 如需转载,请注明出处 http://www.cnblogs.com/nullzx/ 相关博客: 从2-3-4树到红黑树(上) 从2-3-4树到红黑树(中) 1. 实现技 ...
- 红黑树/B+树/AVL树
RB Tree 红黑树 :http://blog.csdn.net/very_2/article/details/5722682 Nginx的RBTree实现 :http://blog.csdn ...
- 论AVL树与红黑树
首先讲解一下AVL树: 例如,我们要输入这样一串数字,10,9,8,7,15,20这样一串数字来建立AVL树 1,首先输入10,得到一个根结点10 2,然后输入9, 得到10这个根结点一个左孩子结点9 ...
- DataStructure——红黑树学习笔记
1.前言 本文伪码和解释参考: http://blog.csdn.net/v_JULY_v/article/details/6105630 C实现的源码本文未贴出,请见: http://blog.cs ...
- 红黑树(Red-Black tree)
红黑树又称红-黑二叉树,它首先是一颗二叉树,它具体二叉树所有的特性.同时红黑树更是一颗自平衡的排序二叉树.我们知道一颗基本的二叉树他们都需要满足一个基本性质–即树中的任何节点的值大于它的左子节点,且小 ...
- map,hash_map, hash_table, 红黑树 的原理和使用
在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...
随机推荐
- HDU-4861-Couple doubi(数学题,难懂!难懂!)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4861 这个题只能说没弄懂,感觉很难,看博客也看不懂,只能,多看几次,看能不能有所突破了. 代码的话只有 ...
- 网站运维工具使用iis日志分析工具分析iis日志(iis日志的配置)
我们只能通过各种系统日志来分析网站的运行状况,对于部署在IIS上的网站来说,IIS日志提供了最有价值的信息,我们可以通过它来分析网站的响应情况,来判断网站是否有性能问题,或者存在哪些需要改进的地方 对 ...
- SVG在网页中的四种使用方式
1,直接打开simple.svg <svg xmlns="http://www.w3.org/2000/svg" width="200" height=& ...
- secureCRT自动化脚本
http://cysnow.iteye.com/blog/1698791 cd \crt "C:\Program Files\VanDyke Software\Clients\SecureC ...
- 正则表达式之一:TSQL注释的查找
最近自己做了个小项目,涉及到了大量的正则表达式匹配和处理,在这里也和大家分享一下. 我相信接触过SQL Server数据库的很多朋友都知道,它是以"--"开头来进行注释的,但你觉得 ...
- Professional C# 6 and .NET Core 1.0 - 37 ADO.NET
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 37 ADO.NET -------- ...
- iOS获取本地时间
NSDate *currentDate = [NSDate date];//获取当前时间,日期 NSDateFormatter *dateFormatter = [[NSDateFormatter a ...
- 基于服务的SOA架构_后续篇
今天是元宵节,首先祝各位广大博友在接下来的光阴中技术更上一层,事事如意! 昨天简单介绍了一下本人在近期开发过的一个电商购物平台的架构流程和一些技术说明:今天将详细总结一下在项目中用到的各个架构技术的环 ...
- 解决 Eclipse build workspace validation javascript 慢的问题
参考: http://blog.csdn.net/zhangzikui/article/details/24805935 http://www.cnblogs.com/wql025/p/4978351 ...
- 如何一步一步用DDD设计一个电商网站(十四)—— 回顾与总结
本系列所有文章 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念 如何一步一步用DDD设计一个电商网站(二)—— 项目架构 如何一步一步用DDD设计一个电商网站(三)—— 初涉核心域 如 ...