-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplay.py
289 lines (234 loc) · 6.72 KB
/
splay.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
280
281
282
283
284
285
286
287
288
289
class node:
def __init__(self) -> None:
self.value = None
self.left = None
self.right = None
self.parent = None
class splay:
def __init__(self) -> None:
self.root = None
def right_rotate(t: splay, y: node) -> None:
x = y.left
y.left = x.right
if x.right:
x.right.parent = y
x.parent = y.parent
if y.parent:
if y == y.parent.right:
y.parent.right = x
else:
y.parent.left = x
else:
t.root = x
x.right = y
y.parent = x
def left_rotate(t: splay, x: node) -> None:
y = x.right
x.right = y.left
if y.left:
y.left.parent = x
y.parent = x.parent
if x.parent:
if x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
else:
t.root = y
y.left = x
x.parent = y
def splay_node(t: splay, x: node) -> None:
while x.parent:
if x == x.parent.left:
if not x.parent.parent:
# zig 的情况
right_rotate(t, x.parent)
elif x.parent == x.parent.parent.left:
# zig-zig 的情况
right_rotate(t, x.parent.parent)
right_rotate(t, x.parent)
else:
# zig-zag 的情况
right_rotate(t, x.parent)
left_rotate(t, x.parent)
else:
if not x.parent.parent:
# zig 的情况
left_rotate(t, x.parent)
elif x.parent == x.parent.parent.right:
# zig-zig 的情况
left_rotate(t, x.parent.parent)
left_rotate(t, x.parent)
else:
# zig-zag 的情况
left_rotate(t, x.parent)
right_rotate(t, x.parent)
def max_node(x: node) -> node:
current = x
while current.right:
current = current.right
return current
def min_node(x: node) -> node:
current = x
while current.left:
current = current.left
return current
def link_right(right: splay, x: node) -> None:
x.left = None
if not right.root:
right.root = x
return
minimum = min_node(right)
x.parent = minimum
minimum.left = x
def link_left(left: splay, x: node) -> None:
x.right = None
if not left.root:
left.root = x
return
maximum = max_node(left)
x.parent = maximum
maximum.right = x
def assemble(left: splay, middle: splay, right: splay) -> None:
a = middle.root.left
b = middle.root.right
if left.root:
middle.root.left = left.root
left.root.parent = middle.root
left_right = max_node(left.root)
left_right.right = a
if a:
a.parent = left_right
if right.root:
middle.root.right = right.root
right.root.parent = middle.root
right_left = min_node(right.root)
right_left.left = b
if b:
b.parent = right_left
def top_down_splay(t: splay, i: int) -> None:
left = splay()
right = splay()
while t.root.value != i:
if t.root.value > i:
if t.root.left.value == i:
temp = t.root.left
temp.parent = None
link_right(right, t.root)
t.root = temp
elif t.root.left.value > i:
temp = t.root.left.left
temp.parent = None
right_rotate(t, t.root)
link_right(right, t.root)
t.root = temp
else:
temp = t.root.left.right
temp.parent = None
link_right(right, t.root)
link_left(left, t.root)
t.root = temp
elif t.root.value < i:
if t.root.right.value == i:
temp = t.root.right
temp.parent = None
link_left(left, t.root)
t.root = temp
elif t.root.right.value > i:
temp = t.root.right.right
temp.parent = None
left_rotate(t, t.root)
link_left(left, t.root)
t.root = temp
else:
temp = t.root.right.left
temp.parent = None
link_left(left, t.root)
link_right(right, t.root)
t.root = temp
assemble(left, t, right)
def insert(t: splay, v: int) -> None:
if not t.root:
t.root = node()
t.root.value = v
return
current = t.root
while current:
trailing = current
if current.value > v:
current = current.left
else:
current = current.right
new_node = node()
new_node.value = v
if trailing.value > v:
trailing.left = new_node
new_node.parent = trailing
else:
trailing.right = new_node
new_node.parent = trailing
splay_node(t, new_node)
def search(t: splay, v: int) -> node:
current = t.root
while current:
if current.value > v:
current = current.left
elif current.value < v:
current = current.right
else:
break
else:
return None
splay_node(t, current)
return current
def min_node(x: node) -> node:
current = x
while current.left:
current = current.left
return current
def delete(t: splay, x: node) -> None:
def transplant(t: splay, u: node, v: node) -> None:
if u.parent == None:
t.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
if v:
v.parent = u.parent
if not x.left:
y = x.right
transplant(t, x, x.right)
elif not x.right:
y = x.left
transplant(t, x, x.left)
else:
y = min_node(x.right)
if y.parent != x:
transplant(t, y, y.right)
y.right = x.right
y.right.parent = y
transplant(t, x, y)
y.left = x.left
y.left.parent = y
splay_node(t, y)
def inorder_walk(n: node, depth: int = 0) -> None:
if n:
inorder_walk(n.left, depth + 1)
print('%d, %d' % (n.value, depth))
inorder_walk(n.right, depth + 1)
if __name__ == '__main__':
nums = [5, 3, 1, 2, 4, 8, 9, 10, 7, 6]
t = splay()
for n in nums:
insert(t, n)
print('Resulting tree of normal splaying:')
inorder_walk(t.root)
top_down_splay(t, 5)
print('Resulting tree of top-down splaying:')
inorder_walk(t.root)
n9 = search(t, 9)
delete(t, n9)
print()
print('Resulting tree after 9 is deleted:')
inorder_walk(t.root)