-
Notifications
You must be signed in to change notification settings - Fork 66
/
os_tree.py
executable file
·161 lines (146 loc) · 4 KB
/
os_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
#!/usr/bin/env python
from rb_tree import RbNode, RbTree
class OSNode(RbNode):
def __init__(self, key, p, left, right, color, size):
RbNode.__init__(self, key, p, left, right, color)
self.size = size
def select_recursive(self, i):
r = self.left.size + 1
if i == r:
return self
elif i < r:
return self.left.select_recursive(i)
else:
return self.right.select_recursive(i - r)
def select_iterative(self, i):
x = self
while True:
r = x.left.size + 1
if i == r:
return x
elif i < r:
x = x.left
else:
x = x.right
i = i - r
def key_rank(self, k):
r = self.left.size + 1
if k == self.key:
return r
elif k < self.key:
return self.left.key_rank(k)
else:
return r + self.right.key_rank(k)
def ith_successor(self, i):
if i == 0:
return self
r = self.right.size
if i <= r:
return self.right.select_recursive(i)
else:
x = self
y = self.p
while y.size != 0 and y.right == x:
x = y
y = y.p
if y.size != 0:
return y.ith_successor(i - r - 1)
class os_tree(RbTree):
nil = OSNode(None, None, None, None, 1, 0)
root = nil
def __init__(self, values):
if isinstance(values, list):
for i in values:
self.insert(OSNode(i, None, None, None, 0, 1))
else:
print("Not invalid argument")
def insert(self, z):
y = self.nil
x = self.root
while x != self.nil:
x.size = x.size + 1
y = x
if z.key <= x.key:
x = x.left
else:
x = x.right
if y == self.nil:
self.root = z
elif z.key <= y.key:
y.left = z
else:
y.right = z
z.p = y
z.left = self.nil
z.right = self.nil
z.color = 0 # red
self.insert_fixed(z)
def delete(self, z):
y = z
y_original_color = y.color
if z.left == self.nil:
x = z.right
self.transplant(z, z.right)
elif z.right == self.nil:
x = z.left
self.transplant(z, z.left)
else:
y = z.right.minimum()
y_original_color = y.color
x = y.right
if y.p == z:
x.p = y
else:
self.transplant(y, y.right)
y.right = z.right
y.right.p = y
self.transplant(z, y)
y.left = z.left
y.left.p = y
y.color = z.color
traverse = x.p
while traverse != self.nil:
traverse.size = traverse.size - 1
traverse = traverse.p
if y_original_color == 1:
self.delete_fixup(x)
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.nil:
y.left.p = x
y.p = x.p
if x.p == self.nil:
self.root = y
elif x.p.left == x:
x.p.left = y
else:
x.p.right = y
y.left = x
x.p = y
y.size = x.size
x.size = x.left.size + x.right.size + 1
def right_rotate(self, y):
x = y.left
y.left = x.right
if x.right != self.nil:
x.right.p = y
x.p = y.p
if y.p == self.nil:
self.root = x
elif y.p.right == y:
y.p.right = x
else:
y.p.left = x
x.right = y
y.p = x
x.size = y.size
y.size = y.left.size + y.right.size + 1
def rank(self, x):
r = x.left.size + 1
y = x
while y != self.root:
if y.p.right == y:
r = r + y.p.left.size + 1
y = y.p
return r