-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbstsize_rl.py
191 lines (168 loc) · 6.2 KB
/
bstsize_rl.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
class BSTnode(object):
"""docstring for BSTnode"""
def __init__(self, parent, t):
self.parent = parent
self.key = t
self.left = None
self.right = None
self.size = 1
def update_stats(self):
self.size = (0 if self.left is None else self.left.size) + \
(0 if self.right is None else self.right.size)
def insert(self, t, NodeType):
self.size += 1
if t < self.key:
if self.left is None:
self.left = NodeType(self, t)
return self.left
else:
return self.left.insert(t, NodeType)
else:
if self.right is None:
self.right = NodeType(self, t)
return self.right
else:
return self.right.insert(t, NodeType)
def find(self, t):
if t == self.key:
return self
elif t < self.key:
if self.left is None:
return None
else:
return self.left.find(t)
else:
if self.right is None:
return None
else:
return self.right.find(t)
def rank(self, t):
"""Return the number of keys <= t in the subtree rooted at this node."""
left_size = 0 if self.left is None else self.left.size
if t == self.key:
return left_size + 1
elif t < self.key:
if self.left is None:
return 0
else:
return self.left.rank(t)
else:
if self.right is None:
return left_size + 1
else:
return self.right.rank(t) + left_size + 1
def minimum(self):
current = self
while current.left is not None:
current = current.left
return current
def successor(self):
if self.right is not None:
return self.right.minimum()
current = self
while current.parent is not None and current.parent.right is current:
current = current.parent
return current.parent
def delete(self):
if self.left is None or self.right is None:
if self is self.parent.left:
self.parent.left = self.left or self.right
if self.parent.left is not None:
self.parent.left.parent = self.parent
else:
self.parent.right = self.left or self.right
if self.parent.right is not None:
self.parent.right.parent = self.parent
current = self.parent
while current is not None:
current.update_stats()
current = current.parent
return self, self.parent
else:
s = self.successor()
self.key, s.key = s.key, self.key
return s.delete()
def check(self, lo, hi):
if lo is not None and self.key <= lo:
raise "BST RI violation"
if hi is not None and self.key >= hi:
raise "BST RI violation"
if self.left is not None:
if self.left.parent is not self:
raise "BST RI violation"
self.left.check(lo, self.key)
if self.right is not None:
if self.right.parent is not self:
raise "BST RI violation"
self.right.check(self.key, hi)
if self.size != 1 + (0 if self.left is None else self.left.size) + (0 if self.right is None else self.right.size):
raise "BST RI violation"
def __repr__(self):
return "BST Node, key: %s" % str(self.key)
class BST(object):
"""docstring for BST"""
def __init__(self, NodeType=BSTnode):
self.root = None
self.NodeType = NodeType
# self.psroot = self.NodeType(None, None)
# def reroot(self):
# self.root = self.psroot.left
def insert(self, t):
if self.root is None:
# self.psroot.left = self.NodeType(self.psroot, t)
# self.reroot()
self.root = self.NodeType(None, t)
return self.root
else:
return self.root.insert(t, self.NodeType)
def find(self, t):
if self.root is None:
return None
else:
return self.root.find(t)
def rank(self, t):
if self.root is None:
return 0
else:
self.root.rank(t)
def delete(self, t):
"""Delete the node for key t if it is in the tree."""
node = self.find(t)
deleted = node.delete()
# self.reroot()
return deleted
def check(self):
if self.root is not None:
self.root.check(None, None)
def __str__(self):
node = self.root
if node is None:
return '<empty tree>'
def recurse(node):
if node is None: return [], 0, 0
label = str(node.key)
left_lines, left_pos, left_width = recurse(node.left)
right_lines, right_pos, right_width = recurse(node.right)
middle = max(right_pos + left_width - left_pos + 1, len(label),2)
pos = left_pos + middle // 2
width = left_pos + middle + right_width - right_pos
label = label.center(middle, '=')
if len(left_lines) < len(right_lines):
left_lines.extend([' ' * left_width]*(len(right_lines) - len(left_lines)))
if len(left_lines) > len(right_lines):
right_lines.extend([' ' * right_width]*(len(left_lines) - len(right_lines)))
lines = [' ' * left_pos + label + ' ' * (right_width - right_pos)]+\
[' ' * (left_pos)+'/'+' '*(middle-2)+'\\'+' '*(right_width - right_pos)]+\
[left_line+' '*(width - left_width - right_width)+right_line for left_line, right_line in zip(left_lines, right_lines)]
return lines, pos, width
return '\n'.join(recurse(self.root) [0])
def test():
test1 = range(0, 100, 10)
test2 = [31, 41, 59, 26, 53, 58, 97, 93, 23]
mytree = BST()
print mytree
for x in test2:
mytree.insert(x)
print
print mytree
return mytree