取自网络https://github.com/spratt/SkipList

AbstractSortedSet.java

package skiplist_m;
/******************************************************************************
* AbstractSortedSet *
* *
* Extends AbstractSet and implements SortedSet, and contains stub methods *
* *
* View README file for information about this project. *
* View LICENSE file for license information. *
******************************************************************************/ import java.util.*; abstract class AbstractSortedSet<E>
extends AbstractSet<E>
implements SortedSet<E> { public E first() {
return null;
} public E last() {
return null;
} public Iterator<E> iterator() {
return null;
} public SortedSet<E> headSet(E toElement) {
return null;
} public SortedSet<E> tailSet(E fromElement) {
return null;
} public SortedSet<E> subSet(E fromElement, E toElement) {
return null;
} public Comparator<? super E> comparator() {
return null; // uses natural ordering
}
}

SkipList.java

package skiplist_m;
/******************************************************************************
* Skiplist *
* *
* View README file for information about this project. *
* View LICENSE file for license information. *
******************************************************************************/ import java.util.Iterator; public class SkipList<E extends Comparable<E>> extends AbstractSortedSet<E> {
private SkipListNode<E> head;
private int maxLevel;
private int size; private static final double PROBABILITY = 0.5; public SkipList() {
size = 0;
maxLevel = 0;
// a SkipListNode with value null marks the beginning
head = new SkipListNode<E>(null);
// null marks the end
head.nextNodes.add(null);
} public SkipListNode getHead() {
return head;
} // Adds e to the skiplist.
// Returns false if already in skiplist, true otherwise.
public boolean add(E e) {
if(contains(e)) return false;
size++;
// random number from 0 to maxLevel+1 (inclusive)
int level = 0;
while (Math.random() < PROBABILITY)
level++;
while(level > maxLevel) { // should only happen once
head.nextNodes.add(null);
maxLevel++;
}
SkipListNode newNode = new SkipListNode<E>(e);
SkipListNode current = head;
do {
current = findNext(e,current,level);
newNode.nextNodes.add(0,current.nextNodes.get(level));
current.nextNodes.set(level,newNode);
} while (level-- > 0);
return true;
} // Returns the skiplist node with greatest value <= e
private SkipListNode find(E e) {
return find(e,head,maxLevel);
} // Returns the skiplist node with greatest value <= e
// Starts at node start and level
private SkipListNode find(E e, SkipListNode current, int level) {
do {
current = findNext(e,current,level);
} while(level-- > 0);
return current;
} // Returns the node at a given level with highest value less than e
private SkipListNode findNext(E e, SkipListNode current, int level) {
SkipListNode next = (SkipListNode)current.nextNodes.get(level);
while(next != null) {
E value = (E)next.getValue();
if(lessThan(e,value)) // e < value
break;
current = next;
next = (SkipListNode)current.nextNodes.get(level);
}
return current;
} public int size() {
return size;
} public boolean contains(Object o) {
E e = (E)o;
SkipListNode node = find(e);
return node != null &&
node.getValue() != null &&
equalTo((E)node.getValue(),e);
} public Iterator<E> iterator() {
return new SkipListIterator(this);
} /******************************************************************************
* Utility Functions *
******************************************************************************/ private boolean lessThan(E a, E b) {
return a.compareTo(b) < 0;
} private boolean equalTo(E a, E b) {
return a.compareTo(b) == 0;
} private boolean greaterThan(E a, E b) {
return a.compareTo(b) > 0;
} /******************************************************************************
* Testing *
******************************************************************************/ public static void main(String[] args) {
SkipList testList = new SkipList<Integer>();
System.out.println(testList);
testList.add(4);
System.out.println(testList);
testList.add(1);
System.out.println(testList);
testList.add(2);
System.out.println(testList);
testList = new SkipList<String>();
System.out.println(testList);
testList.add("hello");
System.out.println(testList);
testList.add("beautiful");
System.out.println(testList);
testList.add("world");
System.out.println(testList);
} public String toString() {
String s = "SkipList: ";
for(Object o : this)
s += o + ", ";
return s.substring(0,s.length()-2);
}
}

SkipListIterator.java

package skiplist_m;
/******************************************************************************
* SkipListIterator *
* *
* View README file for information about this project. *
* View LICENSE file for license information. *
******************************************************************************/ import java.util.*; public class SkipListIterator<E extends Comparable<E>> implements Iterator<E> {
SkipList<E> list;
SkipListNode<E> current; public SkipListIterator(SkipList<E> list) {
this.list = list;
this.current = list.getHead();
} public boolean hasNext() {
return current.nextNodes.get(0) != null;
} public E next() {
current = (SkipListNode<E>)current.nextNodes.get(0);
return (E)current.getValue();
} public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}

SkipListNode.java

