-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_tree_with_recursion.py
43 lines (38 loc) · 1.11 KB
/
to_tree_with_recursion.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
def build_tree(nodes: dict, levels: set, tree: dict):
"""Recursive graph traversal for the build tree"""
for level in levels:
tree[level] = build_tree(nodes, nodes[level], {})
return tree
def to_tree(source: list) -> dict:
"""Function creation tree from pair parent - child"""
roots = set()
nodes = {name: set() for pair in source for name in pair if name}
for parent, child in source:
if not parent:
roots.add(child)
continue
nodes[parent].add(child)
return build_tree(nodes=nodes, levels=roots, tree={})
if __name__ == '__main__':
source = [
(None, 'a'),
(None, 'b'),
(None, 'c'),
('a', 'a1'),
('a', 'a2'),
('a2', 'a21'),
('a2', 'a22'),
('b', 'b1'),
('b1', 'b11'),
('b11', 'b111'),
('b', 'b2'),
('c', 'c1'),
]
expected = {
'a': {'a1': {}, 'a2': {'a21': {}, 'a22': {}}},
'b': {'b1': {'b11': {'b111': {}}}, 'b2': {}},
'c': {'c1': {}},
}
result = to_tree(source)
print(result)
assert result == expected