-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskip_list.py
84 lines (67 loc) · 2.3 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
from random import randint
class SkipNode:
"""A node from a skip list"""
def __init__(self, height=0, elem=None):
self.elem = elem
self.next = [None]*height
class SkipList:
def __init__(self):
self.head = SkipNode()
self.len = 0
self.maxHeight = 0
self.comparisons = 0
def __len__(self):
return self.len
def find(self, elem, update=None):
if update is None:
update = self.updateList(elem)
if len(update) > 0:
candidate = update[0].next[0]
if candidate is not None and candidate.elem == elem:
return candidate
return None
def contains(self, elem, update=None):
return self.find(elem, update) is not None
def randomHeight(self):
height = 1
while randint(1, 2) != 1:
self.comparisons += 1
height += 1
return height
def updateList(self, elem):
update = [None] * self.maxHeight
x = self.head
for i in reversed(xrange(self.maxHeight)):
while x.next[i] is not None and x.next[i].elem < elem:
x = x.next[i]
self.comparisons += 1
update[i] = x
return update
def insert(self, elem):
node = SkipNode(self.randomHeight(), elem)
self.maxHeight = max(self.maxHeight, len(node.next))
while len(self.head.next) < len(node.next):
self.comparisons += 1
self.head.next.append(None)
update = self.updateList(elem)
if not self.find(elem, update):
for i in range(len(node.next)):
node.next[i] = update[i].next[i]
update[i].next[i] = node
self.len += 1
def remove(self, elem):
update = self.updateList(elem)
x = self.find(elem, update)
if x is not None:
for i in reversed(range(len(x.next))):
update[i].next[i] = x.next[i]
if self.head.next[i] is None:
self.maxHeight -= 1
self.len -= 1
def printList(self):
for i in range(len(self.head.next)-1, -1, -1):
x = self.head
while x.next[i] is not None:
print x.next[i].elem,
x = x.next[i]
print ''