package skiplist_m;
/******************************************************************************
* SkipListNode *
* *
* View README file for information about this project. *
* View LICENSE file for license information. *
******************************************************************************/ import java.util.*; public class SkipListNode<E> {
private E value;
public List<SkipListNode<E> > nextNodes; public E getValue() {
return value;
} public SkipListNode(E value) {
this.value = value;
nextNodes = new ArrayList<SkipListNode<E> >();
} public int level() {
return nextNodes.size()-1;
} public String toString() {
return "SLN: " + value;
}
}

SkipList跳跃表(Java实现)的更多相关文章

  1. 小白也能看懂的Redis教学基础篇——朋友面试被Skiplist跳跃表拦住了

    各位看官大大们,双节快乐 !!! 这是本系列博客的第二篇,主要讲的是Redis基础数据结构中ZSet(有序集合)底层实现之一的Skiplist跳跃表. 不知道那些是Redis基础数据结构的看官们,可以 ...

  2. redis skiplist (跳跃表)

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

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

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

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

    SkipList在Leveldb以及lucence中都广为使用,是比较高效的数据结构.由于它的代码以及原理实现的简单性,更为人们所接受.首先看看SkipList的定义,为什么叫跳跃表? "S ...

  5. 【Redis】skiplist跳跃表

    有序集合Sorted Set zadd zadd用于向集合中添加元素并且可以设置分值,比如添加三门编程语言,分值分别为1.2.3: 127.0.0.1:6379> zadd language 1 ...

  6. SkipList 跳跃表

    引子 考虑一个有序表:14->->34->->50->->66->72 从该有序表中搜索元素 < 23, 43, 59 > ,需要比较的次数分别为 ...

  7. 算法: skiplist 跳跃表代码实现和原理

    SkipList在leveldb以及lucence中都广为使用,是比较高效的数据结构.由于它的代码以及原理实现的简单性,更为人们所接受. 所有操作均从上向下逐层查找,越上层一次next操作跨度越大.其 ...

  8. 5分钟了解Redis的内部实现跳跃表(skiplist)

    跳跃表简介 跳跃表(skiplist)是一个有序的数据结构,它通过在每个节点维护不同层次指向后续节点的指针,以达到快速访问指定节点的目的.跳跃表在查找指定节点时,平均时间复杂度为,最坏时间复杂度为O( ...

  9. 跳跃表Skip List的原理和实现

    >>二分查找和AVL树查找 二分查找要求元素可以随机访问,所以决定了需要把元素存储在连续内存.这样查找确实很快,但是插入和删除元素的时候,为了保证元素的有序性,就需要大量的移动元素了.如果 ...

随机推荐

  1. 为什么mfc的入口是InitInstance()而没有WinMain() (转)

    学过PE文件格式,就明白,程序在进入WinMain之前要做很多事情,比如初始Dos头,分配函数表,初始化全局变量,之后才进入程序入口(WinMain) MFC对WindowsAPI进行了封装.在用向导 ...

  2. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---1

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: <Linux命令行与shell脚本 ...

  3. MSP430 G2553 寄存器列表与引脚功能

    USCI_B0 USCI_B0 发送缓冲器UCB0TXBUF 06Fh USCI_B0 接收缓冲器UCB0RXBUF 06Eh USCI_B0 状态UCB0STAT 06Dh USCI B0 I2C ...

  4. linux下不是很完美的提高android虚拟机的启动速度

    去年双十一换的新电脑,华硕vivo4000的,配置的不算很好,4k的屏幕:3840×2160, 940M的显卡, core i7的CPU, 8G的内存,硬盘是1T的机械硬盘,除了硬盘基本感觉还可以吧. ...

  5. UVALive 3664:Guess(贪心 Grade E)

    vj题目链接 题意: 有n (n<16345)个人,每个人有三个数(小于1000且最多两位小数点),表示答对对应题的得分.规定总分越高的人rank越高.总分相同,id小的rank高.现在知道ra ...

  6. Python Challenge 第十关

    第十关是一张牛的图片和一行字:len(a[30])=?.图片中的牛是一个链接,点开后进入一个新页面,只有一行字: a = [1, 11, 21, 1211, 111221, 看来要知道第31个数多长, ...

  7. Android与H5互调

    前言 微信,微博,微商,QQ空间,大量的软件使用内嵌了H5,这个时候就需要了解Android如何更H5交互的了:有些外包公司,为了节约成本,采用Android内嵌H5模式开发,便于在IOS上直接复用页 ...

  8. failed to obtain a cell from its dataSource 问题处理

    最近在处理bugly问题的时候,总会看到回话列表有奔溃,但是由于没有啥具体的细节原因也无从下手. 只知道ConversationListViewController这个类的奔溃,报的问题是这个,也只有 ...

  9. Implement Trie (Prefix Tree) - LeetCode

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  10. 【spring boot】10.spring boot下的单元测试

    spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...