Designing a Skip List
The user is asking for a design of a skip list, including its operations for O(log n) access. I need to provide an explanation of the skip list and its node structure, covering levels, probabilistic heights, and operations like search, insert, and delete, along with their complexities. Although they didn't ask explicitly for pseudocode, suggesting algorithm-level design implies I should include it. I’ll aim for clarity and conciseness while outlining the structure and design choices. Let's start! A skip list is a probabilistic linked structure that gives expected O(log n) time for search/insert/delete while being simpler than balanced trees.
Below is a standard design and its main operations.
1. Data structure design
Node structure
Each node stores:
key (and optionally value)
forward[]: an array of forward pointers, one for each level the node participates in
Node {
key
value
forward[0..level] // forward[i] points to next node at level i
}
List structure
head: a special node with maximum possible level MAX_LEVEL
level: current highest level in the list (0-based)
p: probability parameter (commonly 0.5) that controls level height
SkipList {
head: Node with level = MAX_LEVEL
level: integer in [0..MAX_LEVEL]
p: float in (0, 1) // e.g., 0.5
}
Random level generator
To keep complexity O(log n) in expectation, assign levels randomly when inserting:
randomLevel():
lvl = 0
while random() < p and lvl < MAX_LEVEL:
lvl = lvl + 1
return lvl
This gives an expected number of nodes at level i of n * p^i, so height is O(log n) w.h.p.
2. Search (find key)
Goal: find a node with given key or where it would be inserted.
Idea: Start at highest level, move right while next key is < target; if cannot move right (or next key ≥ target), move one level down; repeat until level 0.
search(S, key):
x = S.head
for i from S.level down to 0:
while x.forward[i] != null and x.forward[i].key < key:
x = x.forward[i]
x = x.forward[0]
if x != null and x.key == key:
return x
else:
return null
Expected time: O(log n)
Because expected height is O(log n), and at each level we do expected constant work.
3. Insertion
Steps:
- As in search, walk down the list but record, for each level, the last node before the insertion position in an
update[] array.
- Call
randomLevel() to get the new node’s level lvl.
- If
lvl > S.level, extend the list’s current level and initialize the new highest levels in update[] with head.
- Insert the new node by fixing forward pointers at all levels from 0 to
lvl.
insert(S, key, value):
update[0..MAX_LEVEL]
x = S.head
// 1. Search & fill update[]
for i from S.level down to 0:
while x.forward[i] != null and x.forward[i].key < key:
x = x.forward[i]
update[i] = x
x = x.forward[0]
// Optional: if key already exists, update value
if x != null and x.key == key:
x.value = value
return
// 2. Generate level for new node
lvl = randomLevel()
if lvl > S.level:
for i from S.level+1 to lvl:
update[i] = S.head
S.level = lvl
// 3. Create and link new node
newNode = Node(key, value, level = lvl)
for i from 0 to lvl:
newNode.forward[i] = update[i].forward[i]
update[i].forward[i] = newNode
Expected time: O(log n), due to search.
4. Deletion
Steps:
- Similar to insertion, search and fill
update[] with the last node before the key at each level.
- If the node with
key exists, update forward pointers to bypass it at all levels where it appears.
- Adjust
S.level if top levels become empty.
delete(S, key):
update[0..MAX_LEVEL]
x = S.head
// 1. Search & fill update[]
for i from S.level down to 0:
while x.forward[i] != null and x.forward[i].key < key:
x = x.forward[i]
update[i] = x
x = x.forward[0]
// 2. If found, unlink
if x != null and x.key == key:
for i from 0 to S.level:
if update[i].forward[i] != x:
break
update[i].forward[i] = x.forward[i]
// 3. Reduce level if highest levels are empty
while S.level > 0 and S.head.forward[S.level] == null:
S.level = S.level - 1
Expected time: O(log n), dominated by search.
5. Complexity summary
- Search: expected O(log n)
- Insert: expected O(log n)
- Delete: expected O(log n)
- Space: expected O(n) total pointers, since each node’s expected level is 1/(1−p)
Picking p = 0.5 and MAX_LEVEL ≈ log₂(n_max) yields good constant factors and O(log n) height with high probability.
If you’d like, I can also provide a concrete implementation in C++, Java, or Python based on this design.