@@ -619,19 +619,22 @@ def hyield_tree(
619619 for _idx , _children in enumerate (levelordergroup_iter (tree )):
620620 padding_depths [_idx + 1 ] = max ([len (node .node_name ) for node in _children ])
621621
622- def _hprint_branch (_node : T ) -> Tuple [List [str ], int ]:
622+ def _hprint_branch (_node : Union [ T , Node ], _cur_depth : int ) -> Tuple [List [str ], int ]:
623623 """Get string for tree horizontally.
624624 Recursively iterate the nodes in post-order traversal manner.
625625
626626 Args:
627627 _node (Node): node to get string
628+ _cur_depth (int): current depth of node
628629
629630 Returns:
630631 (Tuple[List[str], int]): Intermediate/final result for node, index of branch
631632 """
632- node_name_centered = _node .node_name .center (padding_depths [_node .depth ])
633+ if not _node :
634+ _node = Node (" " )
635+ node_name_centered = _node .node_name .center (padding_depths [_cur_depth ])
633636
634- children = list (_node .children )
637+ children = list (_node .children ) if any ( list ( _node . children )) else []
635638 if not len (children ):
636639 node_str = f"{ style_branch } { node_name_centered .rstrip ()} "
637640 return [node_str ], 0
@@ -643,7 +646,7 @@ def _hprint_branch(_node: T) -> Tuple[List[str], int]:
643646 node_str = f"""{ style_branch } { style_branch } { style_branch } """
644647 padding = space * len (node_str )
645648 for idx , child in enumerate (children ):
646- result_child , result_branch_idx = _hprint_branch (child )
649+ result_child , result_branch_idx = _hprint_branch (child , _cur_depth + 1 )
647650 result .extend (result_child )
648651 result_nrow .append (len (result_child ))
649652 result_idx .append (result_branch_idx )
@@ -712,7 +715,7 @@ def _hprint_branch(_node: T) -> Tuple[List[str], int]:
712715 result = [prefix + stem for prefix , stem in zip (result_prefix , result )]
713716 return result , mid
714717
715- result , _ = _hprint_branch (tree )
718+ result , _ = _hprint_branch (tree , 1 )
716719 return result
717720
718721
0 commit comments