当做笔记记录下来:)

相比于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. poj 3230(初始化。。动态规划)

    Travel Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4353   Accepted: 1817 Descriptio ...

  2. typescript 定义全局变量以及扩展原生js对象

    使用“declare global”操作即可. 项目根目录下新建myDeclareFile.d.ts declare global { interface Navigator { mediaSessi ...

  3. centos6.5 卸载adobeflash

    # rpm -e flash-plugin # rpm -qa | grep ^flash-plugin

  4. ubantu启动盘制作

    转载自http://jingyan.baidu.com/article/b24f6c82cf50e086bfe5dae9.html 1 首先打开UltraISO软件,没有的请百度搜索,下载安装,尽量下 ...

  5. Codeforces Round #442 A Alex and broken contest【字符串/常量数组/string类】

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club

    Doki Doki Literature Club Time Limit: 1 Second      Memory Limit: 65536 KB Doki Doki Literature Club ...

  7. 树状数组【CF703D】Mishka and Interesting sum

    Description 给你n( 1<=n<=1000000)个数,以及m(1<=m<=1000000)个询问,每个询问包括l和r,问你在这n个数中,区间l~r,出现偶数个数的 ...

  8. notepad++ remove duplicate line

    To remove duplicate lines just press Ctrl + F, select the “Replace” tab and in the “Find” field, pla ...

  9. Ubuntu下创建程序启动器

    1 以上是使用手动的方法,程序有图标 其中,配置文件改为: [Desktop Entry] Version=1.0 Name=eclipse Exec=/home/hu/soft/eclipse/ec ...

  10. freedom isn't free

    财务自由(除去房和车) 第一阶段: 个人存款达到50万以上 第二阶段 个人存款100~200万 第三阶段 个人存款400万以上 第三阶段以上才能算实现了相对较好的财务自由!come on , boys ...