Skip to content

Commit 775ae3c

Browse files
committed
Implement skip list
1 parent 134e683 commit 775ae3c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class SkipList:
2+
def __init__(self):
3+
self.data = []
4+
5+
def insert(self, value):
6+
"""Insert value in sorted order"""
7+
# Find insertion point
8+
left, right = 0, len(self.data)
9+
while left < right:
10+
mid = (left + right) // 2
11+
if self.data[mid] < value:
12+
left = mid + 1
13+
else:
14+
right = mid
15+
16+
# Insert at found position
17+
self.data.insert(left, value)
18+
19+
def contains(self, value) -> bool:
20+
"""Check if value exists"""
21+
left, right = 0, len(self.data)
22+
while left < right:
23+
mid = (left + right) // 2
24+
if self.data[mid] == value:
25+
return True
26+
elif self.data[mid] < value:
27+
left = mid + 1
28+
else:
29+
right = mid
30+
return False
31+
32+
def to_list(self) -> list:
33+
"""Convert to sorted list"""
34+
return self.data.copy()
35+
36+
def __contains__(self, value) -> bool:
37+
return self.contains(value)

0 commit comments

Comments
 (0)