打印一棵漂亮的树
Print a Pretty Tree
我使用 BFS 从树中提取了一个列表
解决方案= [[6], [0, 7], [None, 3, None, 8], [2, 5, None, 9], [1, None, 4, None, None, None], [None, None, None, None]]
如何将其打印为水平树?
我正在尝试使用
打印它
def _print_tree(n=0,height,solution):
if n > height:
return
for j in range(len(solution[n])):
if solution[n][j]== None:
print(' ' * (2 ** (height - n + 1) - 1),end='')
else:
print(' ' * (2 ** (height - n + 1) - 1), end='')
print(' ',solution[n][j], end='')
但它给出了
我已经试过了,这是结果
nonechar = 'N'
spacechar = '_'
solution = [[6], [0, 7], [None, 3, None, 8], [2, 5, None, 9], [1, None, 4, None, None, None], [None, None, None, 4],[None, 3]]
for i in range(1, len(solution)):
for j in range(len(solution[i-1])):
if (solution[i-1][j] == None):
solution[i].insert(2*j, None)
solution[i].insert(2*j+1, None)
N = len(solution[-1]) * 2 - 1
offset = (N - 1) / 2
spacing = 0
for i in range(len(solution)):
line = spacechar * int(offset)
for j in range(len(solution[i])):
if (solution[i][j] == None):
line += nonechar
else:
line += str(solution[i][j])
if (j != len(solution[i]) - 1):
line += spacechar * int(spacing)
line += spacechar * int(offset)
print(line)
spacing = offset
offset = (offset - 1) / 2
我基本上做的是用缺失的数据填充解决方案列表,以便每个下一个子列表的值都是前一个的两倍。对于第 i
个子列表的每个第 j
个元素,在 [i+1][2*j]
和 [i+1][2*j+1]
下都有值。然后我只使用 ASCII 艺术打印出结果,计算出所需的偏移量和间距。这里的限制是您只能使用数字 0-9 以免搞砸我的树。你得自己想办法解决:)
哦,是的。输出如下所示(随意更改缺失值和空格的字符):
_______________________________________________________________6_______________________________________________________________
_______________________________0_______________________________________________________________7_______________________________
_______________N_______________________________3_______________________________N_______________________________8_______________
_______N_______________N_______________2_______________5_______________N_______________N_______________N_______________9_______
___N_______N_______N_______N_______1_______N_______4_______N_______N_______N_______N_______N_______N_______N_______N_______N___
_N___N___N___N___N___N___N___N___N___N___N___N___N___4___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N_
N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_3_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N
我使用 BFS 从树中提取了一个列表
解决方案= [[6], [0, 7], [None, 3, None, 8], [2, 5, None, 9], [1, None, 4, None, None, None], [None, None, None, None]]
如何将其打印为水平树?
我正在尝试使用
打印它def _print_tree(n=0,height,solution):
if n > height:
return
for j in range(len(solution[n])):
if solution[n][j]== None:
print(' ' * (2 ** (height - n + 1) - 1),end='')
else:
print(' ' * (2 ** (height - n + 1) - 1), end='')
print(' ',solution[n][j], end='')
但它给出了
我已经试过了,这是结果
nonechar = 'N'
spacechar = '_'
solution = [[6], [0, 7], [None, 3, None, 8], [2, 5, None, 9], [1, None, 4, None, None, None], [None, None, None, 4],[None, 3]]
for i in range(1, len(solution)):
for j in range(len(solution[i-1])):
if (solution[i-1][j] == None):
solution[i].insert(2*j, None)
solution[i].insert(2*j+1, None)
N = len(solution[-1]) * 2 - 1
offset = (N - 1) / 2
spacing = 0
for i in range(len(solution)):
line = spacechar * int(offset)
for j in range(len(solution[i])):
if (solution[i][j] == None):
line += nonechar
else:
line += str(solution[i][j])
if (j != len(solution[i]) - 1):
line += spacechar * int(spacing)
line += spacechar * int(offset)
print(line)
spacing = offset
offset = (offset - 1) / 2
我基本上做的是用缺失的数据填充解决方案列表,以便每个下一个子列表的值都是前一个的两倍。对于第 i
个子列表的每个第 j
个元素,在 [i+1][2*j]
和 [i+1][2*j+1]
下都有值。然后我只使用 ASCII 艺术打印出结果,计算出所需的偏移量和间距。这里的限制是您只能使用数字 0-9 以免搞砸我的树。你得自己想办法解决:)
哦,是的。输出如下所示(随意更改缺失值和空格的字符):
_______________________________________________________________6_______________________________________________________________
_______________________________0_______________________________________________________________7_______________________________
_______________N_______________________________3_______________________________N_______________________________8_______________
_______N_______________N_______________2_______________5_______________N_______________N_______________N_______________9_______
___N_______N_______N_______N_______1_______N_______4_______N_______N_______N_______N_______N_______N_______N_______N_______N___
_N___N___N___N___N___N___N___N___N___N___N___N___N___4___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N___N_
N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_3_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N_N