Java SkipList 实现

Java SkipList 实现packagedatas importjava util Random publicclassS publicSkipLi tail publicintn size publicinth high publicRandom publicSkipLi SkipListE

package datastruct; import java.util.Random; public class SkipList { public SkipListEntry head,tail; public int n;//size public int h;//high public Random r; public SkipList() { SkipListEntry p1,p2; p1 = new SkipListEntry(Integer.MIN_VALUE); p2 = new SkipListEntry(Integer.MAX_VALUE); p1.right = p2; p2.left = p1; head = p1; tail = p2; n = 0; h = 0; r = new Random(); } public Integer get(Integer k) { SkipListEntry p = findEntry(k); return p.value == k ? p.value : null; } public void put(Integer k) { SkipListEntry p = findEntry(k); if (p.value == k) { //已有了 就不插入了 return; } //insert k with random height SkipListEntry q = new SkipListEntry(k); q.left = p; q.right = p.right; p.right.left = q; p.right = q; int i = 0; while (r.nextDouble() < 0.5) { if (i>=h) { addLayer(); } while (p.up == null) { p = p.left; } p = p.up; SkipListEntry e = new SkipListEntry(k); e.left = p; e.right = p.right; e.down = q; p.right.left = e; p.right = e; q.up = e; q = e; i = i+1; } n = n+1; } public void remove(Integer k) { SkipListEntry p = findEntry(k); if (p.value != k) { return; } while (p!=null) { p.left.right = p.right; p.right.left = p.left; p = p.up; } } public void list() { for (int i=1;i<=h;i++) { SkipListEntry p = head; for (int j=1;j<=i;j++) { p = p.down; } while (p.right!=null && p.right.value!=Integer.MAX_VALUE) { p = p.right; System.out.print(p.value + "->"); } System.out.println(); } } private void addLayer() { SkipListEntry p1,p2; p1 = new SkipListEntry(Integer.MIN_VALUE); p2 = new SkipListEntry(Integer.MAX_VALUE); p1.right = p2; p1.down = head; p2.left = p1; p2.down = tail; head.up = p1; tail.up = p2; head = p1; tail = p2; h ++; } / * 查找节点 * @param k 待查找的节点值 * @return 节点,如果没有匹配的值,那么应该是离待插入位置最近的节点,在层1 */ private SkipListEntry findEntry(Integer k) { SkipListEntry p ; p = head; while (true) { / * eg:k = 34 * * 10 -> 20 ->30 -> 40 * ^ * p is here */ while (p.right.value!=Integer.MAX_VALUE && p.right.value <= k) { p = p.right; } if (p.down!=null) { p = p.down; } else { break; } } return p; } / * @param args */ public static void main(String[] args) { int COUNT = 100; SkipList l = new SkipList(); Random r = new Random(); int remove = 0,v; for (int i=0;i 
  

参考:http://www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html

相比红黑树:http://stackoverflow.com/questions//skip-list-vs-binary-tree

  • Locking skip lists are insanely fast. They scale incredibly well with the number of concurrent accesses. This is what makes skip lists special, other lock based data structures tend to croak under pressure.
  • Lock-free skip lists are consistently faster than locking skip lists but only barely.
  • transactional skip lists are consistently 2-3 times slower than the locking and non-locking versions.
  • locking red-black trees croak under concurrent access. Their performance degrades linearly with each new concurrent user. Of the two known locking red-black tree implementations, one essentially has a global lock during tree rebalancing. The other uses fancy (and complicated) lock escalation but still doesn't significantly out perform the global lock version.
  • lock-free red-black trees don't exist (no longer true, see Update).
  • transactional red-black trees are comparable with transactional skip-lists. That was very surprising and very promising. Transactional memory, though slower if far easier to write. It can be as easy as quick search and replace on the non-concurrent version.
写过一段代码,大多数情况下,SkipList的性能都优于JDK 的 TreeMap实现(基于红黑树)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/176814.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午8:58
下一篇 2026年3月26日 下午8:59


相关推荐

  • 关于左值和右值的一些问题总结[通俗易懂]

    在C语言当中,我们经常会遇见一些平时感觉怎么用都不会出错的小知识点,但是再将它的难度提高一点点的时候,或者将它改变一点点,我们就不再将它用起来那么的得心应手。左值和右值正是一个这样的十足十的例子。在学习了指针知识之后,高度理解左值与右值便不再显得那么的无聊。这个解释看起来有点傻,但是不得不说:左值就是那些能够出现在赋值符号左边的东西,右值就是那些能够出现在赋值符号右边的东西。例如:a=b+25;这…

    2022年4月10日
    64
  • 文本聚类简单实现_文本聚类分析

    文本聚类简单实现_文本聚类分析引用:CoreConcepts—gensim<<自然语言处理入门>>一、简介文本聚类(textclustering,也称文档聚类或documentclustering)指的是对文档进行的聚类分析,被广泛用于文本挖掘和信息检索领域。最初文本聚类仅用于文本归档,后来人们又挖掘出了许多新用途,比如改善搜索结果、生成同义词,等等。在文本的预处理中,聚类同样可以发挥作用比如在标注语料之前,通常需要从…

    2025年6月28日
    4
  • SVG图像技术摘要

    SVG图像技术摘要

    2022年1月9日
    52
  • Linux lvm扩容

    Linux lvm扩容Linuxlvm扩容一、格式化##Fdisk/dev/sdb##Command(mforhelp):t#转换类型##Hexcode(typeLtolistcodes):L#查看可用类型:##Hexcode(typeLtolistcodes):8e#修改为8e,即LinuxLVM类型##Command(mforhelp):w#保存并退出##mkfs-text4/dev/sdb(centos6)…

    2022年6月20日
    24
  • hashmap的扩容原理_HashMap

    hashmap的扩容原理_HashMap本篇文章分别讲解JDK1.7和JDK1.8下的HashMap底层实现原理文章目录一、什么是HashMap?二、为什么要使用HashMap?三、HashMap扩容为什么总是2的次幂四、JDk1.7扩容死循环问题五、JDK1.8的新结构1.为什么非要使用红黑树呢?2.什么是红黑树?3.红黑树的特性一、什么是HashMap?HashMap数据结构为数组+链表(JDk1.7),JDK1.8中增加了红黑树,其中:链表的节点存储的是一个Entry对象,每个Entry对象存储四个属性(hash,key,v

    2026年2月11日
    5
  • jquery前端递归打印出树状结构的多层复杂map或json键值对数据

    jquery前端递归打印出树状结构的多层复杂map或json键值对数据

    2021年7月17日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号