You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Skip-List/README.md
+85-11Lines changed: 85 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,95 @@
1
1
# Skip List
2
2
3
-
Skip List is a probablistic data-structure with same efficiency as AVL tree or Red-Black tree. The building blocks are hierarchy of layers (regular sorted linked-lists), created probablisticly by coin flipping, each acting as an express lane to the layer underneath, therefore making fast O(log n) search possible by skipping lanes and reducing travel distance. A layer consists of a Head node, holding reference to the subsequent and the node below. Each node also holds similar references as the Head node.
3
+
Skip List is a probablistic data-structure with same logarithmic time bound and
4
+
efficiency as AVL/ or Red-Black tree and provides a clever compromise to
5
+
efficiently support search and update operations and is relatively simpler to
6
+
implement compared to other map data structures.
4
7
5
-

8
+
A skip list *S* consists of series of sorted linked lists *{L0, ..., Ln}*,
9
+
layered hierarchicaly and each layer *L* stores a subset of items in layer *L0*
10
+
in incremental order. The items in layers *{L1, ... Ln}* are chosen at random
11
+
based on a coin flipping function with probability 1/2 . For traversing, every
12
+
item in a layer hold references to the node below and the next node. This
13
+
layers serve as express lanes to the layer underneath them, effectively making
14
+
fast O(log n) searching possible by skipping lanes and reducing travel distance
15
+
and in worse case searching degrades to O (n), as expected with regular linked
16
+
list.
7
17
18
+
For a skip list *S*:
19
+
20
+
1. List *L0* contains every inserted item.
21
+
2. For lists *{L1, ..., Ln}*, *Li* contains a randomly generated subset of the
22
+
items in *Li-1*
23
+
3. Height is determined by coin-flipping.
24
+
25
+

26
+
Figure 1
27
+
28
+
29
+
#Searching
30
+
31
+
Searching for element *N* starts by traversing from top most layer *Ln* until
32
+
*L0*.
33
+
34
+
Our objective is to find an element *K* such that its value at the rightmost
35
+
position of current layer, is less-than target item and its subsequent node has
36
+
a greater-equal value or nil ( * K.key < N.key <= (K.next.key or nil)* ). if
37
+
value of *K.next* is equal to *N*, search is terminated and we return *K.next*,
38
+
otherwise drop underneath using *K.down* to the node below ( at layer Ln-1 ) and
39
+
repeat the process until *L0* where *K.down* is `nil` which indicates that level
40
+
is *L0* and item doesn't exists.
41
+
42
+
43
+
###Example:
44
+
45
+

8
46
9
47
#Inserting
10
48
11
-
1. Inserting a new element in initial state where no prior layer is created yet begins by construction of a Head node with a reference to data node (just like regular lists). After this operation coin-flipping starts to determine wether a new layer must be created. When positive, a new layer containing Head and data node ( similar to previous layer ) is created and head is updated to reference the new layer and the head node underneath. Subsequently data node is also updated to reference the node below; until coin-flip function return a negative value to terminate this process.
49
+
Inserting element *N* has a similar process as searching. It starts by
50
+
traversing from top most layer *Ln* until *L0*. We need to keep track of our
51
+
traversal path using a stack. It helps us to traverse the path upward when
52
+
coin-flipping starts, so we can insert our new element and update references to
53
+
it.
12
54
13
-
2. After initial state, we start at top most layer to find a node where its value is less than the new value and its next node value is greater-equal/nil than new value. Then we use this node to travel to layer underneath until we reach the layer 0 where the element must be inserted first, after this node. Node references must be updated accordingly by back tracing the path to the top. If the first element in the layer does not meet the criteria, same procedure is used to travel to the layer below using the head node.
55
+
Our objective is to find a element *K* such that its value at the rightmost
56
+
position of layer *Ln*, is less-than new item and its subsequent node has a
57
+
greater-equal value or nil ( * K.key < N.key < (K.next.key or nil)* ). Push
58
+
element *K* to the stack and with element *K*, go down using *K.down* to the
59
+
node below ( at layer Ln-1 ) and repeat the process ( forward searching ) up
60
+
until *L0* where *K.down* is `nil` which indicates that level is *L0*. We
61
+
terminate the process when *K.down* is nil.
14
62
63
+
At *L0*, *N* can be inserted after *K*.
15
64
16
-
**Example**
65
+
Here is the interesting part. We use coin flipping function to randomly create
66
+
layers.
17
67
68
+
When coin flip function returns 0, the whole process is finished but when
69
+
returns 1, there are two possibilities:
18
70
19
-
1 - Inserting 10 in initial state with coin flips (0)
71
+
1. Stack is empty ( Level is *L0* / -*Ln* or at uninitialized stage)
72
+
2. Stack has items ( traversing upward is possible )
20
73
21
-

74
+
In case 1:
22
75
76
+
A new layer M* is created with a head node *NM* referencing head node of layer
77
+
below and *NM.next* referencing new element *N*. New element *N* referecing
78
+
element *N* at previous layer.
23
79
24
-
2 - 12 inserted with coin flips (1,1,0)
80
+
In case 2:
25
81
82
+
repeat until stack is empty Pop an item *F* from stack and update the references
83
+
accordingly. *F.next* will be *K.next* and *K.next* will be *F*
84
+
85
+
when stack is empty Create a new layer consisintg of a head node *NM*
86
+
referencing head node of layer below and *NM.next* referencing new element
87
+
*N*. New element *N* referencing element *N* at previous layer.
88
+
26
89
27
-
3 - Inserting 13. with coin flips (0)
90
+
###Example:
91
+
92
+
Inserting 13. with coin flips (0)
28
93
29
94

30
95

@@ -33,7 +98,16 @@ Skip List is a probablistic data-structure with same efficiency as AVL tree or R
0 commit comments