当做笔记记录下来:)

相比于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. 服务器老是出现502 Bad Gateway?

    给你的upstream增加keepalive配置没有? upstream backend { server http://127.0.0.1:8888; keepalive 32; } 在nginx和 ...

  2. F - Oil Deposits 【地图型BFS+联通性】

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  3. log4j笔记:升级2.X版本的日志滚动问题

    因为slf4j依赖的log4j在流量大的时候总遇到多线程引起的死锁问题,升级到log4j2.x版本.原来的log4j.properties配置文件已经不被log4j2支持了,需要改写为log4j2.x ...

  4. [BZOJ5006][LOJ#2290][THUWC2017]随机二分图(概率+状压DP)

    https://loj.ac/problem/2290 题解:https://blog.csdn.net/Vectorxj/article/details/78905660 不是很好理解,对于边(x1 ...

  5. 【带修莫队】bzoj2120 数颜色

    块大小为n2/3. 把询问和修改分开. 每次两个询问之间的修改进行暴力转移,如果修改在上一次询问的区间里,就会对当前状态形成影响. 好慢. #include<cstdio> #includ ...

  6. 【函数式权值分块】【分块】bzoj3196 Tyvj 1730 二逼平衡树

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...

  7. 【暴力】bzoj3713 [PA2014]Iloczyn

    没什么好说的. #include<cstdio> using namespace std; typedef long long ll; ]; int main() { scanf(]=; ...

  8. GetAdaptersInfo & GetAdaptersAddresses

    I use GetAdaptersInfo to get MAC addresses of interfaces.   GetAdaptersInfo exist on old and new ver ...

  9. Delphi制作软键盘

        { 作者: han 日期: 2006.06.02 } unit softkey; interface uses Windows, Messages, SysUtils, Variants, C ...

  10. mysql 5.7.20解压版安装配置

    MySql 5.7.20版本免安装版配置过程   下载地址为: https://dev.mysql.com/downloads/mysql/ 最下面根据自己的操作系统选择合适的型号 下载完以后解压缩到 ...