Rohit Ahuja
Rohit Ahuja

Reputation: 47

Print a Pretty Tree

I have extracted a list from a tree using BFS

solution= [[6], [0, 7], [None, 3, None, 8], [2, 5, None, 9], [1, None, 4, None, None, None], [None, None, None, None]]

How do i print it as a horizontal tree?

Desired Output

I was trying to print it using

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='')

But it gives

Output

Upvotes: 0

Views: 226

Answers (1)

Anton Egorov
Anton Egorov

Reputation: 1374

I've played around and here is the result

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

What I basically did is fill the solution list with the missing data so that each next sub-list has two times more values than the previous one. For each j-th element of the i-th sub-list there are values under [i+1][2*j] and [i+1][2*j+1]. Then I just print out the result using ASCII art, having calculated required offsets and spacing. The limitations here are that you can only use digits 0-9 in order not to screw up my tree. You'll have to figure out the way to solve it on your own :)

Oh, yeah. The output looks like this (feel free to change characters for missing values and spaces):

_______________________________________________________________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

Upvotes: 1

Related Questions