//

//  RB_tree_STL.cpp

//  笔记

//

//  Created by fam on 15/3/21.

//

//

#include "RB_tree_STL.h"

//---------------------------15/03/21----------------------------

RB_tree

{

/*

一个由上而下程序:

为了避免父子节点皆为红色的情况持续向上层发展,形成处理时效上的瓶颈,可以从上向下处理,

假设新增的节点为a,那就沿着a的路径,只要看到一个节点的两个子节点都是红色,就把这个节点改为红色

其他两个子节点改成黑色。

*/

//RB_tree的节点设计

typedef
bool __rb_tree_color_type;

const __rb_tree_color_type __rb_tree_red =

const __rb_tree_color_type __rb_tree_black =
true;

//__rb_tree_node_base

struct __rb_tree_node_base

{

typedef __rb_tree_color_type color_type;

typedef __rb_tree_node_base* base_ptr;

color_type color;

base_ptr parent;

base_ptr left;

base_ptr right;

//一直向左走,就会找到最小值

static base_ptr minimum(base_ptr x)

{

while (x->left !=
)

x=x->left;

return x;

}

//一直向右走

static base_ptr maximum(base_ptr x)

{

while (x->right !=
)

x=x->right;

return x;

}

};

//__rb_tree_node

template<class Value>

struct __rb_tree_node :
public __rb_tree_node_base

{

typedef __rb_tree_node<Value>* link_type;

Value value_field;

};

//节点设计结束

/*

re_tree的迭代器

re_tree的迭代器和slist的迭代器很相似,都是分成两层结构

re_tree属于双向迭代器,但是不具备随机定位的能力

*/

//__rb_tree_base_iterator

struct __rb_tree_base_iterator

{

typedef __rb_tree_node_base::base_ptr  base_ptr;

typedef bidirectional_iterator_tag iterator_category;

typedef ptrdiff_t difference_type;

base_ptr node;

void increment()

{

if(node->right !=
)

{

node = node->right;

while (node->left !=
)

node =node->left;

}

//只要node是他父节点的右儿子,他就比他父亲大,我们要找比node大的

else

{

base_ptr y= node->parent;

while(node == y->right)

{

node = y;

y = y->parent;

}

if(node->right != y)//如果node是根节点,而且没有右儿子

node=y;

}

}

void decrement()

{

//这个发生在node时header或者end()时

if(node->color == __rb_tree_red &&

node->parent->parent == node)

node =node->right;

else
)

{

base_ptr y = node->left;

while(y->right !=
)

y = y->right;

node = y;

}

else

{

base_ptr y =node->parent;

while(node == y->left)

{

node = y;

y = y->parent;

}

node = y;

}

}

//原文说increment有4种状况,其实只有三种,状况2并不是一种结果

};

template<class Value,class Ref,class
Ptr>

struct __rb_tree_iterator :
public __rb_tree_base_iterator

{

typedef Value value_type;

typedef Ref reference;

typedef Ptr pointer;

typedef __rb_tree_iterator<Value, Value&, Value*> iterator;

typedef __rb_tree_iterator<Value,const Value&,const Value*> const_iterator;

typedef __rb_tree_iterator<Value, Ref, Ptr> self;

//经过多日的stl熏陶,感觉有些明白self和iterator的关系了,self是当前的节点本身(因为self用到的地方

//很多,可以简化书写,iterator是一个类型属性供别人使用的。

typedef __rb_tree_node<Value>* link_type;

//构造函数

__rb_tree_iterator(){}

__rb_tree_iterator(link_type x) {node = x;}

__rb_tree_iterator(const iterator& x) {node = it.node;}

referenceoperator*()
const {return link_type(node)->value_field;}

#ifdef __SGI_STL_NO_ARROW_OPERATOR

pointeroperator->()
const {return &(operator*());}

#endif

//++ --操作

self&operator++() {increment();
return *this;}

self&operator++(){increment();return *this;}

selfoperator++(int)

{

self tmp = *this;

increment();

return tmp;

}

self&operator--(){decrement();return *this;}

selfoperator--(int)

{

self tmp = *this;

decrement();

return tmp;

}

}

//class rb_tree

template<class Key,class Value,
class KeyOfValue,class Compare,

