写Java的Skiplist
来源:dev.to
时间:2025-01-26 10:09:34 117浏览 收藏
一分耕耘,一分收获!既然打开了这篇文章《写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;
}
}
}
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于文章的相关知识,也可关注golang学习网公众号。
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
483 收藏
-
173 收藏
-
174 收藏
-
414 收藏
-
379 收藏
-
208 收藏
-
141 收藏
-
382 收藏
-
420 收藏
-
159 收藏
-
409 收藏
-
352 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习