Printing BFS (Binary Tree) in Level Order with Specific Formatting

前端 未结 15 1374
情歌与酒
情歌与酒 2020-12-04 13:00

To begin with, this question is not a dup of this one, but builds on it.

Taking the tree in that question as an example,

    1 
   / \\
  2   3
 /            


        
15条回答
  •  渐次进展
    2020-12-04 13:40

    For those who are simply interested in visualizing binary trees (and not so much in the theory behind breadth-first search), there is a show function in the binarytree package. Applied to the example given in the question,

    from binarytree import Node, show
    
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.right.left = Node(5)
    root.right.right = Node(6)
    
    show(root)
    

    which prints

        1    
       / \   
      2   3  
     /   / \ 
    4   5   6
    

提交回复
热议问题