Skip to content

Commit 546daa1

Browse files
committed
Added Binary Euler Tour
1 parent 85930d8 commit 546daa1

File tree

3 files changed

+86
-35
lines changed

3 files changed

+86
-35
lines changed

.idea/workspace.xml

Lines changed: 46 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
3.85 KB
Binary file not shown.

08-Trees/binary_euler_tour.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import euler_tour
2+
3+
class BinaryEulerTour(euler_tour.EulerTour):
4+
""" Abstract base class for performing Euler tour of a binary tree.
5+
6+
This version includes an additional hook invisit that is called after the tour
7+
of the left subtree( if any), yet before the tour of the right subtree( if any).
8+
9+
Note: Right child is always assigned index 1 in path, even if no left sibling. """
10+
11+
def _tour(self, p, d, path):
12+
results = [None, None]
13+
self._hook_previsit(p, d, path)
14+
if self._tree.left(p) is not None:
15+
path.append(0)
16+
results[0] = self._tour(self._tree.left(p), d + 1, path)
17+
path.pop()
18+
self._hook_invisit(p, d, path)
19+
if self._tree.right(p) is not None:
20+
path.append(1)
21+
results[1] = self._tour(self._tree.left(p), d + 1, path)
22+
path.pop()
23+
answer = self._hook_postvisit(p, d, path, results)
24+
return answer
25+
26+
def _hook_invisit(self, p, d, path):
27+
pass
28+
29+
30+
class BinaryLayout(BinaryEulerTour):
31+
"""Class for computing (x, y) coordinates for node of a binary tree"""
32+
33+
def __init__(self, tree):
34+
super().__init__(tree)
35+
self._count = 0
36+
37+
def _hook_invisit(self, p, d, path):
38+
p.element().setX(self._count)
39+
p.element().setY(d)
40+
self._count += 1

0 commit comments

Comments
 (0)