Skip List

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

• Invented around 1990 by Bill Pugh
• Generalization of sorted linked lists – so simple to implement
• Expected search time is O(log n)
• Randomized data structure:
- use random coin flips to build the data structure

Perfect Skip Lists

• Keys in sorted order.
• O(log n) levels
• Each higher level contains 1/2 the elements of the
level below it.
• Header & sentinel nodes are in every level
• 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

When search for k:

 If k = key, done!
 If k < next key, go down a level
 If k ≥ next key, go right
In other words,
• To find an item, we scan along the shortest list until
we would “pass” the desired item.
• At that point, we drop down to a slightly more
complete list at one level lower.
• Remember: sorted sequential searching...
for(i = 0; i < n; i++)
 if(X[i] >= K) break;
if(X[i] != K) return FAIL;


Search Time:

• O(log n) levels --- because you cut the # of items in
half at each level
• Will visit at most 2 nodes per level:
If you visit more, then you could have done it on
one level higher up.
• Therefore, search time is O(log n).


Insert & Delete

• Insert & delete might need to rearrange the entire
list
• Like Perfect Binary Search Trees, Perfect Skip Lists
are too structured to support efficient updates.
• Idea:
- Relax the requirement that each level have exactly half
the items of the previous level
- Instead: design structure so that we expect 1/2 the items
to be carried up to the next level
- Skip Lists are a randomized data structure: the same
sequence of inserts / deletes may produce different
structures depending on the outcome of random coin
flips.


Randomization

• Allows for some imbalance (like the +1 -1 in AVL trees)
• Expected behavior (over the random choices) remains
the same as with perfect skip lists.
• Idea: Each node is promoted to the next higher level
with probability 1/2
- Expect 1/2 the nodes at level 1
- Expect 1/4 the nodes at level 2
- ...
• Therefore, expect # of nodes at each level is the same as
with perfect skip lists.
• Also: expect the promoted nodes will be well
distributed across the list

Find k
Insert node in level 0
let i = 1
while FLIP() == “heads”:
insert node into level i
i++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值