-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplay_tree.py
288 lines (259 loc) · 8.81 KB
/
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
280
281
282
283
284
285
286
287
288
import random
from node import Node
from pairing_heap_interface import PairingHeapInterface
import numpy as np
import sys
sys.setrecursionlimit(100000)
class SplayTree(PairingHeapInterface):
updates = 0
links = 0
comps = 0
def __init__(self):
self.root = None
# rotate left at node x
def left_rotate(self, x):
# print("rotate left")
y = x.rightChild
x.rightChild = y.leftChild
self.updates += 1
if y.leftChild is not None:
y.leftChild.parent = x
self.updates += 1
y.parent = x.parent
self.updates += 1
if x.parent is None:
self.root = y
elif x == x.parent.leftChild:
x.parent.leftChild = y
else:
x.parent.rightChild = y
self.updates += 1
y.leftChild = x
self.updates += 1
x.parent = y
self.updates += 1
x.min = x.key
if x.leftChild is not None:
x.min = min(x.min, x.leftChild.min)
if x.rightChild is not None:
x.min = min(x.min, x.rightChild.min)
self.updates += 1
y.min = y.key
if y.leftChild is not None:
y.min = min(y.min, y.leftChild.min)
if y.rightChild is not None:
y.min = min(y.min, y.rightChild.min)
self.updates += 1
self.comps += 4
# rotate left at node y
def right_rotate(self, x):
# print("rotate right")
y = x.leftChild
x.leftChild = y.rightChild
self.updates += 1
if y.rightChild is not None:
y.rightChild.parent = x
self.updates += 1
y.parent = x.parent
self.updates += 1
if x.parent is None:
self.root = y
elif x == x.parent.rightChild:
x.parent.rightChild = y
else:
x.parent.leftChild = y
self.updates += 1
y.rightChild = x
self.updates += 1
x.parent = y
self.updates += 1
x.min = x.key
if x.leftChild is not None:
x.min = min(x.min, x.leftChild.min)
if x.rightChild is not None:
x.min = min(x.min, x.rightChild.min)
y.min = y.key
if y.leftChild is not None:
y.min = min(y.min, y.leftChild.min)
if y.rightChild is not None:
y.min = min(y.min, y.rightChild.min)
self.comps += 1
def splay(self, x):
while x.parent != None:
if x.parent.parent == None:
if x == x.parent.leftChild:
# zig rotation
self.right_rotate(x.parent)
else:
# zag rotation
self.left_rotate(x.parent)
elif x == x.parent.leftChild and x.parent == x.parent.parent.leftChild:
# zig-zig rotation
self.right_rotate(x.parent.parent)
self.right_rotate(x.parent)
elif x == x.parent.rightChild and x.parent == x.parent.parent.rightChild:
# zag-zag rotation
self.left_rotate(x.parent.parent)
self.left_rotate(x.parent)
elif x == x.parent.rightChild and x.parent == x.parent.parent.leftChild:
# zig-zag rotation
self.left_rotate(x.parent)
self.right_rotate(x.parent)
elif x == x.parent.leftChild and x.parent == x.parent.parent.rightChild:
# zag-zig rotation
self.right_rotate(x.parent)
self.left_rotate(x.parent)
else:
print("broke")
return None
def left(self, x):
if x is None:
return x
if x.leftChild is None:
return x
return self.left(x.leftChild)
def right(self, x):
if x is None:
return x
if x.rightChild is None:
return x
return self.right(x.rightChild)
# new leftmost node and splay to the root
def insert(self, x):
leftmost = self.left(self.root)
if leftmost is None:
self.root = x
self.updates += 1
return
leftmost.leftChild = x
self.updates += 1
x.parent = leftmost
x.leftChild = None
x.rightChild = None
x.min = x.key
self.updates += 3
self.links += 1
self.splay(x)
def find_min_node(self, x):
x = self.root
assert self.root is not None
while x.key != self.root.min:
if x.leftChild is None and x.rightChild is None:
print("buugggg")
elif x.leftChild is None:
x = x.rightChild
elif x.rightChild is None:
x = x.leftChild
else:
if x.leftChild.min == self.root.min:
x = x.leftChild
elif x.rightChild.min == self.root.min:
x = x.rightChild
else:
print("bug3000")
self.updates += 1
return x
def listInorderTree(self, root):
if root is None:
return []
else:
return self.listInorderTree(root.leftChild) + [root.key] + self.listInorderTree(root.nextSibling)
def find_min(self):
print(self.listInorderTree(self.root))
x = self.find_min_node(self.root)
if x.key != self.root.min:
print("nope")
print(x.key)
return x
def delete(self, x):
if x.leftChild is not None and x.rightChild is not None:
# rightmost in left subtree
predecessor = self.right(x.leftChild)
x.key, predecessor.key = predecessor.key, x.key
x = predecessor
self.updates += 1
if x.parent is not None:
parent = x.parent
else:
parent = None
# leaf, delete it
if x.leftChild is None and x.rightChild is None:
if x.parent is not None:
if x.parent.leftChild == x:
x.parent.leftChild = None
else:
x.parent.rightChild = None
self.updates += 1
# one child
elif x.leftChild is not None:
if x.parent is not None:
if x == x.parent.leftChild: # TODO check
x.parent.leftChild = x.leftChild
x.leftChild.parent = x.parent
else:
x.parent.rightChild = x.leftChild
x.leftChild.parent = x.parent
else:
self.root = x.leftChild
x.leftChild.parent = None
elif x.rightChild is not None:
if x.parent is not None:
if x == x.parent.leftChild:
x.parent.leftChild = x.rightChild
x.rightChild.parent = x.parent
else:
x.parent.rightChild = x.rightChild
x.rightChild.parent = x.parent
else:
self.root = x.rightChild
x.rightChild.parent = None
else:
print("NO")
self.updates += 2
if parent is None:
if self.root.leftChild is not None:
self.root.min = min(self.root.min, self.root.leftChild.min)
if self.root.rightChild is not None:
self.root.min = min(self.root.min, self.root.rightChild.min)
self.updates += 1
else:
parent.min = parent.key
if parent.leftChild is not None:
parent.min = min(parent.min, parent.leftChild.min)
if parent.rightChild is not None:
parent.min = min(parent.min, parent.rightChild.min)
self.updates += 2
if self.root is None:
print("CRY before splay")
self.splay(parent)
if self.root is None:
print("cry after splay")
if self.root is None:
print("cry")
return x
# note new min updates in rotations during splay
def delete_min(self):
return (self.delete(self.find_min()), self.comps, self.links)
# def in_order(self, node):
# if node is not None:
# if node.leftChild is not None:
# self.in_order(node.leftChild)
# print(node.key)
# if node.rightChild is not None:
# self.in_order(node.rightChild)
def pointer_updates(self):
return self.updates
# if __name__ == "__main__":
# tree = SplayTree()
# #tree.insert(Node(3))
# #tree.insert(Node(4))
# tree.insert(Node(5))
# tree.insert(Node(7))
# #tree.insert(Node(8))
# tree.insert(Node(1))
# tree.insert(Node(10))
# #tree.insert(Node(9))
# tree.in_order(tree.root)
# print(tree.find_min().key)
# tree.delete_min()
# print(tree.find_min().key)