我有一个数组(树),如:打印数组作为树
['Humidity\n', ['High', ['Outlook\n', ['Sunny', ['Temp\n', ['Hot', ['Wind\n', ['Weak', ['No']], ['Strong', ['No']]]], ['Mild', ['No']]]], ['Overcast', ['Temp\n', ['Hot', ['Yes']], ['Mild', ['Yes']]]], ['Rain', ['Temp\n', ['Mild', ['Wind\n', ['Weak', ['Yes']], ['Strong', ['No']]]]]]]], ['Normal', ['Wind\n', ['Weak', ['Outlook\n', ['Rain', ['Yes']], ['Overcast', ['Temp\n', ['Cool', ['Yes']], ['Hot', ['Yes']]]], ['Sunny', ['Yes']]]], ['Strong', ['Temp\n', ['Cool', ['No']], ['Mild', ['Outlook\n', ['Rain', ['Yes']], ['Sunny', ['Yes']]]]]]]]]
我想打印一棵树。这是我尝试
def printTree(treeToPrint, tab):
print tab + treeToPrint[0]
newTab = tab + "--"
nextTree = treeToPrint[1:]
for tree in nextTree:
print newTab + tree[0]
printTree(tree[1], newTab + "--")
在打印输出一样:
Humidity
--High
----Outlook
------Sunny
--------Temp
----------Hot
------------Wind
--------------Weak
----------------No
--------------Strong
----------------No
----------Mild
------------No
------Overcast
--------Temp
----------Hot
------------Yes
----------Mild
------------Yes
------Rain
--------Temp
----------Mild
------------Wind
--------------Weak
----------------Yes
--------------Strong
----------------No
--Normal
----Wind
------Weak
--------Outlook
----------Rain
------------Yes
----------Overcast
------------Temp
--------------Cool
----------------Yes
--------------Hot
----------------Yes
----------Sunny
------------Yes
------Strong
--------Temp
----------Cool
------------No
----------Mild
------------Outlook
--------------Rain
----------------Yes
--------------Sunny
----------------Yes
有人可以帮我代表了一个漂亮的格式,这棵树。
定义'漂亮的格式'请... – ChristopheD
看看他们如何在这里打印出树木:http://6.006.scripts.mit.edu/~6.006/spring08/wiki/index.php?title=Binary_Search_树可能有点像你想以漂亮的格式做 –
我的意思是,像这样:http://upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Binary_tree.svg/220px-Baryary_tree.svg。 png – riship89