用java写一个skiplist
import java.util.ArrayList; public class SkipList { // Node of the SkipList public static class SkipListNode<K extends Comparable<K>, V> { public K key; public V value; public ArrayList<SkipListNode<K, V>> nextNodes; public SkipListNode(K key, V value) { this.key = key; this.value = value; nextNodes = new ArrayList<SkipListNode<K, V>>(); } } // SkipList public static class SkipListMap<K extends Comparable<K>, V> { public static final double PROBABILITY = 0.5; // Probability for level generation public SkipListNode<K, V> head; public int maxLevel; // Maximum level in the SkipList public int size; public SkipListMap() { // The head is the leftmost "platform" this.head = new SkipListNode<>(null, null); head.nextNodes.add(null); // Level 0 this.size = 0; this.maxLevel = 0; } // Add a node to the SkipList public void put(K key, V value) { if (key == null) { return; } // Check if the key already exists in the SkipList, update the value if so // Method: Find the rightmost node less than the key at the bottom level SkipListNode<K, V> less = mostRightLessNodeInTree(key); // less.nextNodes.get(0) -- SkipListNode<K, V> find = less.nextNodes.get(0); if (find.key.compareTo(key) == 0) { find.value = value; } else { // Generate a random level for the new node int newNodeLevel = 0; while (Math.random() < PROBABILITY) { newNodeLevel++; } // If the new level exceeds the current max level, adjust the head levels and max level while (newNodeLevel > maxLevel) { maxLevel++; head.nextNodes.add(null); } SkipListNode<K, V> newNode = new SkipListNode<>(key, value); for (int i = 0; i < newNodeLevel; i++) { newNode.nextNodes.add(null); } int level = maxLevel; SkipListNode<K, V> pre = head; while (level >= 0) { // Find the predecessor node at the current level pre = mostRightLessNodeInLevel(pre, key, level); // Insert the new node between the predecessor and its successor if (level <= newNodeLevel) { newNode.nextNodes.set(level, pre.nextNodes.get(level)); pre.nextNodes.set(level, newNode); } level--; } size++; } } // Remove a node from the SkipList public void remove(K key) { // Check if the key exists in the SkipList if (!containsKey(key)) { return; } size--; // Start from the top level, remove the node at each level until the bottom int level = maxLevel; SkipListNode<K, V> pre = head; while (level >= 0) { // Find the predecessor node at the current level pre = mostRightLessNodeInLevel(pre, key, level); // Remove the node SkipListNode<K, V> next = pre.nextNodes.get(level); if (next != null && next.key.compareTo(key) == 0) { pre.nextNodes.set(level, next.nextNodes.get(level)); } // If a level has only the head node left, remove this level if (level != 0 && pre == head && pre.nextNodes.get(level) == null) { head.nextNodes.remove(level); maxLevel--; } level--; } } // Start from the top level and traverse down to find the rightmost node less than the key at level 0 public SkipListNode<K, V> mostRightLessNodeInTree(K key) { if (key == null) { return null; } int level = maxLevel; SkipListNode<K, V> cur = head; while (level >= 0) { cur = mostRightLessNodeInLevel(cur, key, level); level--; } return cur; } // At a specific level, find the rightmost node less than the key public SkipListNode<K, V> mostRightLessNodeInLevel(SkipListNode<K, V> cur, K key, int level) { if (key == null) { return null; } SkipListNode<K, V> pre = null; cur = cur.nextNodes.get(level); while (cur.key.compareTo(key) < 0) { pre = cur; cur = cur.nextNodes.get(level); } return pre; } // Check if the SkipList contains the given key public Boolean containsKey(K key) { if (key == null) { return false; } SkipListNode<K, V> less = mostRightLessNodeInTree(key); SkipListNode<K, V> find = less.nextNodes.get(0); return find != null && find.key.compareTo(key) == 0; } } }
登录后复制
以上是用java写一个skiplist的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前
By DDD
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前
By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。
