Created
May 11, 2015 08:03
-
-
Save zopieux/cc4c4154ab5b6936b08a to your computer and use it in GitHub Desktop.
Python 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
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