-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpov.py
More file actions
87 lines (64 loc) · 2.55 KB
/
pov.py
File metadata and controls
87 lines (64 loc) · 2.55 KB
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
from json import dumps
class Tree(object):
def __init__(self, label, children=[]):
self.label = label
self.children = children
def __dict__(self):
return {self.label: [c.__dict__() for c in sorted(self.children)]}
def __str__(self, indent=None):
return dumps(self.__dict__(), indent=indent)
def __lt__(self, other):
return self.label < other.label
def __eq__(self, other):
return self.__dict__() == other.__dict__()
def from_pov(self, from_node):
if self.label == from_node:
return self
for i in range(len(self.children)):
if self.children[i].label == from_node:
x = self.children[i]
current = i
y= Tree(self.label)
if self.children:
for j in range(len(self.children)):
if j != current:
y.children = y.children+ [self.children[j]]
x.children = x.children + [y]
return x
else:
try:
x = self.children[i].from_pov(from_node)
current = i
y= Tree(self.label)
if self.children:
for j in range(len(self.children)):
if j != current:
y.children = y.children+ [self.children[j]]
x.add_tree(self.children[current].label,y)
return x
except ValueError :
pass
else:
raise ValueError('ValueError')
def add_tree(self,label, subtree):
for i in range(len(self.children)):
if label == self.children[i].label:
self.children[i].children = self.children[i].children + [subtree]
else:
z = self.children[i].add_tree(label,subtree)
if z is None:
continue
def path_to(self, from_node, to_node):
node_tree = self.from_pov(from_node)
for child in node_tree.children:
if child.label == to_node:
return [from_node,child.label]
else:
try:
y= child.path_to(child.label,to_node)
except ValueError:
pass
else:
return [from_node]+ y
else:
raise ValueError('ValueError')