Implementing the skip list data structure in java --reference
reference:http://www.mathcs.emory.edu/~cheung/Courses/323/Syllabus/Map/skip-list-impl.html
- The link list element structure used to implement a Skip List
- The link list element used to implement the skip list has 4 links (not including the data portion):

- The link list element used to implement the skip list has 4 links (not including the data portion):
- The Entry strcuture in a Skip List (the SkipListEntry class)
- Skip List entry:
public class SkipListEntry
{
public String key;
public Integer value; public SkipListEntry up; // up link
public SkipListEntry down; // down link
public SkipListEntry left; // left link
public SkipListEntry right; // right link ...
(methods)
}
Note:
- As you can see, my entry type is again very specific (no generic types):
- String key
- Integer value
- When I write the demo program, I will do it using specific types (classes), not parameterized classes
I have showed you how to convert a specific class into a parameterized class, so you can write one if you want to
- Reason for using specific classes:
- My choice is didactic in nature; I don't want to spend time analyzing the overly complex syntax of parameterized classes
- I want to spend my time teaching algorithms, not Java syntax
- As you can see, my entry type is again very specific (no generic types):
- Skip List entry:
- Making (and using) the special −∞ and +∞ elements
- Representing the −∞ element and the +∞ element:
- The −∞ and the +∞ is just an ordinary Skip List Entry containing a special value for the key field.
- We can accommodate the −∞ element and the +∞ element by defining 2 special key value:
public class SkipListEntry
{
public String key;
public Integer value; public SkipListEntry up, down, left, right; public static String negInf = "-oo"; // -inf key value
public static String posInf = "+oo"; // +inf key value ....
} - How to instantiate a Skip List entry containing +∞:
SkipListEntry x = new SkipListEntry( SkipListEntry.posInf, null );
How to check if an Skip List entry x contains +∞:
key == SkipListEntry.posInf
OK, now we move on to the Skip list itself....
- Representing the −∞ element and the +∞ element:
- Structure (class) to represent a Skip List
- Remember that a Skip List is a very complicated list
But.... It is never the less a list
- To represent a list, we only use a pointer (that points to the first element)
- Often, we use more pointers for improve efficiency (such as a tail pointer)
- Variables in the SkipList class:
public class SkipList
{
public SkipListEntry head; // First element of the top level
public SkipListEntry tail; // Last element of the top level public int n; // number of entries in the Skip List public int h; // Height
public Random r; // Coin toss .... }Note:
- The Random object r is used to determine the height of a newly added entry
(We use r to simulate a coin toss experiment)
- The Random object r is used to determine the height of a newly added entry
- Example illustrating how the variables are used:

Note:
- Since the logical top level does not contain any entries:
- The implementation will omit the logical top layer
- The variables head and tail provide quick access to the end elements of the real top layer
Usage of head and tail:
- They allow us to easily add an new layer above the top layer
- Since the logical top level does not contain any entries:
- Remember that a Skip List is a very complicated list
- Constructing a Skip List object
- The constructor will construct an empty Skip List which looks like this:

- Constructor code:
public SkipList() // Constructor...
{
SkipListEntry p1, p2; /* -----------------------------------
Create an -oo and an +oo object
----------------------------------- */
p1 = new SkipListEntry(SkipListEntry.negInf, null);
p2 = new SkipListEntry(SkipListEntry.posInf, null); /* --------------------------------------
Link the -oo and +oo object together
--------------------------------------- */
p1.right = p2;
p2.left = p1; /* --------------------------------------
Initialize "head" and "tail"
--------------------------------------- */
head = p1;
tail = p2; /* --------------------------------------
Other initializations
--------------------------------------- */
n = 0; // No entries in Skip List
h = 0; // Height is 0 r = new Random(); // Make random object to simulate coin toss
} - The SkipList class so far:
public class SkipList
{
public SkipListEntry head; // First element of the top level
public SkipListEntry tail; // Last element of the top level public int n; // number of entries in the Skip List public int h; // Height
public Random r; // Coin toss public SkipList() // Constructor...
{
SkipListEntry p1, p2; p1 = new SkipListEntry(SkipListEntry.negInf, null);
p2 = new SkipListEntry(SkipListEntry.posInf, null); head = p1;
tail = p2; p1.right = p2;
p2.left = p1; n = 0; h = 0;
r = new Random();
} ...
}
- The constructor will construct an empty Skip List which looks like this:
- Implementing the basic Map operations
- Basic Map operations:
- get()
- put()
- remove()
Notice that each basic operation must first find (search) the appropriate entry (using a key) before the operation can be completed.
So we must learn how to search a Skip List for a given key first....
- Basic Map operations:
- Search operation in a skip list
- Consider the links traversed to locate the key 50:

- Psuedo code:
p = head; repeat
{ Move to the right until your right neighbor node
contains a key that is greater than k if ( not lowest level )
Drop down one level
else
exit
} - Search algorithm for Skip List:
/* ------------------------------------------------------
findEntry(k): find the largest key x <= k
on the LOWEST level of the Skip List
------------------------------------------------------ */ public SkipListEntry findEntry(String k)
{
SkipListEntry p; /* -----------------
Start at "head"
----------------- */
p = head; while ( true )
{
/* ------------------------------------------------
Search RIGHT until you find a LARGER entry E.g.: k = 34 10 ---> 20 ---> 30 ---> 40
^
|
p must stop here
p.right.key = 40
------------------------------------------------ */
while ( (p.right.key) != SkipListEntry.posInf &&
(p.right.key).compareTo(k) <= 0 )
{
p = p.right; // Move to right
} /* ---------------------------------
Go down one level if you can...
--------------------------------- */
if ( p.down != null )
{
p = p.down; // Go downwards
}
else
{
break; // We reached the LOWEST level... Exit...
}
} return(p); // Note: p.key <= k
} - Note:
- If the key k is found in the Skip List, findEntry(k) will return the reference to the entry containg the key k
- If the key k is not found in the Skip List, findEntry(k) will return the reference to the floorEntry(k) entry containg a key that issmaller than k
Example: findEntry(42) will return the reference to 39:

- If the key k is found in the Skip List, findEntry(k) will return the reference to the entry containg the key k
- Consider the links traversed to locate the key 50:
- Implementing the "get(Key k)" method
- get(k):
/** Returns the value associated with a key. */ public Integer get (String k)
{
SkipListEntry p; p = findEntry(k); if ( k.equals( p.key ) )
return(p.value);
else
return(null);
}
- get(k):
- Put(k,v): inserting into a Skip List
- Pseudo code for put(k,v):
put(k, v)
{
SkipListEntry p; p = findEntry(k); if ( k.equals( p.key ) ) // Found !
{
p.value = v; // Update the value
return; // Done
} /* ==================================================
Not found. Then: p == floorEntry(k) !!!
================================================== */ (1) insert (k,v) AFTER p
(2) make a column of (k,v) of RANDOM height
}
- Recall what happens when we insert a new entry:
- Before insertion:

- After inserting key 42:

Note:
- As part of the insert operation, we will make a column (see figure above) for that key
- The height of the column will be random...
(We have also seen how to use a random "trial" to generate a random height)
- As part of the insert operation, we will make a column (see figure above) for that key
- Before insertion:
- Step-by-step depictions of the steps necessary for insertion: put("42", ??)
- Before the insertion:

- Step 1: find the insert position
p = findEntry(k)

- Step 2: insert q after p:

- Now make a column of random height: repeat these steps a random number of times
- Starting at p, (using p to) scan left and find the first entry that has an up-entry:

Make p point to the up-element:

- Create a new entry with the same key (we are making the "tower"):

- Insert the newly created entry: right of p and up from q:

- Make q point to the newly inserted entry (to continue the iteration if necessay)

- Starting at p, (using p to) scan left and find the first entry that has an up-entry:
- I will repeat the steps and show the effect of building a "tower":
- Starting at p, scan left and find the first entry that has an up-element:

- Create a new entry (we are making another level of the "tower"):

- Insert the newly created entry: right of p and up from q:

- Make q point to the newly inserted entry (to continue the iteration if necessay)

(And so on)
- Starting at p, scan left and find the first entry that has an up-element:
- Before the insertion:
- Note:
- If the height of the "tower" is = h:

we must add an new empty layer before we can insert another entry:

- If the height of the "tower" is = h:
- Pseudo code for put(k,v):
- Adding a (empty) layer
- Before we can do anything, We need to what are the changes in the Skip List when we add an empty layer to the Skip List:
- Here is the Skip List before we add a new (empty) top layer:

- Here is the Skip List before we add a new (empty) top layer:

- Here is the Skip List before we add a new (empty) top layer:
- Add layer algorithm:
SkipListEntry p1, p2; /* -----------------------------
Make the -oo and +oo entries
---------------------------- */
p1 = new SkipListEntry(SkipListEntry.negInf, null);
p2 = new SkipListEntry(SkipListEntry.posInf, null); /* --------------------
Link them
-------------------- */
p1.right = p2;
p1.down = head; p2.left = p1;
p2.down = tail; head.up = p1;
tail.up = p2; /* --------------------
Update head and tail
-------------------- */
head = p1;
tail = p2; h = h + 1; // One more level...
- Before we can do anything, We need to what are the changes in the Skip List when we add an empty layer to the Skip List:
- The put() method
- put(k,v) psuedo code:
p = findEntry(k); // Find insert location if ( entry found )
{
update the value in p;
exit;
} /* ----------------------------------
Insert a brand new entry (k,v) p put q here
| |
V V
[ ] <------> [ ]
---------------------------------- */ q = new Entry(k,v); // Make new entry
link q after p; /* ------------------------
Make a random tower...
------------------------ */
while ( random() < 0.5 /* coin toss */ )
{
if ( height of tower >= h )
{
create a new TOP layer (see: click here)
} p = Find the first left element in the next level above; q = new Entry(k,v);
link q after p;
}
- The put() method for Skip List in Java:
public Integer put (String k, Integer v)
{
SkipListEntry p, q;
int i; p = findEntry(k); // Try find the entry /* ------------------------
Check if key is found
------------------------ */
if ( k.equals(p.key) ) // If key found, update the value and we are done...
{
Integer old = p.value; // Remember the old value p.value = v; // Update value return(old); // Return the old value
} /* -------------------------------------------------------------
Key k is not found, then p = floorEntry(k) (See: click here) The rest of the code will insert a new entry (k,v)
------------------------------------------------------------- */ q = new SkipListEntry(k,v); // Create a new entry with k and v /* --------------------------------------------------------------
Insert q into the lowest level after SkipListEntry p: p put q here p q
| | | |
V V V V V
Lower level: [ ] <------> [ ] ==> [ ] <--> [ ] <--> [ ]
--------------------------------------------------------------- */
q.left = p;
q.right = p.right;
p.right.left = q;
p.right = q; /* -----------------------------------------------------
Make a "tower" of the entry e or RANDOM height
----------------------------------------------------- */ i = 0; // Current level = 0 while ( r.nextDouble() < 0.5 /* Coin toss */ )
{
// Coin toss success ! ---> build one more level !!! /* -------------------------------------------------------------------
Check if we need to increase the height of the -oo and +oo "pillars
------------------------------------------------------------------- */
if ( i >= h ) // We reached the top level !!!
{
Create a new empty TOP layer (see: click here)
(Put the code from above here.... I left it out for brevity)
} /* ------------------------------------
Find first element with an UP-link
------------------------------------ */
while ( p.up == null )
{
p = p.left;
} /* --------------------------------
Make p point to this UP element
-------------------------------- */
p = p.up; /* ---------------------------------------------------
Add one more (k,*) to the column Schema for making the linkage: p <--> e(k,*) <--> p.right
^
|
v
q
---------------------------------------------------- */
SkipListEntry e; e = new SkipListEntry(k, null); // Don't need the value... /* ---------------------------------------
Initialize links of e
--------------------------------------- */
e.left = p;
e.right = p.right;
e.down = q; /* ---------------------------------------
Change the neighboring links..
--------------------------------------- */
p.right.left = e;
p.right = e;
q.up = e; q = e; // Set q up for next iteration (if there is one)
// See here for more detail: click here i = i + 1; // Current level increases by one
} n = n + 1; // One more entry in the Skip List return(null); // No old value
}
- Example Program: (Demo above code)
- SkipListEntry.java Prog file: click here
- SkipList.java Prog file: click here
- Test program 1 (inserts 4 entries): click here
- Test program 2 (inserts 40 entries): click here
Example output: (The keys are strings)
- - - - - - - - - -
10
13
15 15
2
21
25
31 31 31
33 33 33 33 33 33 33 33 33 33
36
38
39 39 39 39 39
41 41 41
42 42 42 42
5 5 5
54 54
57
59 59 59 59 59 59 59
60 60
63 63
65
69
7
71 71 71 71 71
72
77 77
81
82
86
88
90
92 92
99
+ + + + + + + + + +
- put(k,v) psuedo code:
- Deleting an entry from a Skip List
- What you must do to the skip list to remove an entry:
- Before deletinng the entry 25:

- After deleting the entry 25:

(The whole column containing entries for 25 must be deleted !!!)
- Before deletinng the entry 25:
- Step-by-step to accomplish: remove(25)
- Before the deletion:

- Step 1: locate the desired element (at the lowest level of the skip list):

- While p != null, repeat these steps to remove the column:
- Unlink the element at p (by making the left neighbor and the right neighbor pointing to each other)

- Move p upward (prepare for loop)

- Unlink the element at p (by making the left neighbor and the right neighbor pointing to each other)
- Result of removal:

- Before the deletion:
- What you must do to the skip list to remove an entry:
- The Removal Algorithm
- Psuedo code:
p = findExntry(k); if (p.key != k)
return(null); // Not found, don't remove /* ------------------------------------------------------------
We are at level 0
Travel up the tower and link the left and right neighbors
------------------------------------------------------------ */
while ( p != null )
{
p.left.right = p.right;
p.right.left = p.left;
}
补充,jdk中有一个java.util.concurrent.ConcurrentSkipListMap,可以参考这个skiplist实现。
* @author Doug Lea
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @since 1.6
- Psuedo code:
Implementing the skip list data structure in java --reference的更多相关文章
- Data structure basics - Java Implementation
Stack & Queue Implementations FixedCapacityQueue package cn.edu.tsinghua.stat.mid_term; import j ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode Two Sum III - Data structure design
原题链接在这里:https://leetcode.com/problems/two-sum-iii-data-structure-design/ 题目: Design and implement a ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- LeetCode 笔记27 Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- javascript AES加密 C#AES解密实现
首先需要引入js类库 crypto-js(开源),地址:http://code.google.com/p/crypto-js 现在很多人无法打开这个地址不要紧,下面我们会将全部代码贴出来 需要引入 a ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- Tkinter教程之Event篇(1)'
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823544 ''Tkinter教程之Event篇(1)'''# 事件的使用方法'''1.测试鼠 ...
- Hold住:坚持的智慧
这类励志的书读完时,感觉很激励人,可读完后总觉得空空的.同样这本书读完后没特别的感觉(也许书中的思想已影响了我,只是目前还说不太清楚),只感觉有些句子很有感觉,做个汇总: 1. 荀子有言:“ ...
- MAC机常用快捷键整理表格
MAC机常用快捷键整理表格 范围 快捷键 说明 图形 (Command 键)在某些 Apple 键盘上,此键也可能为标志() Control (Control 键) Alt Opt ...
- java出现no XXX in java.library.path的解决办法及eclipse配置
java一般使用两个path:classpath 和 java.library.path classpath是指向jar包的位置 java.library.path是非java类包的位置如(dll,s ...
- 15个顶级Java多线程面试题及答案
1)现在有T1.T2.T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行? 这个线程问题通常会在第一轮或电话面试阶段被问到,目的是检测你对”join”方法是否熟悉.这个多线程问题比 ...
- homework-08 C++课程课后思考与练习
经过上次晚交作业导致没分以后 我再也不敢晚交作业了 今天就把这次作业先写了 homework Part 1 1. 理解C++变量的作用域和生命周期 a) 用少于10行代码演示你对局部变量的生命周期的理 ...
- POJ 3159 Candies (栈优化spfa)
Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...
- CodeForces 489A SwapSort (选择排序法)
SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...