-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathgraphs.py
81 lines (56 loc) · 1.92 KB
/
graphs.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
from typing import List
class Vertex:
def __init__(self, val):
self.val = val
self.edges = set()
def __str__(self):
return f"Vertex({self.val}) with edges {len(self.edges)}"
class Ditex(Vertex): # Directed vertex
def __init__(self, val):
self.val = val
self.in_edges = set()
self.out_edges = set()
@property
def edges(self):
return self.in_edges | self.out_edges
def __str__(self):
return f"Ditex({self.val})(in:{len(self.in_edges)})(out:{len(self.out_edges)})"
class BiNode:
def __str__(self):
return f"({self.left if self.left else '_' }/{self.val}\\{self.right if self.right else '_'})"
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BiPaNode(BiNode):
def __init__(self, val, left=None, right=None):
super().__init__(val, left=left, right=right)
self.parent = None
def ltbt(arr: List[int]) -> BiNode:
return list_to_binary_tree(arr)
def list_to_binary_tree(arr: List[int]) -> BiNode:
nodes = [BiPaNode(arr[0])]
for i in range(1, len(arr)):
parent = nodes[(i - 1)//2]
if not parent and arr[i]:
raise Exception(f"Cannot have children without parents to attach to,"
f" for {arr[i]} on input {arr}")
if not arr[i]:
nodes.append(None)
else:
node_now = BiPaNode(arr[i])
node_now.parent = parent
nodes.append(node_now)
if (i-1) % 2:
parent.right = node_now
else:
parent.left = node_now
return nodes[0]
def get_node_by_val(root: BiNode, target):
if not root:
return None
if root.val == target:
return root
else:
return get_node_by_val(root.left, target) \
or get_node_by_val(root.right, target)