写Java的Skiplist
来源:dev.to
2025-01-26 10:09:34
0浏览
收藏
一分耕耘,一分收获!既然打开了这篇文章《写Java的Skiplist》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!
import java.util.ArrayList;
public class SkipList {
// Node of the SkipList
public static class SkipListNode, V> {
public K key;
public V value;
public ArrayList> nextNodes;
public SkipListNode(K key, V value) {
this.key = key;
this.value = value;
nextNodes = new ArrayList>();
}
}
// SkipList
public static class SkipListMap, V> {
public static final double PROBABILITY = 0.5; // Probability for level generation
public SkipListNode 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 less = mostRightLessNodeInTree(key);
// less.nextNodes.get(0) --
SkipListNode 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 newNode = new SkipListNode<>(key, value);
for (int i = 0; i < newNodeLevel; i++) {
newNode.nextNodes.add(null);
}
int level = maxLevel;
SkipListNode 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 pre = head;
while (level >= 0) {
// Find the predecessor node at the current level
pre = mostRightLessNodeInLevel(pre, key, level);
// Remove the node
SkipListNode 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 mostRightLessNodeInTree(K key) {
if (key == null) {
return null;
}
int level = maxLevel;
SkipListNode 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 mostRightLessNodeInLevel(SkipListNode cur, K key, int level) {
if (key == null) {
return null;
}
SkipListNode 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 less = mostRightLessNodeInTree(key);
SkipListNode find = less.nextNodes.get(0);
return find != null && find.key.compareTo(key) == 0;
}
}
}
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于文章的相关知识,也可关注golang学习网公众号。
版本声明
本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
多晶碳化硅和非晶碳化硅薄膜沉积详述
- 上一篇
- 多晶碳化硅和非晶碳化硅薄膜沉积详述
- 下一篇
- 因存在安全隐患,特斯拉召回33.57万辆电动汽车
查看更多
最新文章
-
- 文章 · java教程 | 1小时前 |
- Java ForkJoinPool asyncMode 调整任务队列顺序
- 121浏览 收藏
-
- 文章 · java教程 | 11小时前 | 可观测性 · Java教程 · LongAdder JFR事件流 RecordingStream Java延迟指标 近似分位数
- JFR 事件流如何在线聚合延迟指标
- 418浏览 收藏
-
- 文章 · java教程 | 15小时前 | Java · 并发编程 · CompletableFuture · java completablefuture 取消原因 异步编排
- Java CompletableFuture 怎样在阶段之间保留取消原因
- 415浏览 收藏
-
- 文章 · java教程 | 19小时前 | spring · Spring Boot · 事务管理 · Java教程 · REQUIRES_NEW Spring事务事件 TransactionalEventListener AFTER_COMMIT Spring事件
- Spring 事务事件监听器如何在提交后执行
- 126浏览 收藏
-
- 文章 · java教程 | 22小时前 | 配置管理 · Java · Spring Boot · 故障排查 · Java教程 · spring boot actuator @ConfigurationProperties 配置属性绑定 PropertySource
- Spring Boot 配置属性绑定失败怎样定位字段来源
- 184浏览 收藏
-
- 文章 · java教程 | 1天前 | Java ·
- Java Vector API 怎样批量计算浮点数组
- 319浏览 收藏
-
- 文章 · java教程 | 1天前 | Java · java Class-File API LineNumberTable
- Java Class-File API 如何重写方法的行号表属性
- 418浏览 收藏
-
- 文章 · java教程 | 1天前 | Java · 性能优化 · Java教程 · arena 内存映射 超大文件 FileChannel.map Java MemorySegment
- Java 内存段怎样安全映射超大文件
- 435浏览 收藏
-
- 文章 · java教程 | 1天前 | Java · record pattern Java记录模式 嵌套record Java模式匹配 Java switch模式
- Java 记录模式怎样拆解嵌套数据对象
- 308浏览 收藏
-
- 文章 · java教程 | 1天前 |
- Java 虚拟线程批量发起网络请求时如何限制并发度
- 265浏览 收藏
-
- 文章 · java教程 | 1天前 | Java · 并发编程 · 虚拟线程 · java 结构化并发 StructuredTaskScope 子任务取消
- Java 结构化并发怎样统一取消一组子任务
- 370浏览 收藏
-
- 文章 · java教程 | 1天前 | 并发 · Java · java threadlocal ScopedValue 并发上下文
- Java ScopedValue 如何替代只读 ThreadLocal 上下文
- 457浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 402次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 478次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 487次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 433次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 260次使用
查看更多
相关文章
-
- Java try-with-resources 多个资源关闭顺序是什么
- 2026-09-10 501浏览
-
- 矩阵主副对角线快速定位技巧
- 2026-05-31 501浏览
-
- Java多态优化流程代码与行为分发改进
- 2026-05-26 501浏览
-
- JVM 类元数据双亲委派链表深度解析
- 2026-05-21 501浏览
-
- 反射异常处理:InvocationTargetException解析与应用
- 2026-05-16 501浏览

