当做笔记记录下来:)

相比于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的实现的更多相关文章

  1. 探索c#之跳跃表(SkipList)

    阅读目录: 基本介绍 算法思想 演化步骤 实现细节 总结 基本介绍 SkipList是William Pugh在1990年提出的,它是一种可替代平衡树的数据结构. SkipList在实现上相对比较简单 ...

  2. SkipList算法实现

    SkipList是一种快速查找链表,链表元素的是有序的.由W.Pugh发明于1989年.其算法复杂度如下: Average Worst caseSpace O(n) O(n log n)Search ...

  3. [转载] 跳表SkipList

    原文: http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html leveldb中memtable的思想本质上是一个skiplist ...

  4. 浅析SkipList跳跃表原理及代码实现

    本文将总结一种数据结构:跳跃表.前半部分跳跃表性质和操作的介绍直接摘自<让算法的效率跳起来--浅谈“跳跃表”的相关操作及其应用>上海市华东师范大学第二附属中学 魏冉.之后将附上跳跃表的源代 ...

  5. 跳表SkipList

    原文:http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html 跳表SkipList   1.聊一聊跳表作者的其人其事 2. 言归正 ...

  6. skiplist 跳表(2)-----细心学习

    快速了解skiplist请看:skiplist 跳表(1) http://blog.sina.com.cn/s/blog_693f08470101n2lv.html 本周我要介绍的数据结构,是我非常非 ...

  7. skiplist 跳表(1)

    最近学习中遇到一种新的数据结构,很实用,搬过来学习. 原文地址:skiplist 跳表   为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. ...

  8. 计算机程序的思维逻辑 (75) - 并发容器 - 基于SkipList的Map和Set

    上节我们介绍了ConcurrentHashMap,ConcurrentHashMap不能排序,容器类中可以排序的Map和Set是TreeMap和TreeSet,但它们不是线程安全的.Java并发包中与 ...

  9. redis skiplist (跳跃表)

    redis skiplist (跳跃表) 概述 redis skiplist 是有序的, 按照分值大小排序 节点中存储多个指向其他节点的指针 结构 zskiplist 结构 // 跳跃表 typede ...

  10. SkipList跳表基本原理

    为什么选择跳表 目前经常使用的平衡数据结构有:B树,红黑树,AVL树,Splay Tree, Treep等. 想象一下,给你一张草稿纸,一只笔,一个编辑器,你能立即实现一颗红黑树,或者AVL树 出来吗 ...

随机推荐

  1. crontab执行python报错原因总结

    1.相对路径导致   2.环境变量问题,py脚本首行应指定python路径,不能用软链接   3.python3.3是默认utf-8,需要 &&脚本   最好使用crontab -e ...

  2. (四)mysql数据类型

    数据类型基本介绍 数值类型 整形类型:tinyint,int,bigint 浮点类型:float,double 字符串类型 char系列:char varchar text系列:text blob系列 ...

  3. flutter 修改anroid默认example包名

  4. HDU 多校1.5

    Expectation Division Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  5. 贮油点问题(C++)

    贮油点问题…..一道送命系列的递推… 描述 Description 一辆重型卡车欲穿过S公里的沙漠,卡车耗汽油为1升/公里,卡车总载油能力为W公升.显然卡车装一次油是过不了沙漠的.因此司机必须设法在沿 ...

  6. Diamond Collector (动态规划)

    问题 I: Diamond Collector 时间限制: 1 Sec  内存限制: 64 MB提交: 22  解决: 7[提交][状态][讨论版] 题目描述 Bessie the cow, alwa ...

  7. 浅析module.exports和exports区别和使用

    module.exports和exports 写node的时候,特别是自定义模块的时候,都是一顿乱敲,然后module.exports={}完事. 但有时候去看别人写的代码的时候会发现还可以expor ...

  8. sed replace HEX sequence in your binary file:

    Here is how to replace a HEX sequence in your binary file: $ sed 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x5 ...

  9. python 统计发送请求到接收response的时间

    由于需要测试请求一个接口所耗用的时间,在网上查找资料也麻烦,所以自己总结一下 找到elapsed 函数 ,按照文档说的是获取请求发出的时间至响应到达经过的时间,,具体用法如下: 执行的结果是 微秒 单 ...

  10. 【点分治】hdu5016 Mart Master II

    点分治好题. ①手动开栈. ②dp预处理每个点被哪个市场控制,及其距离是多少,记作pair<int,int>数组p. ③设dis[u].first为u到重心s的距离,dis[u].seco ...