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;
}
}
}
0
0
相关文章
基于Perlin噪声的AI智能漫游与归巢机制设计
如何用Java写一个简单的新闻发布系统
️「Java+AI」Stable Diffusion插件开发:3倍速图像生成优化技巧
Java调用PyTorch模型完整指南:打破语言壁垒的AI应用开发
2025Java开发者技能图谱:热门技术栈学习路径
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。
16
2026.03.11
本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。
23
2026.03.10
本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。
75
2026.03.09
本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。
95
2026.03.06
本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。
218
2026.03.05
本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。
420
2026.03.04
2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!
168
2026.03.04
本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。
222
2026.03.03
本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。
33
2026.03.03
热门下载
相关下载
精品课程
最新文章
