-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathwalker.py
37 lines (30 loc) · 997 Bytes
/
walker.py
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
from gen import Tree, gentree
from operator import lt, gt
from sys import stdout
class DrawTree:
def __init__(self, tree, parent, depth=-1):
self.x = -1
self.y = depth
self.tree = tree
self.children = [DrawTree(t, self, depth + 1) for t in tree]
# XXX: how else do I determine if a node has a left brother?
# Should this be here when I have to maintain the ancestor node
# anyway
self.parent = parent
self.thread = None
self.mod = None
self.ancestor = self
def left(self):
return self.thread or len(self.children) and self.children[0]
def right(self):
return self.thread or len(self.children) and self.children[-1]
def lbrother(self):
n = None
for node in self.parent.children:
if node == self:
return n
else:
n = self
def walker(tree):
dt = DrawTree(tree, None, 0)
firstwalk(tree)