当做笔记记录下来:)

相比于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. 《锋利的JQuery》读书要点笔记1——认识JQuery&&选择器

    <锋利的jQuery>源码下载,包括了这本书中全部代码以及用到的CSS文件 第一章 认识jQuery jQuery是个Js库.首先该明确的一点是:在jQuery库中$就是jQuery的一个 ...

  2. python-函数(装饰器)

    装饰器 装饰器的主要功能: 在不改变函数调用方式的基础上在函数的前.后添加功能. 装饰器的固定格式: #装饰器的本质 :闭包函数 #功能:就是在不改变原函数调用方式的情况下,在这个函数前后加上扩展功能 ...

  3. springBoot 发布war包

    1.packaging 改为war <packaging>war</packaging> 2.剔除内置tomcat <dependency> <groupId ...

  4. Unable to connect to vulnerability scanner

    问题:   "Unable to connect to vulnerability scanner. If the system has been updated recently the ...

  5. 洛谷 P1316 丢瓶盖【二分答案】

    题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? 输入输出 ...

  6. luogu P1038借教室【Noip提高组2012】

    这道题我读完题目的第一感觉是: 这不就是个线段树??用线段树维护区间最小值,检查是否满足订单要求即可判断. 对于修改操作直接在区间上进行. 据说会卡一卡线段树,但是貌似写一个懒标记,连zkw线段树都不 ...

  7. Counting Haybales (线段树)

    Counting Haybales 时间限制: 50 Sec  内存限制: 256 MB提交: 52  解决: 18[提交][状态][讨论版] 题目描述 Farmer John is trying t ...

  8. 【博弈论】【SG函数】【枚举】bzoj1874 [BeiJing2009 WinterCamp]取石子游戏

    枚举第一步可能达到的状态,判断是否是必败态即可. #include<cstdio> #include<set> #include<cstring> using na ...

  9. SecureCRT实现跳板机自动登录

    背景: 1.通常运维会开放几个内网的机器能跳转到外网机器进行访问,这样的就是跳板机. 2.比如线上有120.0.0.2这台机器,而内网192.168.1.2这台连接了VPN,能通过SSH登录120.0 ...

  10. 简单说说DNS劫持_firefox吧_百度贴吧

    简单说说DNS劫持_firefox吧_百度贴吧 DNSSEC