C++实现红黑树,仿STL封装
- //RB_Tree.hpp
- //The code of red black trees
- //2011/12/31 by Adoo
- // The foundation :http://www.roading.org/?p=691
- #ifndef RB_TREES_HPP
- #define RB_TREES_HPP
- #include<iterator>
- #include<iomanip>
- #include<deque>
- enum RB_Color{
- red,
- black
- };
- template<typename Type>
- class RB_Tree{
- private:
- struct rb_node;
- class node_iterator;
- public:
- typedef node_iterator iterator;
- typedef const node_iterator const_iterator;
- RB_Tree(){
- _nil->_color=black;
- _root=_nil;
- };
- ~RB_Tree()
- {
- for(iterator iter=begin(); iter !=end();)
- {
- eraser(iter++);
- }
- _root=_nil;
- }
- iterator begin(){
- return sub_min(_root);
- }
- iterator end(){
- return iterator(_nil);
- }
- static iterator sub_min(iterator iter){
- rb_node *min=iter.pointer();
- while(min->_left !=_nil)
- {
- min=min->_left;
- }
- return min;
- }
- iterator insert(Type value){
- rb_node *y=_nil;
- rb_node *z=new rb_node; //create a node by the value
- //needn't set the z's color ,because red is rb_node's default color
- z->_value=value;
- z->_left=_nil;
- z->_right=_nil;
- rb_node* x=_root; //x iterator from _root
- while(x !=_nil )
- {
- y=x;
- if(x->_value< z->_value)
- x=x->_right;
- else
- x=x->_left;
- }
- z->_parent=y;
- if(y==_nil) //determine z should be y's left or right
- _root=z;
- else
- if(y->_value < z->_value)
- y->_right=z;
- else
- y->_left=z;
- rb_insert_fixup(z); //restore the red black properties
- return z;
- }
- iterator eraser(iterator iter){
- rb_node* z=iter.pointer();
- rb_node* y=z;
- RB_Color y_color=y->_color;
- rb_node *x=NULL;
- if(z->_left==_nil ){ //case1: z's left child is nil
- x=z->_right;
- transplant(z, z->_right);
- }
- else{
- if(z->_right==_nil){// case2: z's right child is nil
- x=z->_left;
- transplant(z, z->_left);
- }
- else{//case3: both children of z are not nil
- y=sub_min(z->_right).pointer();
- y_color=y->_color;
- x=y->_right;
- if(y->_parent==z)
- x->_parent=y;
- else{
- transplant(y, y->_right);
- //link z's right subtree into y, only y isn't z's child;
- y->_right=z->_right;
- y->_right->_parent=y;
- }
- transplant(z, y);
- //link z's subtree into y.
- y->_left=z->_left;
- y->_left->_parent=y;
- y->_color=z->_color;
- }
- }
- iterator result = ++iterator(z);
- delete z;
- if(y_color==black)
- eraser_fixup(x);
- return result;
- };
- private:
- void transplant(rb_node *u, rb_node *v){
- if(u->_parent == _nil)
- {
- _root=v;
- }
- else
- if(u== u->_parent->_left)
- u->_parent->_left=v;
- else
- u->_parent->_right=v;
- v->_parent=u->_parent;
- };
- void left_rotate(rb_node *x){
- rb_node* y=x->_right; //set y
- x->_right=y->_left; //turn y's left subtree into x's right subtree
- if(y->_left !=_nil)
- y->_left->_parent=x;
- y->_parent=x->_parent; //link y to x's parent
- if(x->_parent != _nil )
- {
- if(x->_parent->_left==x)
- x->_parent->_left=y;
- else
- x->_parent->_right=y;
- }
- else
- _root=y;
- y->_left=x; //put x on y's left
- x->_parent=y;
- }
- void right_rotate(rb_node *x)
- {
- rb_node* y=x->_left; //set y;
- x->_left=y->_right; //turn y's right subtree into x's left subtree
- if(y->_right != _nil)
- y->_right->_parent=x;
- y->_parent=x->_parent; //link y to x's parent
- if(x->_parent != _nil)
- {
- if(x==x->_parent->_left)
- x->_parent->_left=y;
- else
- x->_parent->_right=y;
- }
- else
- _root=y;
- y->_right=x; //put x on y's right;
- x->_parent=y;
- }
- void rb_insert_fixup(rb_node* z){
- while(z->_parent->_color==red){
- if(z->_parent==z->_parent->_parent->_left){
- rb_node* y=z->_parent->_parent->_right;
- if(y->_color==red){
- z->_parent->_color=black;
- y->_color=black;
- z->_parent->_parent->_color=red;
- z=z->_parent->_parent;
- }
- else{
- if(z==z->_parent->_right){
- z=z->_parent;
- left_rotate(z);
- }
- z->_parent->_color=black;
- z->_parent->_parent->_color=red;
- right_rotate(z->_parent->_parent);
- }
- }
- else{
- rb_node* y=z->_parent->_parent->_left;
- if(y->_color==red){
- z->_parent->_color=black;
- y->_color=black;
- z->_parent->_parent->_color=red;
- z=z->_parent->_parent;
- }
- else{
- if(z==z->_parent->_left){
- z=z->_parent;
- right_rotate(z);
- }
- z->_parent->_color=black;
- z->_parent->_parent->_color=red;
- left_rotate(z->_parent->_parent);
- }
- }
- }
- _root->_color=black;
- };;
- void eraser_fixup(rb_node* x){
- while(x != _root && x->_color ==black){
- if(x==x->_parent->_left){
- rb_node* w=x->_parent->_right;
- if(w->_color == red){ //case 1: x's sbling w is red.
- x->_parent->_color=red;
- w->_color=black;
- left_rotate(x->_parent); //convert case 1 to case 2.
- }
- else{//case 2 : x's sbling w is black.
- if(w->_left->_color == black && w->_right->_color == black){
- //case 2.1 : both children of w are black
- w->_color=red;
- x=x->_parent;
- }
- else{
- if(w->_left->_color==red && w->_right->_color==black){
- //case 2.2: w's left child is red and w's right child is black.
- //we convert this case to case 2.3.
- w->_left->_color=black;
- w->_color=red;
- right_rotate(w);
- w=x->_parent->_right;
- }
- //case 2.3: w's right child is red;
- w->_color=x->_parent->_color;
- w->_parent->_color=black;
- w->_right->_color= black;
- left_rotate(x->_parent);
- x=_root; //terminate the loop;
- }
- }
- }
- else{
- rb_node* w=x->_parent->_right;
- if(w->_color == red){
- x->_parent->_color=red;
- w->_color=black;
- left_rotate(x->_parent);
- }
- else{
- if(w->_right->_color == black && w->_left->_color == black){
- w->_color=red;
- x=x->_parent;
- }
- else{
- if(w->_right->_color==red && w->_left->_color == black){
- w->_right->_color=black;
- w->_color=red;
- left_rotate(w);
- w=x->_parent->_left;
- }
- w->_color=x->_parent->_color;
- w->_parent->_color=black;
- w->_left->_color= black;
- right_rotate(x->_parent);
- x=_root; //terminate the loop;
- }
- }
- }
- }
- x->_color=black;
- };
- private:
- rb_node* _root;
- public:
- static rb_node* _nil;
- };
- template<typename Type>
- struct RB_Tree<Type>::rb_node
- {
- Type _value;
- rb_node *_left;
- rb_node *_right;
- rb_node *_parent;
- RB_Color _color;
- rb_node()
- :_value(Type()),_left(NULL),_right(NULL),_parent(NULL),_color(red)
- {};
- };
- template<typename Type>
- class RB_Tree<Type>::node_iterator:
- public std::iterator<std::bidirectional_iterator_tag ,rb_node>
- {
- public:
- node_iterator(rb_node* n): _node(n){};
- Type& operator* () const{
- return _node->_value;
- };
- rb_node* operator ->()const
- {
- return _node;
- };
- node_iterator operator++ ()
- {
- if(_node==RB_Tree<Type>::_nil)
- return _node;
- if(_node->_right!=RB_Tree<Type>::_nil)
- {
- *this=RB_Tree<Type>::sub_min(_node->_right);
- }
- else
- {
- rb_node *parent=_node->_parent;
- while(parent !=RB_Tree<Type>::_nil&& _node==parent->_right)
- {
- _node=parent;
- parent=parent->_parent;
- }
- _node=parent;
- }
- return *this;
- }
- node_iterator operator++(int){
- node_iterator ret(*this);
- ++*this;
- return ret;
- }
- bool operator ==( node_iterator r_iter)
- {
- return _node == r_iter._node;
- };
- bool operator !=( node_iterator r_iter){
- return _node != r_iter._node;
- }
- rb_node* pointer()
- {
- return _node;
- }
- private:
- rb_node* _node;
- };
- #endif
- //各种
C++实现红黑树,仿STL封装的更多相关文章
- stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list
stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...
- stl map底层之红黑树插入步骤详解与代码实现
转载注明出处:http://blog.csdn.net/mxway/article/details/29216199 本篇文章并没有详细的讲解红黑树各方面的知识,只是以图形的方式对红黑树插入节点需要进 ...
- [转]SGI STL 红黑树(Red-Black Tree)源代码分析
STL提供了许多好用的数据结构与算法,使我们不必为做许许多多的重复劳动.STL里实现了一个树结构-Red-Black Tree,它也是STL里唯一实现的一个树状数据结构,并且它是map, multim ...
- SGI STL红黑树中迭代器的边界值分析
前言 一段程序最容易出错的就是在判断或者是情况分类的边界地方,所以,应该对于许多判断或者是情况分类的边界要格外的注意.下面,就分析下STL中红黑树的迭代器的各种边界情况.(注意:分析中STL使用的版本 ...
- 【stl学习笔记】红黑树
转自维基百科 红黑树是一种平衡二叉搜索树,它可以在O(log n)时间内做查找,插入和删除,这里的n是树中元素的数目. 性质: 红黑树是每个节点都带有颜色属性的二叉查找树,颜色为红色或黑色.在二叉查找 ...
- 浅谈算法和数据结构: 七 二叉查找树 八 平衡查找树之2-3树 九 平衡查找树之红黑树 十 平衡查找树之B树
http://www.cnblogs.com/yangecnu/p/Introduce-Binary-Search-Tree.html 前文介绍了符号表的两种实现,无序链表和有序数组,无序链表在插入的 ...
- 谈c++ pb_ds库(二) 红黑树大法好
厉害了,没想到翻翻pb_ds库看到这么多好东西,封装好的.现成的splay.红黑树.avl... 即使不能在考场上使用也可以用来对拍哦 声明/头文件 #include <ext/pb_ds/tr ...
- 论AVL树与红黑树
首先讲解一下AVL树: 例如,我们要输入这样一串数字,10,9,8,7,15,20这样一串数字来建立AVL树 1,首先输入10,得到一个根结点10 2,然后输入9, 得到10这个根结点一个左孩子结点9 ...
- map,hash_map, hash_table, 红黑树 的原理和使用
在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...
随机推荐
- 安装MongoDB -- Windows平台
1. 安装MongoDB 2. 添加环境变量 将安装后的bin目录,添加至系统的Path环境变量中,例如我的安装路径为"C:\Program Files\MongoDB\Server\3.2 ...
- 查看线程linux cpu使用率
Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算 转 http://www.cnblogs.com/lidabo/p/4738113.html目录(?)[-] proc文件系统 p ...
- Huffman树及其应用
哈夫曼树又称为最优二叉树,哈夫曼树的一个最主要的应用就是哈夫曼编码,本文通过简单的问题举例阐释哈夫曼编码的由来,并用哈夫曼树的方法构造哈夫曼编码,最终解决问题来更好的认识哈夫曼树的应用--哈夫曼编码. ...
- Adatper中获取宽高为0的问题
但是我们想在getView()中获取ImageView的宽和高存在问题,在getView()里面刚开始显示item的时候利用ImageView.getWidth() 获取的都是0,为什么刚开始获取不到 ...
- 关于开发环境 git 重新部署
apps 开发机器 多次因为升级出现无法登陆 下面就重新部署 流程做笔记 1 备份 根目录下的 那一堆shell 和 Cache/data 下的系统配置 2 shell : su www ...
- Nginx+Keepalived 实现双击热备及负载均衡
Nginx master : 10.1.58.191 Nginx负载均衡主机 Nginx slave : 10.1.58.181 Nginx负载均衡备机Nginx_VIP_TP: 10 ...
- [PWA] Keynote: Progressive Web Apps across all frameworks
PWA: Add to home screen Angular Universal Server side rendering: for achieving better proference on ...
- hadoop文件的序列化
目录 1.为什么要序列化? 2.什么是序列化? 3.为什么不用Java的序列化? 4.为什么序列化对Hadoop很重要? 5.Hadoop中定义哪些序列化相关的接口呢? 6.Hadoop 自定义Wri ...
- UNIX环境高级编程第二版代码笔记
1. 第一个程序 gcc 1.1.c /tmp/ccbnJqcB.o: In function `main': 1.1.c:(.text+0x17): undefined reference to ...
- windows和linux双系统删除linux
装了Windows和linux双系统的朋友,在后期要删除linux是个比较头痛的问题,因为MBR已经被linux接管,本文的目的是如何在windows 和linux双系统下,简单,完美地卸载linux ...