Tree visualization algorithm

前端 未结 4 824
清酒与你
清酒与你 2020-12-15 01:14

Is there some algorithm for tree data structure visualization? I tried googling, but couldnt find any. I\'m pretty sure there has to be some algorithm for this not that simp

4条回答
  •  半阙折子戏
    2020-12-15 02:13

    You can also print the tree left-to-right i.e. root at the very left, the first level right to that and so on. You'll find the tree printed with each level on it's own 'column'. The algorithm is somewhat like this:

    print(node, spaces):
        if node has left child:
            print(left_child, spaces + '    ')
        print spaces + node + '\n'
        if node has right child:
            print(right_child, spaces + '    ')
    

    This algorithm will print one tree node per line. Each level of the tree will be indented to the right by some spaces. This algorithm will print the items in ascending order, but the descending order may be achieved by processing the right child first.

提交回复
热议问题