1) An empty SkipList
2) Finding an element with key x
Observe that we return x, if exists, or succ(x) if x is not in the SkipList
3) Inserting new element X
Determine k the number of levels in which x participates (explained later)
Do find(x), and insert x to the appropriate places in the lowest k levels. (after the elements at which the search path turns down or terminates)
Example – inserting 119. k=2
If k is larger than the current number of levels, add new levels (and update top)
Example – inser(119) when k=4
k – the number of levels at which an element x participate.
Use a random function OurRnd() — returns 1 or 0 (True/False) with equal probability.
While( OurRnd() ) k++ ;
Deleteing a key x
Find x in all the levels it participates, and delete it using the standard ‘delete from a linked list’ method.
If one or more of the upper levels are empty, remove them (and update top)
Facts about SkipList
The expected number of levels is O( log n )
(here n is the numer of elements)
The expected time for insert/delete/find is O( log n )
The expected size (number of cells) is O(n )1.7.2 redis SkipList 实现
/* ZSETs use a specialized version of Skiplists */