class Alloc = alloc>

class rb_tree

{

protected:

typedef
void* void_point;

typedef __rb_tree_node_base* base_ptr;

typedef __rb_tree_node<Value> rb_tree_node;

typedef simple_alloc<rb_tree_node, Alloc> rb_tree_node_allocator;

typedef __rb_tree_color_type color_type;

public:

typedef Key key_type;

typedef Value value_type;

typedef value_type* pointer;

typedef
const value_type* const_pointer;

typedef value_type& reference;

typedef
const value_type& const_reference;

typedef rb_tree_node* link_type;

typedef size_t size_type;

typedef ptrdiff_t difference_type;

protected:

//内存分配和释放

link_type get_node()

{

return rb_tree_node_allocator::allocate();

}

void put_node(link_type p)

{

rb_tree_node_allocator::deallocate(p);

}

//申请内存并调用构造函数等于new操作

link_type create_node(const value_type& x)

{

link_type tmp = get_node();

__STL_TRY

{

construct(&tmp->value_field,x);

}

__STL_UNWIND(put_node(tmp));

return tmp;

}

//克隆一个节点,只有颜色和键值,不会拷贝关系(左右儿子) 有个疑问,为什么parent没有赋0

link_type clone_node(link_type x)

{

link_type tmp = create_node(x->value_field);

tmp->color = x->color;

tmp->left =;

tmp->right =;

return temp;

}

void destroy_node(link_type p)

{

destroy(&p->value_field);

put_node(p);

}

protected:

size_type node_count;

link_type header;

Compare key_compare;

link_type& root()const {return (link_type&) header->parent;}

link_type& leftmost()const {return (link_type&) header->left;}

link_type rightmost()const {return (link_type&) header->right;}

//一下12个函数全部为了简化取成员操作

static link_type& left(link_type x)

{

return (link_type&)(x->left);

}

static link_type& right(link_type x)

{

return (link_type&)(x->right);

}

static link_type& parent(link_type x)

{

return (link_type&)(x->parent);

}

static reference& value(link_type x)

{

return x->value_field;

}

static
const Key& key(link_type x)

{

return KeyOfValue()( value(x));

}

static color_type& color(link_type x)

{

return (color_type&)(x->color);

}

static link_type& left(base_ptr x)

{

return (link_type&)(x->left);

}

static link_type& right(base_ptr x)

{

return (link_type&)(x->right);

}

static link_type& parent(base_ptr x)

{

return (link_type&)(x->parent);

}

static reference& value(base_ptr x)

{

return ((link_type)x)->value_field;

}

static
const Key& key(base_ptr x)

{

return KeyOfValue()( value(link_type(x)));

}

static color_type& color(base_ptr x)

{

return (color_type&)(link_type(x)->color);

}

//求最大和最小值

static link_type minimum(link_type x)

{

return (link_type) __rb_tree_node_base::minimum(x);

}

static link_type maximum(link_type x)

{

return (link_type) __rb_tree_node_base::maximum(x);

}

public:

typedef __rb_tree_iterator<value_type, reference, pointer> iterator;

private:

iterator __insert(base_ptr x, base_ptr y,const value_type& v);

link_type __copy(link_type x, link_type p);

void __erase(link_type x);

//初始化,给header申请一个节点的内存,让root(header->parent)为0,让左右最值都为header,并设置成红色

void init()

{

header = get_node();

color(header) = __rb_tree_red;

root() =;

leftmost() = header;

rightmost() =header;

}

public:

rb_tree(const Compare& comp=Compare()

: node_count(), key_compare(comp))

{

init();

}

~rb_tree()

{

clear();

put_node(header);

}

rb_tree<Key, Value, KeyOfValue, Compare, Alloc>&

operator=(const rb_tree<Key, Value, KeyOfValue, Compare, Alloc>& x);

public:

//各种基础操作

Compare key_comp()const {return key_compare;}

iterator begin() {return leftmost();}

iterator end() {return header;}

bool empty()
;}

size_type size()const {return node_count;}

size_type max_size());}

public:

pair<iterator,bool> insert_unique(const value_type& x);

};

