-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb-splay-tree.py
279 lines (244 loc) · 8.25 KB
/
b-splay-tree.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright Sergey Lagov 2022 [email protected]
import re
class Node:
def __init__(self, k=None, v=None, l=None, r=None, p=None):
self.key = k
self.val = v
self.left = l
self.right = r
self.parent = p
def search(self, k: int) -> (bool, 'Node'):
cur = self
while True:
if k == cur.key:
return True, cur
if k < cur.key:
if cur.left is None:
return False, cur
cur = cur.left
continue
if cur.right is None:
return False, cur
cur = cur.right
def add(self, k: int, v: str) -> (bool, 'Node'):
cur = self
while True:
if k == cur.key:
return False, cur
if k < cur.key:
if cur.left is None:
cur.left = Node(k=k, v=v, p=cur)
return True, cur.left
cur = cur.left
continue
if cur.right is None:
cur.right = Node(k=k, v=v, p=cur)
return True, cur.right
cur = cur.right
def is_left_child(self) -> bool:
if self.parent is None:
return False
return self is self.parent.left
class SplayTree:
def __init__(self, r=None):
self.root = r
def __search(self, k: int) -> (bool, Node):
if self.root is None:
return False, None
return self.root.search(k)
def __add(self, k: int, v: str) -> (bool, Node):
if self.root is None:
self.root = Node(k=k, v=v)
return True, self.root
return self.root.add(k, v)
def __extreme(self, max=True) -> (bool, Node):
if self.root is None:
return False, None
cur = self.root
while (cur.right if max else cur.left) is not None:
cur = cur.right if max else cur.left
return True, cur
def __min(self) -> (bool, Node):
return self.__extreme(max=False)
def __max(self) -> (bool, Node):
return self.__extreme()
def __rotate(self, x: Node, right: bool) -> Node:
m = x.right if right else x.left
p = x.parent
if p.parent is None:
self.root = x
else:
if p.is_left_child():
p.parent.left = x
else:
p.parent.right = x
x.parent = p.parent
if right:
x.right = p
p.left = m
else:
x.left = p
p.right = m
p.parent = x
if m is not None:
m.parent = p
return x
@staticmethod
def merge(lhs: 'SplayTree', rhs: 'SplayTree') -> Node:
if lhs.root is None:
if rhs.root is None:
return None
rhs.root.parent = None
return rhs.root
if rhs.root is None:
lhs.root.parent = None
return lhs.root
lhs.root.parent, rhs.root.parent = None, None
node = lhs.__max()[1]
lhs.__splay(node)
node.right = rhs.root
rhs.root.parent = node
return node
def add(self, k: int, v: str):
status, node = self.__add(k, v)
self.__splay(node)
if not status:
raise Exception('adding present element')
def set(self, k: int, v: str):
status, node = self.__search(k)
self.__splay(node)
if not status:
raise Exception('setting absent element')
node.val = v
def delete(self, k: int):
status, node = self.__search(k)
self.__splay(node)
if not status:
raise Exception('deleting absent element')
self.root = SplayTree.merge(SplayTree(r=node.left),
SplayTree(r=node.right))
def search(self, k: int) -> (bool, Node):
status, node = self.__search(k)
self.__splay(node)
if not status:
return False, None
return True, node
def min(self) -> Node:
status, node = self.__min()
self.__splay(node)
if not status:
raise Exception('empty, no minimum element')
return node
def max(self) -> Node:
status, node = self.__max()
self.__splay(node)
if not status:
raise Exception('empty, no maximum element')
return node
def __is_zig(self, x: Node) -> bool:
if x.parent is self.root:
return True
return False
def __is_zig_zig(self, x: Node) -> bool:
if x.is_left_child() and x.parent is not None and x.parent.is_left_child():
return True
if not x.is_left_child() and x.parent is not None and not x.parent.is_left_child():
return True
return False
def __is_zig_zag(self, x: Node) -> bool:
if x.is_left_child() and x.parent is not None and not x.parent.is_left_child():
return True
if not x.is_left_child() and x.parent is not None and x.parent.is_left_child():
return True
return False
def __zig(self, x) -> Node:
return self.__rotate(x, True if x.is_left_child() else False)
def __zig_zig(self, x: Node) -> Node:
if x.is_left_child():
self.__rotate(x.parent, True)
return self.__rotate(x, True)
self.__rotate(x.parent, False)
return self.__rotate(x, False)
def __zig_zag(self, x: Node) -> Node:
if x.is_left_child():
self.__rotate(x, True)
return self.__rotate(x, False)
self.__rotate(x, False)
return self.__rotate(x, True)
def __splay(self, x: Node):
while x is not None and x is not self.root:
if self.__is_zig(x):
self.__zig(x)
elif self.__is_zig_zig(x):
self.__zig_zig(x)
elif self.__is_zig_zag(x):
self.__zig_zag(x)
def print_tree(tree: SplayTree):
cur_len, cur_layer, next_layer, stop = 1, {0: tree.root}, dict(), False
while not stop:
stop = True
for i in range(cur_len):
node = cur_layer.pop(i) if i in cur_layer else None
if node is None:
print('_' if i == 0 else ' _', end='')
continue
if node.left is not None:
stop = False
next_layer[i * 2] = node.left
if node.right is not None:
stop = False
next_layer[i * 2 + 1] = node.right
if node.parent is None:
print('[{} {}]'.format(node.key, node.val), end='')
continue
if i == 0:
print('[{} {} {}]'.format(node.key, node.val, node.parent.key), end='')
continue
print(' [{} {} {}]'.format(node.key, node.val, node.parent.key), end='')
cur_layer, next_layer = next_layer, dict()
cur_len *= 2
print()
def main():
st = SplayTree()
while True:
try:
line = input()
except EOFError:
break
if not line:
continue
try:
if line == 'print':
print_tree(st)
continue
if line == 'min':
n = st.min()
print(f'{n.key} {n.val}')
continue
if line == 'max':
n = st.max()
print(f'{n.key} {n.val}')
continue
if re.fullmatch(r'delete (0|(-?[1-9]\d*))', line):
_, k = re.split(' ', line)
st.delete(int(k))
continue
if re.fullmatch(r'search (0|(-?[1-9]\d*))', line):
_, k = re.split(' ', line)
status, n = st.search(int(k))
print(f'1 {n.val}' if status else '0')
continue
if re.fullmatch(r'add (0|(-?[1-9]\d*)) .*', line):
_, k, v = re.split(' ', line)
st.add(int(k), v)
continue
if re.fullmatch(r'set (0|(-?[1-9]\d*)) .*', line):
_, k, v = re.split(' ', line)
st.set(int(k), v)
continue
except Exception:
print('error')
continue
print('error')
if __name__ == '__main__':
main()