-
Notifications
You must be signed in to change notification settings - Fork 0
/
skip_list.py
214 lines (186 loc) · 6.78 KB
/
skip_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
A not so neat implementation of skip list data structure.
Skip list can do search, insert and delete in O(lg n) time,
in O(n) space, with high probability
"""
from __future__ import annotations
from random import randrange
from typing import List, Optional, Tuple
class SkipListNode:
def __init__(self, value: int = -1):
self.value: int = value
self.next: Optional[SkipListNode] = None
self.pre: Optional[SkipListNode] = None
self.down: Optional[SkipListNode] = None
self.up: Optional[SkipListNode] = None
def search(self, x: int, path: List[SkipListNode]) \
-> Tuple[Optional[SkipListNode], List[SkipListNode]]:
"""
search and return the node with value x
(node, path)
"""
if self.value == x:
# item found, go to level 0 to find the position
if self.down is None:
# item found
return self, path
else:
# go down
path.append(self)
return self.down.search(x, path)
if self.down is None:
# level 0, linear search
if self.next is None or self.next.value > x:
# hit list end or larger value at level 0, return None
return None, path
else:
return self.next.search(x, path)
if self.next is None or self.next.value > x:
# hit list end or larger value, go down level
path.append(self)
return self.down.search(x, path)
else:
return self.next.search(x, path)
def search_node_right_before(self, x: int, path: List[SkipListNode]) \
-> Tuple[SkipListNode, List[SkipListNode]]:
"""
search the node right before the value x should be inserted
"""
if self.value == x:
return self.search(x, path)
if self.down is None:
# level 0, linear search
if self.next is None or self.next.value > x:
return self, path
else:
return self.next.search_node_right_before(x, path)
if self.next is None or self.next.value > x:
path.append(self)
return self.down.search_node_right_before(x, path)
return self.next.search_node_right_before(x, path)
def insert_after(self, x: int) -> SkipListNode:
"""
insert a node as successor of current node
"""
new_node = SkipListNode(value=x)
new_node.next = self.next
new_node.pre = self
self.next = new_node
if new_node.next is not None:
new_node.next.pre = new_node
return new_node
def insert_above(self) -> SkipListNode:
new_node = SkipListNode(value=self.value)
new_node.down = self
self.up = new_node
return new_node
def delete(self) -> None:
if self.up is not None:
self.up.delete()
self.pre.next = self.next
if self.next is not None:
self.next.pre = self.pre
class SkipList:
def __init__(self) -> None:
self.node_list: List[SkipListNode] = list()
def is_empty(self) -> bool:
return len(self.node_list) == 0
def top_list_node(self) -> SkipListNode:
return self.node_list[len(self.node_list) - 1]
def search(self, x: int) -> bool:
n, _ = self.top_list_node().search(x, [])
return n is not None
def insert(self, x: int) -> None:
print(f"inserting {x}")
if self.is_empty():
# if the list is empty
self.node_list.append(SkipListNode(value=x))
return
if self.node_list[0].value > x:
# insert the first element
new_node = SkipListNode(x)
new_node.next = self.node_list[0]
self.node_list[0].pre = new_node
self.node_list[0].up = None
self.node_list[0] = new_node
if len(self.node_list) > 1:
self.node_list[0].up = self.node_list[1]
self.node_list[1].down = self.node_list[0]
for node in self.node_list:
node.value = x
return
node, path = self.top_list_node().search_node_right_before(x, [])
last_node = node.insert_after(x)
for path_node in path:
if randrange(2) == 0:
break
new_node = path_node.insert_after(x)
new_node.down = last_node
last_node.up = new_node
last_node = new_node
else:
while True:
if randrange(2) == 0:
break
start_node = self.top_list_node().insert_above()
new_node = last_node.insert_above()
self.node_list.append(start_node)
start_node.next = new_node
new_node.pre = start_node
last_node = new_node
def delete(self, x: int) -> bool:
print(f'deleting {x}')
if self.node_list[0].value == x:
# delete first element
self.node_list[0] = self.node_list[0].next
old_up = self.node_list[0].up
if len(self.node_list) > 1:
self.node_list[0].up = self.node_list[1]
self.node_list[1].down = self.node_list[0]
for node in self.node_list:
node.value = self.node_list[0].value
if old_up is not None:
old_up.delete()
return True
node, path = self.top_list_node().search(x, [])
if node is None:
return False
node.delete()
return True
def show(self):
first_row: List[SkipListNode] = []
process_node = self.node_list[0]
while process_node is not None:
first_row.append(process_node)
process_node = process_node.next
node_matrix: List[List[Optional[SkipListNode]]] = [[None for _ in first_row] for _ in
self.node_list]
node_matrix[0] = first_row
for i, node in enumerate(first_row):
node = node.up
for j in range(1, len(self.node_list)):
if node is None:
break
node_matrix[j][i] = node
node = node.up
value_matrix = '\n'.join(
["\t".join([str(n.value) if n is not None else "" for n in row]) for row in
node_matrix]) + '\n'
return value_matrix
if __name__ == '__main__':
ll = SkipList()
ll.insert(1)
ll.insert(5)
ll.insert(10)
ll.insert(20)
ll.insert(15)
ll.insert(25)
print(ll.show())
ll.delete(15)
print(ll.show())
ll.delete(1)
print(ll.show())
ll.insert(1)
print(ll.show())
ll.insert(30)
print(ll.show())