stl源码剖析 详细学习笔记 RB_tree (1)的更多相关文章

  1. stl源码剖析 详细学习笔记 RB_tree (2)

    //---------------------------15/03/22---------------------------- //一直好奇KeyOfValue是什么,查了下就是一个和仿函数差不多 ...

  2. stl源码剖析 详细学习笔记 set map

    // //  set map.cpp //  笔记 // //  Created by fam on 15/3/23. // // //---------------------------15/03 ...

  3. stl源码剖析 详细学习笔记 hashtable

    //---------------------------15/03/24---------------------------- //hashtable { /* 概述: sgi采用的是开链法完成h ...

  4. stl源码剖析 详细学习笔记heap

    // //  heap.cpp //  笔记 // //  Created by fam on 15/3/15. // // //---------------------------15/03/15 ...

  5. stl源码剖析 详细学习笔记 空间配置器

    //---------------------------15/04/05---------------------------- /* 空间配置器概述: 1:new操作包含两个阶段操作 1>调 ...

  6. stl源码剖析 详细学习笔记 算法(1)

    //---------------------------15/03/27---------------------------- //算法 { /* 质变算法:会改变操作对象之值 所有的stl算法都 ...

  7. stl源码剖析 详细学习笔记 算法总览

    //****************************基本算法***************************** /* stl算法总览,不在stl标准规格的sgi专属算法,都以 *加以标 ...

  8. stl源码剖析 详细学习笔记 hashset hashmap

    //---------------------------15/03/26---------------------------- //hash_set { /* hash_set概述: 1:这是一个 ...

  9. stl源码剖析 详细学习笔记priority_queue slist

    // //  priority_queue.cpp //  笔记 // //  Created by fam on 15/3/16. // // //------------------------- ...

随机推荐

  1. Azure 元数据服务:适用于 Windows VM 的计划事件(预览)

    计划事件是 Azure 元数据服务中的其中一个子服务. 它负责显示有关即将发生的事件(例如,重新启动)的信息,使应用程序可以为其做准备并限制中断. 它可用于所有 Azure 虚拟机类型(包括 PaaS ...

  2. SQL Server中数据库文件的存放方式,文件和文件组 (转载)

    简介 在SQL SERVER中,数据库在硬盘上的存储方式和普通文件在Windows中的存储方式没有什么不同,仅仅是几个文件而已.SQL SERVER通过管理逻辑上的文件组的方式来管理文件.理解文件和文 ...

  3. 安装和配置Apache服务器(下)

    Apache的配置文档:http://httpd.apache.org/docs/current/. 1.监听端口: 默认的端口号为80端口,如果端口号冲突改为8080端口. 注:每改一次httpd. ...

  4. Java数组、集合的三种遍历方式(包懂)

    1 for循环 for(int i = 0;i<arr.length;i++){ System.out.print(arr[i]+" "); } 2 foreach循环,这种 ...

  5. 阿里八八Alpha阶段Scrum(11/12)

    今日进度 叶文滔: 合并日程界面debug成功,但是目前出现了新的问题,日程界面一些控件无法适配屏幕,正在排查问题 李嘉群: 尝试用okhttp的方式发送请求 王国超: 今天开始进行recycerli ...

  6. 2.js深入(以通俗易懂的语言解释JavaScript)

    1.函数返回值: 即函数的执行结果 可以没有return 经验:一个函数应该只返回一种类型的值 2.函数传参 可变参(不定参):arguments ——>(参数的个数可变,参数数组) 例子1:求 ...

  7. 实现统计 android手机 CPU使用率

    # -*- coding:utf-8 -*- ''' Created on Sep 10, 2018 @author: SaShuangYiBing ''' import subprocess imp ...

  8. git版本管理工具-git的概述

    什么是git Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目的一种工具 Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不 ...

  9. 协程运行原理猜测: async/await

    1.根据await调用链寻找最终的生产者或服务提供者: 2.请求服务: 3.进行执行环境切换,跳出顶层函数(第一个无await修饰的函数),执行后面的语句: 4.服务完成,将服务数据复制给最底层的aw ...

  10. PHP缓存锁原理及利用

    原文链接:https://blog.csdn.net/tim_phper/article/details/54949404 概述: 项目当中经常要考虑数据高并发的情况,为了避免并发导致出现一些资源重复 ...