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;
}
}
}
0
0
相关文章
基于Perlin噪声的AI智能漫游与归巢机制设计
如何用Java写一个简单的新闻发布系统
️「Java+AI」Stable Diffusion插件开发:3倍速图像生成优化技巧
Java调用PyTorch模型完整指南:打破语言壁垒的AI应用开发
2025Java开发者技能图谱:热门技术栈学习路径
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。
835
2023.06.15
java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。
741
2023.07.05
Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。
397
2023.08.01
Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。
399
2023.08.02
java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。
446
2023.08.02
java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。
430
2023.08.02
Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。
16926
2023.08.03
热门下载
相关下载
精品课程
最新文章


