【发布时间】:2011-08-21 01:08:40
【问题描述】:
如何在 O(n) 时间复杂度(最坏情况)中将 2 个给定的 Skip lists(每个都有 n 个键)合并为一个 Skip List?
只是寻找算法 - 没有特定的实现/语言。
【问题讨论】:
标签: algorithm data-structures merge skip-lists
如何在 O(n) 时间复杂度(最坏情况)中将 2 个给定的 Skip lists(每个都有 n 个键)合并为一个 Skip List?
只是寻找算法 - 没有特定的实现/语言。
【问题讨论】:
标签: algorithm data-structures merge skip-lists
store the two skip lists in two arrays: A,B
merge the arrays.
repeat until getting into root ('top' is a linked list containing only one element):
for each second entry in the skip list 'top' add a 'tag' (link 'upstairs' of the previous level)
确实是O(n),因为store和merge都是O(n),在循环中,你需要迭代:
n+n/2+n/4+n/8+... = sigma(n/2^k) where k in (0,infinity) = 2n
【讨论】: