forked from PedroCardouzo/FormalLanguages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.py
52 lines (46 loc) · 1.12 KB
/
tree_test.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class TreeNode:
"""value of the node and an arbitrary number of children"""
def __init__(self, value, *children):
self.children = [x for x in children if x is not None]
self.value = value
# extract_all :: TreeNode -> [String]
# if is a leaf, returns a list containing only the node value of the leaf.
# else appends itself in front of every element in the list returned by calling this function recursively on its children
def extract_all(node):
if node.children == []:
return [node.value]
else:
acc = []
for child in node.children:
acc += [node.value+x for x in extract_all(child)]
return acc
def main():
# just some example code to show a group buddy how to get every possible path through a TreeNode
t = TreeNode(
'A',
TreeNode(
'B',
TreeNode(
'1'
),
TreeNode(
'2'
)
),
TreeNode(
'C',
TreeNode(
'+'
),
TreeNode(
'-'
),
TreeNode(
'*',
TreeNode('#')
)
)
)
print(extract_all(t)) # expected output is ['AB1', 'AB2', 'AC+', 'AC-', 'AC*#']
if __name__ == '__main__':
main()