This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpov.py
More file actions
67 lines (48 loc) · 1.82 KB
/
Copy pathpov.py
File metadata and controls
67 lines (48 loc) · 1.82 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
from json import dumps
class Tree:
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):
return self.clone().from_pov_helper(from_node)
def path_to(self, from_node, to_node):
path = self.from_pov(from_node).path_to_helper(to_node)
if path is None:
raise ValueError(r".+")
return path
def clone(self):
return Tree(self.label, [c.clone() for c in self.children])
def find(self, label):
nodes = [(None, self)]
while nodes != []:
parent, curr = nodes.pop()
if curr.label == label:
return (parent, curr)
nodes.extend((curr, c) for c in curr.children)
raise ValueError(r".+")
def from_pov_helper(self, from_node):
parent, from_node = self.find(from_node)
if parent is not None:
for i in range(len(parent.children)):
if parent.children[i].label == from_node.label:
parent.children.pop(i)
break
parent = self.from_pov_helper(parent.label)
from_node.children.append(parent)
return from_node
def path_to_helper(self, to_node):
if self.label == to_node:
return [self.label]
for c in self.children:
path = c.path_to_helper(to_node)
if path is not None:
return [self.label] + path
return None