用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脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前
By DDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前
By DDD
Inzoi:如何申請學校和大學
4 週前
By DDD
如何修復KB5055518無法在Windows 10中安裝?
2 週前
By DDD
Roblox:Dead Rails - 如何召喚和擊敗Nikola Tesla
4 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...
