私はこれを試してみます: (疑似コードではなく Python 2.x を自由に使用しました)。
# Doesn't work yet.
# Assumes two characters ('42', '06' etc) per string representation of number.
# If not, alter the formatting %02d as appropriate.
global line_length
line_length=80
def pad(number_of_nodes):
global line_length
return "_"*(line_length/number_of_nodes)
def printlevel(nodes):
global line_length
padstring=pad(len(nodes))
stringnodes=[ "%02d"%(n) for n in nodes ]
leader="_"* abs( (line_length/2) - len(padstring) )
print leader, padstring.join(stringnodes)
for level in [ [50],
[42,71],
[31,45,60,98],
[6,11,43,49,55]
]:
printlevel(level)
(私が信じている)質問は、実際にツリーノードにアクセスするよりもフォーマットに関するものであると思います...ツリーをリストのリストにフラット化しました..これは実際には機能しませんが、再配置できると思いますそれを機能させるために....