Skip Lists

~3 mins read

Searching sorted data is a common use case

For static data, you could simply use binary search over an array

For dynamic data, one way is to balance a tree as you insert new elements.

A simpler alternative is a skip list. It has multiple linked lists and it balances probabilistically.

A skip list uses log-linear memory while a tree uses linear. Both have logarithmic search and insert time. But the former is simpler to implement, better for concurrent operations due to this simplicity, better for caching due to its linear structure

Example

Data:

Consider inserting the elements 10, 20, 30, 40, and 50 into a skip list.

Initial Skip List (empty):

1
2
3
4
Level 3: None 
Level 2: None 
Level 1: None 
Level 0: None

Inserting 10:

1
2
3
4
Level 3: None 
Level 2: None 
Level 1: [10] 
Level 0: [10]

Inserting 20:

1
2
3
4
Level 3: None 
Level 2: None 
Level 1: [10] 
Level 0: [10] -> [20]

Inserting 30:

1
2
3
4
Level 3: None 
Level 2: [30] 
Level 1: [10] -> [30] 
Level 0: [10] -> [20] -> [30]

Inserting 40:

1
2
3
4
Level 3: None 
Level 2: [30] 
Level 1: [10] -> [30] 
Level 0: [10] -> [20] -> [30] -> [40]

Inserting 50:

1
2
3
4
5
Level 3: None 
Level 2: [30] 
Level 1: [10] -> [30] -> [50] 
Level 0: [10] -> [20] -> [30] -> [40] -> [50]

Visual Representation

Skip List After All Insertions:

1
2
3
4
5
Level 3: None 
Level 2: [30] 
Level 1: [10] -> [30] -> [50] 
Level 0: [10] -> [20] -> [30] -> [40] -> [50]

Search Example

🎰