-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_order.py
41 lines (29 loc) · 848 Bytes
/
pre_order.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
#create generator that will yield the nodes in proper order
G = {
'a': ['b', 'c'],
'b': ['a', 'd', 'e'],
'c': ['a', 'h'],
'd': ['b'],
'e': ['b', 'f', 'g'],
'f': ['e'],
'g': ['e'],
'h': ['c', 'i'],
'i': ['h']}
#preorder: A, B, D, E, F, G, C, H, I
def pre_order(G, root):
generator = _pre_order(G, root, None)
print_generator(generator)
def print_generator(gen):
for e in gen:
if type(e) != str:
print_generator(e)
else:
print e
def _pre_order(G, root, parent):
children = get_children(G, root, parent)
yield root
for c in children:
yield _pre_order(G, c, root)
def get_children(G, root, parent):
return [child for child in G[root] if child != parent]
pre_order(G, 'a')