Skip to content

Instantly share code, notes, and snippets.

@alpden550
Created January 21, 2020 16:50
Show Gist options
  • Save alpden550/0bb86e76671f2cb2d03de15a1bf7afea to your computer and use it in GitHub Desktop.
Save alpden550/0bb86e76671f2cb2d03de15a1bf7afea to your computer and use it in GitHub Desktop.
Flat tree
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', [])}
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