-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch4_treesandgraphs.py
419 lines (334 loc) · 11.2 KB
/
ch4_treesandgraphs.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Problems from Chapter 4 of Cracking the Coding Interview"""
import dataclasses
import random
from collections import deque
@dataclasses.dataclass
class node:
id: int
visited: bool = False
children: [] = dataclasses.field(default_factory=list)
@dataclasses.dataclass
class binarytree_node:
id: int
left: None = None
right: None = None
parent: None = None
def isleaf(self):
return self.left is None and self.right is None
def inorder_traversal(tree):
if tree is None:
return []
else:
left_tree = inorder_traversal(tree.left)
right_tree = inorder_traversal(tree.right)
return left_tree + [tree.id] + right_tree
def preorder_traversal(tree):
if tree is None:
return []
else:
left_tree = preorder_traversal(tree.left)
right_tree = preorder_traversal(tree.right)
return [tree.id] + left_tree + right_tree
def postorder_traversal(tree):
if tree is None:
return []
else:
left_tree = postorder_traversal(tree.left)
right_tree = postorder_traversal(tree.right)
return left_tree + right_tree + [tree.id]
def setupgraph():
nodes = {}
for i in range(26):
char = chr(ord("A") + i)
nodes[char] = node(char)
nodes["S"].children.extend([nodes["A"], nodes["B"]])
nodes["A"].children.extend([nodes["C"], nodes["D"]])
nodes["B"].children.extend([nodes["E"]])
nodes["C"].children.extend([nodes["A"], nodes["B"]])
return nodes
def dfs(node, target, graph):
print(f"Exploring {node.id}")
node.visited = True
if node.id == target:
return True
else:
for node in graph[node.id].children:
if not node.visited:
if dfs(node, target, graph):
return True
return False
def bfs(node, target, graph):
tovisit = deque([node])
while tovisit:
visiting = tovisit.pop()
visiting.visited = True
if visiting.id == target:
return True
tovisit.extendleft(visiting.children)
return False
def routebetweennodes(graph):
return dfs(graph["S"], "E", graph)
return bfs(graph["S"], "E", graph)
def insert_value(value, tree):
if tree is None:
return binarytree_node(value)
else:
cursor = tree
while True:
if value <= cursor.id:
if cursor.left is None:
cursor.left = binarytree_node(value)
return tree
else:
cursor = cursor.left
else:
if cursor.right is None:
cursor.right = binarytree_node(value)
return tree
else:
cursor = cursor.right
# def minimalbst(nums, tree=None):
# midindex = len(nums)//2
# if nums:
# tree = insert_value(nums[midindex], tree)
# tree = minimalbst(nums[:midindex], tree)
# tree = minimalbst(nums[midindex+1:], tree)
# return tree
def minimalbst(nums):
if not nums:
return None
else:
midindex = len(nums) // 2
tree = binarytree_node(nums[midindex])
tree.left = minimalbst(nums[:midindex])
tree.right = minimalbst(nums[midindex + 1:])
return tree
def setup_binarytree_imbalanced():
"""
5
/ \
3 19
/ \
2 17
/ /
1 4
"""
btn = binarytree_node
return btn(5, btn(3, btn(2, btn(1)), btn(17, btn(4))), btn(19))
def treedepth(tree):
if tree is None:
return 0
left_depth = treedepth(tree.left)
right_depth = treedepth(tree.right)
return max(left_depth, right_depth) + 1
def checkbalanced(tree):
if tree is None or tree.isleaf():
return True
if tree.left is not None:
if tree.left.isleaf() is False and tree.right is None:
return False
if tree.right is not None:
if tree.right.isleaf() is False and tree.left is None:
return False
return checkbalanced(tree.left) and checkbalanced(tree.right)
def is_bst(tree):
if tree is None:
return True
if tree.left is not None and tree.left.id > tree.id:
return False
if tree.right is not None and tree.right.id < tree.id:
return False
return is_bst(tree.left) and is_bst(tree.right)
def find_smallest(tree):
if tree is None:
raise Exception("Don't look for what doesn't exist")
if tree.left is None:
return tree.id
return find_smallest(tree.left)
def find_node(tree, value):
# cursor = tree
# while cursor is not None:
# if cursor.id == value:
# return cursor
# if cursor.id > value:
# cursor = cursor.left
# else:
# cursor = cursor.right
# return cursor
if tree is None or tree.id == value:
return tree
if value < tree.id:
return find_node(tree.left, value)
else:
return find_node(tree.right, value)
def find_successor(tree, value):
target_node = find_node(tree, value)
if target_node is None:
raise Exception("Get better trees")
if (
target_node.right is None and
target_node.parent is not None and
target_node.parent.id > value
):
return target_node.parent.id
return find_smallest(target_node.right)
def generate_bst_withparents():
btn = binarytree_node
tree = btn(5, btn(3), btn(17))
tree.left.parent = tree
tree.right.parent = tree
cursor = tree.right
cursor.left = btn(11)
cursor.right = btn(23)
cursor.left.parent = cursor
cursor.right.parent = cursor
return tree
def buildorder(projects, dependancies):
""" Dependancies:
Inputs: {'A': ['E', 'C'], 'B': ['C'], 'C': ['F'], 'D': [], 'E': [], 'F': []}
Outputs: {'A': [], 'B': [], 'C': ['B', 'A'], 'D': [], 'E': ['A'], 'F': ['C']}
"""
dependancy_inputs = {}
dependancy_outputs = {}
for project in projects:
dependancy_inputs[project] = []
dependancy_outputs[project] = []
for dependancy in dependancies:
dependancy_inputs[dependancy[1]].append(dependancy[0])
dependancy_outputs[dependancy[0]].append(dependancy[1])
buildorder = []
cyclic = False
print(dependancy_inputs)
print(dependancy_outputs)
while not cyclic and dependancy_inputs:
cyclic = True
freeprojects = []
for project, dependancies in dependancy_inputs.items():
if not dependancies:
cyclic = False
freeprojects.append(project)
buildorder.append(project)
for freeproject in freeprojects:
del dependancy_inputs[freeproject]
for project in dependancy_outputs[freeproject]:
dependancy_inputs[project].remove(freeproject)
if cyclic:
return []
return buildorder
def find_paths(tree, value1, value2):
path1 = None
path2 = None
tovisit = deque()
tovisit.append((tree, []))
while tovisit and (path1 is None or path2 is None):
visiting = tovisit.pop()
# print(visiting)
if visiting[0] is not None:
if visiting[0].id == value1:
path1 = visiting[1] + [value1]
elif visiting[0].id == value2:
path2 = visiting[1] + [value2]
leftnodetovisit = (visiting[0].left, visiting[1] + [visiting[0].id])
rightnodetovisit = (visiting[0].right, visiting[1] + [visiting[0].id])
tovisit.extendleft([leftnodetovisit, rightnodetovisit])
return path1, path2
def lastcommonality(path1, path2):
# [5, 3, 2, 1]
# [5, 3, 17, 4, 8, 9, 27]
set1 = set(path1)
for node in reversed(path2):
if node in set1:
return node
return None
def firstcommonancestor(tree, value1, value2):
# 5
# / \
# 11 7
# / \
# 3 13
# / \
# 2 1
if tree is None:
raise Exception("A tree of at least two nodes is required.")
path1, path2 = find_paths(tree, value1, value2)
if not (path1 and path2):
raise Exception("A value does not have a path")
return lastcommonality(path1, path2)
def bst_sequences(tree):
# 11
# / \
# 7 17
# / \
# 3 9
# / \
# 8 10
# [[3,8,10,17], [7,9], [11]]
pass
def preorder_traversal(tree):
if tree is None:
return ["X"]
else:
left_tree = preorder_traversal(tree.left)
right_tree = preorder_traversal(tree.right)
return [str(tree.id)] + left_tree + right_tree
def check_subtree(tree1, tree2):
tree1_traversal = preorder_traversal(tree1)
tree2_traversal = preorder_traversal(tree2)
return "".join(tree2_traversal) in "".join(tree1_traversal)
def tree_match(tree1, tree2):
if tree1 is None and tree2 is None:
return True
elif tree1 is None or tree2 is None:
return False
elif tree1.id != tree2.id:
return False
else:
return tree_match(tree1.left, tree2.left) and tree_match(tree1.right, tree2.right)
def check_subtree_alt(tree1, tree2):
if tree1 is None:
return False
elif tree1.id == tree2.id and tree_match(tree1, tree2):
return True
else:
return check_subtree_alt(tree1.left, tree2) or check_subtree_alt(tree1.right, tree2)
def pathswithsum(tree, value):
pathsums = []
tovisit = deque()
tovisit.append((tree,[]))
while tovisit:
visiting = tovisit.pop()
path = [val + visiting[0].id for val in visiting[1]]
path.append(visiting[0].id)
pathsums.extend(path)
if visiting[0].left is not None:
tovisit.append((visiting[0].left, path))
if visiting[0].right is not None:
tovisit.append((visiting[0].right, path))
return pathsums.count(value)
if __name__ == "__main__":
# graph = setupgraph()
# print(routebetweennodes(graph))
randomnums = sorted([random.randint(1, 50) for _ in range(11)])
bst = minimalbst(randomnums)
# print(randomnums)
# print(minimalbst(randomnums))
# print(checkbalanced(minimalbst(randomnums)))
# print(checkbalanced(setup_binarytree_imbalanced()))
# print(is_bst(setup_binarytree_imbalanced()))
# tree = generate_bst_withparents()
# print(find_successor(tree, 22))
# projects = ["A", "B", "C", "D", "E", "F", "H"]
# dependancies = [("E", "A"), ("C", "B"), ("C", "A"), ("F", "C"), ("C", "E"), ("F", "D")]
# print(buildorder(projects, dependancies))
# binarytree = setup_binarytree_imbalanced()
# print(firstcommonancestor(binarytree, 1, 5))
# print(inorder_traversal(bst))
# print(preorder_traversal(bst))
# print(postorder_traversal(bst))
tree1 = setup_binarytree_imbalanced()
btn = binarytree_node
tree2 = btn(17, btn(4))
# tree2 = btn(17, None, btn(4))
# print(check_subtree(tree1, tree2))
# print(check_subtree_alt(tree1, tree2))
print(pathswithsum(tree1, 5))