-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler_tour.py
More file actions
131 lines (106 loc) · 3.57 KB
/
euler_tour.py
File metadata and controls
131 lines (106 loc) · 3.57 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
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
"""
Supports variations on the Euler Tour Traversal algorithm
"""
from tree import Tree
class EulerTour:
"""
Abstract base class for performing a Euler Tour of a Tree
Hook methods _hook_previsit and _hook_postvisit can be overridden by subclasses
"""
def __init__(self, tree):
"""
Constructs a EulerTour template for a given tree
tree: a Tree instance
"""
self._tree = tree
def tree(self):
"""
returns: a reference to the tree being traversed
"""
return self._tree
def execute(self):
"""
Perform the tour and return any result from post visit of the root
"""
if len(self._tree) > 0:
return self._tour(self._tree.root(), 0, [])
def _tour(self, p, d, path):
"""
Performs a tour of the subtree rooted at p
p: a Position in the Tree
d: depth of p in the Tree
path: list of indices of children on path from the root to p
"""
self._hook_previsit(p, d, path)
results = []
path.append(0)
for c in self._tree.children(p):
results.append(self._tour(c, d + 1, path))
path[-1] += 1
path.pop()
answer = self._hook_postvisit(p, d, path, results)
return answer
#----Hook methods to be overridden----
def _hook_previsit(self, p, d, path):
pass
def _hook_postvisit(self, p, d, path, results):
pass
#-------------------------Euler Tour subclasses-------------------------
class PreorderPrintIndentedTour(EulerTour):
"""
Produces an indented preorder list of a Tree's elements
"""
def _hook_previsit(self, p, d, path):
print(2 * d * ' ' + str(p.element()))
class PreorderPrintLabeledTour(EulerTour):
"""
Produces a labeled and indented preorder list of a Tree's elements
"""
def _hook_previsit(self, p, d, path):
label = '.'.join(str(j+1) for j in path)
print(2 * d * ' ' + label, str(p.element()))
class ParenthesizeTour(EulerTour):
"""
Prints aparenthetic representation of a tree
"""
def _hook_previsit(self, p, d, path):
if path and path[-1] > 0:
print(', ', end='')
print(p.element(), end='')
if not self.tree().is_leaf(p):
print('(', end='')
def _hook_postvisit(self, p, d, path, results):
if not self.tree().is_leaf(p):
print(')', end='')
class BinaryEulerTour(EulerTour):
"""
Abstract base class for performing Euler Tour of a Binary Tree
Contains an additional _hook_invisit called after the tour of the left subtree
"""
def _tour(self, p, d, path):
results = [None, None]
self._hook_previsit(p, d, path)
if self._tree.left(p) is not None:
path.append(0)
results[0] = self._tour(self._tree.left(p), d+1, path)
path.pop()
self._hook_invisit(p, d, path)
if self._tree.right(p) is not None:
path.append(1)
results[1] = self._tour(self._tree.right(p), d+1, path)
path.pop()
answer = self._hook_postvisit(p, d, path, results)
return answer
def _hook_invisit(self, p, d, path):
pass
class BinaryLayout(BinaryEulerTour):
"""
Class for computing the (x,y) cordinates for each node of a binary tree
"""
def __init__(self, tree):
super().__init__(tree)
self._count = 0
def _hook_invisit(self, p, d, path):
p.element().setX(self._count)
p.element().setY(d)
self._count += 1