SkipList的实现
当做笔记记录下来:)
相比于bTree,skiplist占用更多的空间。
而在并发的环境下skiplist会比bTree效率更高。
(bTree插入的时候需要rebalance,需要lock比较多的结点,而skiplist则只需要lock跟node相关的几个结点)
SkipList.h
#define MAX_LEVEL 16 typedef int KeyType;
typedef int ValueType; typedef struct nodeStructure* Node;
struct nodeStructure
{
KeyType key;
ValueType value;
Node forward[];
}; class SkipList
{
public:
SkipList();
Node createNode(int level, const KeyType& key, const ValueType& value);
void createList();
bool insert(const KeyType& key, const ValueType& value);
bool erase(const KeyType& key, ValueType& value);
bool search(const KeyType& key, ValueType& value);
private:
int randomLevel();
private:
int _level;
Node _header;
};
SkipList.cpp
#include "SkipList.h"
#include <iostream> SkipList::SkipList()
{
_level = ;
_header = createNode(MAX_LEVEL, , );
Node tail = createNode(, 0x7fffffff, );
for (int i = ; i < MAX_LEVEL; ++i)
_header->forward[i] = tail; } Node SkipList::createNode(int level, const KeyType& key, const ValueType& value)
{
Node node = (Node)malloc(
sizeof(nodeStructure) + sizeof(Node) * level);
node->key = key;
node->value = value;
return node;
} int SkipList::randomLevel()
{
int k = ;
// 50% k++
while (rand() % )
k++;
return (k < MAX_LEVEL) ? k : MAX_LEVEL - ;
} bool SkipList::insert(const KeyType& key, const ValueType& value)
{
Node update[MAX_LEVEL];
Node node = _header;
// search
for (int i = _level; i >= ; --i)
{
while (node->forward[i]->key < key)
node = node->forward[i];
// record previous node
update[i] = node;
} // same key
if (node->forward[]->key == key)
return false; int level = randomLevel();
// update level
if (level > _level)
{
for (int i = _level + ; i <= level; ++i)
update[i] = _header;
_level = level;
} // update pointer
Node insNode = createNode(level, key, value);
for (int i = level; i >= ; --i)
{
node = update[i];
insNode->forward[i] = node->forward[i];
node->forward[i] = insNode;
}
return true;
} // similar to insert
bool SkipList::erase(const KeyType& key, ValueType& value)
{
Node update[MAX_LEVEL];
Node node = _header;
for (int i = _level; i >= ; --i)
{
while (node->forward[i]->key < key)
node = node->forward[i];
update[i] = node;
} node = node->forward[];
if (node->key != key)
return false; for (int i = ; i < _level; ++i)
{
if (update[i]->forward[i] != node)
break;
update[i]->forward[i] = node->forward[i];
}
free(node);
return true;
} bool SkipList::search(const KeyType& key, ValueType& value)
{
Node node = _header;
for (int i = _level; i >= ; --i)
// find the last node->key < key
while (node->forward[i]->key < key)
node = node->forward[i];
// node->key >= key
node = node->forward[];
if (node->key == key)
{
value = node->value;
return true;
}
return false;
}
SkipList的实现的更多相关文章
- 探索c#之跳跃表(SkipList)
阅读目录: 基本介绍 算法思想 演化步骤 实现细节 总结 基本介绍 SkipList是William Pugh在1990年提出的,它是一种可替代平衡树的数据结构. SkipList在实现上相对比较简单 ...
- SkipList算法实现
SkipList是一种快速查找链表,链表元素的是有序的.由W.Pugh发明于1989年.其算法复杂度如下: Average Worst caseSpace O(n) O(n log n)Search ...
- [转载] 跳表SkipList
原文: http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html leveldb中memtable的思想本质上是一个skiplist ...
- 浅析SkipList跳跃表原理及代码实现
本文将总结一种数据结构:跳跃表.前半部分跳跃表性质和操作的介绍直接摘自<让算法的效率跳起来--浅谈“跳跃表”的相关操作及其应用>上海市华东师范大学第二附属中学 魏冉.之后将附上跳跃表的源代 ...
- 跳表SkipList
原文:http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html 跳表SkipList 1.聊一聊跳表作者的其人其事 2. 言归正 ...
- skiplist 跳表(2)-----细心学习
快速了解skiplist请看:skiplist 跳表(1) http://blog.sina.com.cn/s/blog_693f08470101n2lv.html 本周我要介绍的数据结构,是我非常非 ...
- skiplist 跳表(1)
最近学习中遇到一种新的数据结构,很实用,搬过来学习. 原文地址:skiplist 跳表 为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. ...
- 计算机程序的思维逻辑 (75) - 并发容器 - 基于SkipList的Map和Set
上节我们介绍了ConcurrentHashMap,ConcurrentHashMap不能排序,容器类中可以排序的Map和Set是TreeMap和TreeSet,但它们不是线程安全的.Java并发包中与 ...
- redis skiplist (跳跃表)
redis skiplist (跳跃表) 概述 redis skiplist 是有序的, 按照分值大小排序 节点中存储多个指向其他节点的指针 结构 zskiplist 结构 // 跳跃表 typede ...
- SkipList跳表基本原理
为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. 想象一下,给你一张草稿纸,一只笔,一个编辑器,你能立即实现一颗红黑树,或者AVL树 出来吗 ...
随机推荐
- 删除某个ros包之后catkin_make冒错
CMake Error at /home/ubuntu/Workspaces/rosProject/workspace1/devel/share/costmap_2d/cmake/costmap_2d ...
- MATLAB作图方法与技巧(三)
1.利用指令plot绘制圆的参数方程x = sin(t),y = cos(t),(0<=t<=2*pi)的曲线图. 代码如下 t = linspace(0,2*pi,100); x = s ...
- E - A strange lift 【数值型BFS+上下方向】
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...
- 模板—字符串—KMP(单模式串,单文本串)
模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm& ...
- 集训day15 t1 poj3728
[问题描述] 有一颗n个节点的树 每个节点上都有许多奸商在卖东西,第i个奸商的理想价格为vi,即他会以vi的价格购买或卖出一件东西 有m个人希望从树上的某个点走到另一个点,问你在只进行一次买卖(每次仅 ...
- Axis2 解析
代码生成 Java to WSDL:WSDL to Java:XSD to WSDL:WSDL to XML:WSDL to SOAP:WSDL to Service: Apache Axis2 ...
- 【后缀数组】poj3693 Maximum repetition substring
sa在清空方面存在一些奇怪的问题……难以ac.(留坑?)
- Bootstrap-datetimepicker日期插件简单使用
写在前面: 日期组件有很多,这里简单的记录下bootstrap的一个日期插件datetimepicker,使用方法比较简单,基本上看一些就会了,但是还是记录下. 这个就不过多的说了,简单粗暴上代码 & ...
- 【R笔记】给R加个编译器——notepad++
R的日记-给R加个编译器 转载▼ R是一款强大免费且开源的统计分析软件,这是R的长处,可也是其“缺陷”的根源:不似商业软件那样user-friendly.记得初学R时,给我留下最深印象的不是其功能的强 ...
- hadoop map(分片)数量确定
之前学习hadoop的时候,一直希望可以调试hadoop源码,可是一直没找到有效的方法,今天在调试矩阵乘法的时候发现了调试的方法,所以在这里记录下来. 1)事情的起因是想在一个Job里设置map的数量 ...