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
|
class Node(object):
# ctor :: a -> [a] -> Node(a)
def __init__(self, value, children=[]):
self.value = value
self.children = children
# is_leaf :: Node(a) -> Boolean
def is_leaf(node):
return len(node.children) == 0
# enumerate :: Node(a) -> Set(List(a))
def enumerate(node):
current = []
result = []
q = []
q.append(node)
while q:
x = q.pop()
print(x.value)
for c in x.children:
q.append(c)
current.append(x.value)
print(current)
if is_leaf(x):
result.append(current)
print("Reseting current")
current = []
return result
node = Node('root', [
Node('a', [
Node('b', [Node('c')]),
Node('c', [Node('b')]),
]),
Node('b', [
Node('a', [Node('c')]),
Node('c', [Node('a')]),
]),
Node('c', [
Node('a', [Node('b')]),
Node('b', [Node('a')]),
])
])
print('----------')
print(enumerate(node))
|