First tree
This commit is contained in:
parent
84017f8208
commit
f2810f8fa8
35
Tree.py
Executable file
35
Tree.py
Executable file
@ -0,0 +1,35 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# \file Tree.py
|
||||
# \brief TODO
|
||||
# \author Florent Guiotte <florent.guiotte@gmail.com>
|
||||
# \version 0.1
|
||||
# \date 21 sept. 2017
|
||||
#
|
||||
# TODO details
|
||||
|
||||
import collections
|
||||
|
||||
def Tree():
|
||||
return collections.defaultdict(Tree)
|
||||
|
||||
def walk(node):
|
||||
"""iterate tree in pre-order depth"""
|
||||
if isinstance(node, str): yield "+-" + node
|
||||
if not isinstance(node, str):
|
||||
for child in node.values():
|
||||
for n in walk(child):
|
||||
yield "| " + n
|
||||
|
||||
if __name__ == "__main__":
|
||||
t = Tree()
|
||||
t[1] = "test"
|
||||
t[2][1] = "yoyo"
|
||||
t[2][2] = "qweqweqwe"
|
||||
t[3][1] = "u wot"
|
||||
t[2][3][1] = "yoloy"
|
||||
t[2][3][2][1] = "qwe"
|
||||
t[4] = "wat"
|
||||
|
||||
for i in walk(t):
|
||||
print(i)
|
||||
Loading…
Reference in New Issue
Block a user