-
Notifications
You must be signed in to change notification settings - Fork 10
/
trees.py
348 lines (266 loc) · 9.23 KB
/
trees.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import random
########## Nodes.
# Nodes to be used in trees.
class TreeNode:
'''A node in a binary tree.'''
def __init__(self, n):
self.data = n
self.left = self.right = None
def __str__(self):
return str(self.data)
def __repr__(self):
return self.__str__()
def num_children(self):
'''N.num_children() -> int
Returns the number of children of N that are not None.
'''
return sum([1 for child in [self.left, self.right] if child])
class TreapNode(TreeNode):
'''A node in a treap.'''
def __init__(self, data, priority):
super().__init__(data) # Parent constructor.
self.priority = priority
def __str__(self):
return "({}, {})".format(self.data, self.priority)
class AvlNode(TreeNode):
'''A node in an AVL tree.'''
def __init__(self, data):
super().__init__(data) # Parent constructor.
self.height = 0
def __str__(self):
return "({}, {})".format(self.data, self.height)
########## Trees. ##########
# Trees utilizing above nodes. Use helper functions defined outside
# the class to achieve functionality.
class Bst:
'''A BST. Does not contain duplicates. Nodes are of type TreeNode.'''
def __init__(self):
self.root = None
self.size = 0
def __str__(self):
return tree_string(self.root)
def __repr__(self):
return self.__str__()
def add(self, n):
self.root, added = bst_add(self.root, n)
if added:
self.size += 1
return added
def find(self, n):
return bst_find(self.root, n)
def remove(self, n):
self.root, removed = bst_remove(self.root, n)
if removed:
self.size -= 1
return removed
def clear(self):
self.__init__()
class Treap(Bst):
'''A treap. Does not contain duplicates. Nodes are of type TreapNode.'''
max_priority = 1 << 10
def __init__(self):
super().__init__()
self.priorities = set()
def add(self, n):
priority = random.randint(0, Treap.max_priority)
while priority in self.priorities:
priority = random.randint(0, Treap.max_priority)
self.root, added = treap_add(self.root, n, priority)
if added:
self.size += 1
self.priorities.add(priority)
return added
def remove(self, n):
self.root, removed = treap_remove(self.root, n)
if removed:
self.size -= 1
return removed
class AvlTree(Bst):
'''An AVL tree. Does not contain duplicates. Nodes are of type AvlNode.'''
def __init__(self):
super().__init__()
def add(self, n):
self.root, added = avl_add(self.root, n)
if added:
self.size += 1
return added
def remove(self, n):
self.root, removed = avl_remove(self.root, n)
if removed:
self.size -= 1
return removed
########## Tree helper functions. ##########
# Work for any type of node above.
def tree_string(node, level = 0):
'''tree_string(node) -> str
Returns a string representation of the subtree rooted at node.
credit: https://stackoverflow.com/questions/20242479/printing-a-tree-data-structure-in-python
'''
if not node:
return '\n'
prefix = ' '*level
str = repr(node) + '\n'
if node.num_children():
str += prefix + '|_ ' + tree_string(node.left, level+1)
str += prefix + '|_ ' + tree_string(node.right, level+1)
return str
def tree_size(node):
'''tree_size(node) -> int
Returns a string representation of the subtree rooted at node.
'''
pass
def tree_height(node):
'''tree_height(node) -> int
Returns the height of the subtree rooted at node. Returns -1 if
node is None.
A node's height is the value of its height attribute, if it
exists. Otherwise it has to be computed.
See
- EAFP at https://docs.python.org/3.4/glossary.html
- https://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python
'''
pass
def inorder(n):
'''inorder(node) -> [node content]
Returns an inorder traversal of the subtree rooted at node; empty
list if n is None.
'''
pass
def preorder(n):
'''preorder(node) -> [node content]
Returns an preorder traversal of the subtree rooted at node; empty
list if n is None.
'''
pass
def postorder(n):
'''postorder(node) -> [node content]
Returns an postorder traversal of the subtree rooted at node;
empty list if n is None.
'''
pass
def update_height(node):
'''update_height(node) -> None
Updates the value of node's height attribute using the height of
its children.
Assumes that node has a height attribute.
'''
pass
def rotate_left(node):
'''rotate_left(node) -> node
Returns the root of the tree obtained by rotating node to the
left. Updates the height attribute of nodes where necessary and if
the attribute is present.
'''
pass
def rotate_right(node):
'''rotate_right(node) -> node
Returns the root of the tree obtained by rotating node to the
right. Updates the height attribute of nodes where necessary and if
the attribute is present.
'''
pass
########## BST helper functions. ##########
def bst_find(node, n):
'''bst_find(node, int) -> bool
Returns whether n is contained in the subtree rooted at
node. Assumes the subtree to be a BST with no duplicates.
'''
pass
def bst_find_min(node):
'''bst_find_min(node) -> int
Returns the smallest value stored in the subtree rooted at
node. Assumes the subtree to be a BST with no duplicates.
'''
pass
def bst_add(node, n):
'''bst_add(node, int) -> (node, bool)
Returns the result of adding n to the subtree rooted at
node. Assumes the subtree to be a BST with no duplicates.
The first returned value is the root of the tree obtained as a
result of the addition. The second value indicates whether
addition succeeded. Addition fails if n is already present in the
subtree.
'''
pass
def bst_remove(node, n):
'''bst_remove(node, int) -> (node, bool)
Returns the result of removing n from the subtree rooted at
node. Assumes the subtree to be a BST with no duplicates.
The first returned value is the root of the tree obtained as a
result of the removal. The second value indicates whether removal
succeeded. Removal fails if n is not present in the subtree.
'''
pass
########## Treap helper functions. ##########
def treap_add(node, n, p):
'''treap_add(node, int, int) -> (node, bool)
Returns the result of adding n with priority, p, to the subtree
rooted at node. Assumes the subtree to be a treap with no
duplicate values.
The first returned value is the root of the treap obtained as a
result of the addition. The second value indicates whether
addition succeeded. Addition fails if n is already present in the
subtree.
'''
pass
def treap_remove(node, n):
'''treap_remove(node, int) -> (node, bool)
Returns the result of removing n from the subtree rooted at
node. Assumes the subtree to be a treap with no duplicate values.
The first returned value is the root of the treap obtained as a
result of the removal. The second value indicates whether removal
succeeded. Removal fails if n is not present in the subtree.
'''
pass
########## AVL helper functions. ##########
def avl_balanced(node):
'''avl_balanced(node) -> bool
Returns whether the AVL property is satisfied at node. Should work
for any of the nodes defined above.
'''
pass
def avl_left_left(node):
'''avl_left_left(node) -> node
Returns the root of the tree obtained by resolving a left-left
case at node.
'''
pass
def avl_right_right(node):
'''avl_right_right(node) -> node
Returns the root of the tree obtained by resolving a right_right
case at node.
'''
pass
def avl_left_right(node):
'''avl_left_right(node) -> node
Returns the root of the tree obtained by resolving a left_right
case at node.
'''
pass
def avl_right_left(node):
'''avl_right_left(node) -> node
Returns the root of the tree obtained by resolving a right_left
case at node.
'''
pass
def avl_add(node, n):
'''avl_add(node, int) -> (node, bool)
Returns the result of adding n to the subtree rooted at
node. Assumes the subtree to be a valid AVL tree with no
duplicates.
The first returned value is the root of the AVL tree obtained as a
result of the addition. The second value indicates whether
addition succeeded. Addition fails if n is already present in the
subtree.
'''
pass
def avl_remove(node, n):
'''avl_remove(node, int) -> (node, bool)
Returns the result of removing n from the subtree rooted at
node. Assumes the subtree to be a valid AVL tree with no
duplicates.
The first returned value is the root of the AVL tree obtained as a
result of the removal. The second value indicates whether removal
succeeded. Removal fails if n is not present in the subtree.
'''
pass