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