Created
January 21, 2020 16:50
-
-
Save alpden550/0bb86e76671f2cb2d03de15a1bf7afea to your computer and use it in GitHub Desktop.
Flat tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flat = {} | |
make_tree_flat(tree, flat) | |
{'a': (None, ['b', 'e']), | |
'b': ('a', ['c', 'd']), | |
'c': ('b', []), | |
'd': ('b', []), | |
'e': ('a', ['f']), | |
'f': ('e', ['g']), | |
'g': ('f', [])} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tree = ('a', [ | |
('b', [ | |
('c', []), | |
('d', []), | |
]), | |
('e', [ | |
('f', [ | |
('g', []), | |
]), | |
]), | |
]) | |
def make_tree_flat(tree, dictionary, parent=None): | |
node, branches = tree | |
children = [] | |
dictionary[node] = (parent, children) | |
for branch in branches: | |
name = make_tree_flat(branch, dictionary, parent=node) | |
children.append(name) | |
return node |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment