Skip Lists
Skip Lists
CMSC 420
Linked Lists Benefits & Drawbacks
• Benefits:
- Easy to insert & delete in O(1) time
- Don’t need to estimate total memory needed
• Drawbacks:
- Hard to search in less than O(n) time
(binary search doesn’t work, eg.)
- Hard to jump to the middle
• Skip Lists:
- fix these drawbacks
- good data structure for a dictionary ADT
Skip Lists
31
2 31
2 15 31 96
2 10 15 16 31 71 96
sentinel
header
Perfect Skip Lists
31
2 31
31
2 15 31 96
15 96
2 10 15 16 31 71 96
Perfect Skip Lists, continued
• Nodes are of variable size:
- contain between 1 and O(log n) pointers
• Pointers point to the start of each node
(picture draws pointers horizontally for visual clarity)
• Called skip lists because higher level lists let you skip over
many items
31
2 31
31
2 15 31 96
15 96
2 10 15 16 31 71 96
Perfect Skip Lists, continued
Change
Comparison current
Find 71 location
31
31 2 91
16
15 31 2
71 96 91
2 89
2 10 15 16 31 71 86 87 91 96
Insertion:
Insert 87
31
31 2 91
16 91
15 31 2
71 96
2 89
2 10 15 16 31 71 86 87 91 96
Insertion:
Insert 87
31
31 2 87 91
16 91
15 31 2
71 87 96
2 89
2 10 15 16 31 71 86 87 87 91 96
Find k
Insert node in level 0 Just insertion into
let i = 1 a linked list after
while FLIP() == “heads”: last visited node in
insert node into level i level i
i++
Deletion:
Delete 87
31
31 2 87 91
16 91
15 31 2
71 87 96
2 89
2 10 15 16 31 71 86 87 87 91 96
Deletion:
Delete 87
31
31 2 91
16 91
15 31 2
71 96
2 89
2 10 15 16 31 71 86 87 91 96
There are no “bad” sequences:
31
31 2 91
16 91
15 31 2
71 96
2 89
2 10 15 16 31 71 86 87 91 96
0.5
• Steps to go up j levels =
Make one step, then make either
C(j-1) steps if this step went up [Prob = 0.5]
C(j) steps if this step went left [Prob = 0.5]
So:
2C(j) = 2 + C(j-1) + C(j)
C(j) = 2 + C(j-1)