简体   繁体   English

反向打印树级别

[英]Printing tree levels in reverse

I have a code where it prints the Huffman Tree. 我有一个代码在其中打印霍夫曼树。 It is this part: 这部分是:

while len(numArr) > 1:
    numArr = [numArr[0] + numArr[1]] + numArr[2:]
    numArr = sorted(numArr)
    valHold = numArr[0] * 8
    print(numArr)

Don't mind the valHold variables I use it to compute for the uncompressed bits of the input string. 不用介意valHold变量,我用它来计算输入字符串的未压缩位。

Let's say I have 1,1,1,2,3,4 as the elements of list numArr (the elements comes from a Counter and transferred to letter_ar r and numArr to separate the two). 假设我有1,1,1,2,3,4作为列表numArr的元素(这些元素来自Counter并转移到letter_ar r和numArr以便将两者分​​开)。

I can only print it like this: 我只能这样打印:

1,1,1,1,2,3,4
1,1,2,2,3,4
2,2,2,3,4
2,3,4,4
4,4,5
5,8
13

Is there a way I can print it the other way? 有没有其他方法可以打印? The way it will more look like a tree? 这样看起来会更像一棵树吗? Like this: 像这样:

13
5,8
4,4,5
2,3,4,4
2,2,2,3,4
1,1,2,2,3,4
1,1,1,1,2,3,4

It will be much better if you can teach me how to print it with indent: 如果您可以教我如何使用缩进来打印,那就更好了:

     13
     5,8
    4,4,5
   2,3,4,4
  2,2,2,3,4
 1,1,2,2,3,4
1,1,1,1,2,3,4

Please note that the elements of the numArr list is not predefined. 请注意, numArr列表的元素未预定义。 It is based on what the user inputs in the program. 它基于用户在程序中输入的内容。

Sure: 当然:

tree = []
while len(numArr) > 1:
    numArr = [numArr[0] + numArr[1]] + numArr[2:]
    numArr = sorted(numArr)
    valHold = numArr[0] * 8
    tree.append(numArr)

indent = len(tree)
for row in tree[::-1]:
    print(" " * indent, row)
    indent -= 1

You could output your data in a tree format as follows: 您可以按以下树状格式输出数据:

numArray = [
    [1, 2, 1, 4, 1, 1, 3],
    [2, 4, 1, 3, 2, 1],
    [2, 3, 2, 4, 2],
    [4, 2, 3, 4],
    [5, 4, 4],
    [8, 5],
    [13]]

output = [','.join(str(x) for x in sorted(row)) for row in numArray[::-1]]

for row in output:
    print row.center(len(output[-1]))

This would display: 这将显示:

      13     
     5,8     
    4,4,5    
   2,3,4,4   
  2,2,2,3,4  
 1,1,2,2,3,4 
1,1,1,1,2,3,4

[::-1] can be used to read an array in reverse order. [::-1]可用于以相反的顺序读取数组。 So the idea here is to read each row and convert each element into a string. 因此,这里的想法是读取每一行并将每个元素转换为字符串。 These are then joined using commas to create a list of numbers. 然后使用逗号将它们连接起来以创建数字列表。 Finally each row is then displayed centred based on the length of the longest entry. 最后,然后根据最长条目的长度将每行居中显示。

In order to print in reverse order, you can put it in a list first and later reverse it. 为了以相反的顺序打印,您可以先将其放在列表中,然后再反转。

array = []
while len(numArr) > 1:
    numArr = [numArr[0] + numArr[1]] + numArr[2:]
    numArr = sorted(numArr)
    array.append(numArr)
array.reverse()

To indent the output and align it with each number might need a little extra work, but you can try to center the output as a work-around. 要使输出缩进并使其与每个数字对齐可能需要一些额外的工作,但是您可以尝试将输出居中作为一种解决方法。 First convert each list to a string, and compute maximum width. 首先将每个列表转换为字符串,然后计算最大宽度。 Then use str.center to center the text. 然后使用str.center将文本居中。

array_str = list(map(lambda level: ','.join(str(i) for i in level), array))
width = max(len(s) for s in array_str)
for s in array_str:
    print(s.center(width))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM