算法导论14(数据结构的扩张)
2015-05-29 20:15
435 查看
14.1 动态顺序统计
顺序统计树(order-statistic tree)只是简单地在每个结点上存储附加信息的一颗红黑树,使得可以在O(logn)时间内确定任何的顺序统计量。
14.2 如何扩张数据结构
14.3 区间树
区间树(interval tree)是一种对动态集合进行维护的红黑树。
顺序统计树(order-statistic tree)只是简单地在每个结点上存储附加信息的一颗红黑树,使得可以在O(logn)时间内确定任何的顺序统计量。
struct node { int key,color,size; node *left,*right,*p; node():color(BLACK){} }; //查找具有给定秩的元素,时间复杂度为O(logn)。 node *OSSelect(node *x,int i) { int r=x->left->size+1; if(i==r)return x; else if(i<r)return OSSelect(x->left,i); else return OSSelect(x->right,i-r); } //确定一个元素的秩,时间复杂度为O(logn)。 int OSRank(node *root,node *x) { int r=x->left->size+1; node *y=x; while(y!=root) { if(y==y->p->right)r+=y->p->left->size+1; y=y->p; } return r; } //对子树规模的维护 //(1)插入:对由根至叶子的路径上遍历的每一个结点x,都增加x->size属性。新增加结点的size为1。 //(2)删除:我们只需要遍历一条由结点y(从它在树中的原始位置开始)至根的简单路径,并减少路径上每个结点的size属性的值。 //(3)在左旋和右旋中增加下面两行。 y->size=x->size; x->size=x->left->size+x->right->size+1;
14.2 如何扩张数据结构
14.3 区间树
区间树(interval tree)是一种对动态集合进行维护的红黑树。
struct node { int key,color; node *left,*right,*p; pair<int,int> int; node():color(BLACK){} }; //判断i和j是否重叠。 bool overlap(pair<int,int> i,pair<int,int> j) { return i.first<j.second&&j.first<i.second; } //返回一个指向区间树中元素x的指针,使x->int与i重叠;若此元素不存在,则返回nil。时间复杂度为O(logn)。 node *intervalSearch(node *root,pair<int,int> i) { node *x=root; while(x!=nil&&overlap(i,x->int)) { if(x->left!=nil&&x->left->max>=i.first)x=x->left; else x=x->right; } return x; } //对信息的维护 x->max=max(max(x->int.second,x->left->max),x->right->max);
相关文章推荐
- 数据结构面试题总结3——数组:求数组中两个元素的最小距离
- 星际之门(一)
- C#数据结构回顾之循环队列
- Dancing Links讲解和练习整理
- 运行时数据结构
- C#数据结构回顾之链栈
- 数据结构练习 02-线性结构3. Pop Sequence (25)
- 该学学数据结构了,不会数据结构真是寸步难行啊。。。。。
- 深入云存储系统Swift核心组件:Ring数据结构及构建、重平衡操作
- LeetCode---题目难度、出题频率、数据结构、考察点统计
- 【转】MySQL索引背后的数据结构及算法原理
- 堆、堆栈与数据结构中的堆
- 数据结构(C语言版)第一版 习题P37~P38
- 大顶堆排序C实现--【严蔚敏数据结构实现】
- 数据结构之单链表(对单链表的所有操作)
- 数据结构面试题总结2——数组:求出现次数超过一半的元素
- 【数据结构】栈的应用--括号的匹配(c++)
- 数据结构面试题总结1——数组:求最大、次大值
- 数据结构之---C语言实现数组的顺序存储表示(可运行)
- 【数据结构】循环队列的实现(c++)