Skip to content

Instantly share code, notes, and snippets.

@zopieux
Created May 11, 2015 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zopieux/cc4c4154ab5b6936b08a to your computer and use it in GitHub Desktop.
Save zopieux/cc4c4154ab5b6936b08a to your computer and use it in GitHub Desktop.
Python tree
def tree(root):
"""
Node(label = "string", children = [list of Node])
"""
ret = [root.label]
ln = len(root.children)
for i, child in enumerate(root.children):
for j, s in enumerate(tree(child)):
char = '│ '
if i == ln - 1: # last child
if j == 0: # first line of last child
char = '└─'
else:
char = ' '
elif j == 0: # first line of other children
char = '├─'
ret.append('%s %s' % (char, s))
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment