@@ -377,6 +377,7 @@ def hprint_tree(
377377 tree : T ,
378378 node_name_or_path : str = "" ,
379379 max_depth : int = 0 ,
380+ intermediate_node_name : bool = True ,
380381 style : str = "const" ,
381382 custom_style : Iterable [str ] = [],
382383) -> None :
@@ -457,12 +458,14 @@ def hprint_tree(
457458 tree (Node): tree to print
458459 node_name_or_path (str): node to print from, becomes the root node of printing
459460 max_depth (int): maximum depth of tree to print, based on `depth` attribute, optional
461+ intermediate_node_name (bool): indicator if intermediate nodes have node names, defaults to True
460462 style (str): style of print, defaults to const style
461463 custom_style (Iterable[str]): style of icons, used when `style` is set to 'custom'
462464 """
463465 result = hyield_tree (
464466 tree ,
465467 node_name_or_path = node_name_or_path ,
468+ intermediate_node_name = intermediate_node_name ,
466469 max_depth = max_depth ,
467470 style = style ,
468471 custom_style = custom_style ,
@@ -474,6 +477,7 @@ def hyield_tree(
474477 tree : T ,
475478 node_name_or_path : str = "" ,
476479 max_depth : int = 0 ,
480+ intermediate_node_name : bool = True ,
477481 style : str = "const" ,
478482 custom_style : Iterable [str ] = [],
479483) -> List [str ]:
@@ -555,6 +559,7 @@ def hyield_tree(
555559 tree (Node): tree to print
556560 node_name_or_path (str): node to print from, becomes the root node of printing
557561 max_depth (int): maximum depth of tree to print, based on `depth` attribute, optional
562+ intermediate_node_name (bool): indicator if intermediate nodes have node names, defaults to True
558563 style (str): style of print, defaults to const style
559564 custom_style (Iterable[str]): style of icons, used when `style` is set to 'custom'
560565
@@ -609,9 +614,10 @@ def hyield_tree(
609614
610615 # Calculate padding
611616 space = " "
612- padding_depths = {}
613- for _idx , _children in enumerate (levelordergroup_iter (tree )):
614- padding_depths [_idx + 1 ] = max ([len (node .node_name ) for node in _children ])
617+ padding_depths = collections .defaultdict (int )
618+ if intermediate_node_name :
619+ for _idx , _children in enumerate (levelordergroup_iter (tree )):
620+ padding_depths [_idx + 1 ] = max ([len (node .node_name ) for node in _children ])
615621
616622 def _hprint_branch (_node : T ) -> Tuple [List [str ], int ]:
617623 """Get string for tree horizontally.
@@ -623,17 +629,19 @@ def _hprint_branch(_node: T) -> Tuple[List[str], int]:
623629 Returns:
624630 (Tuple[List[str], int]): Intermediate/final result for node, index of branch
625631 """
626- padding_depth = padding_depths [_node .depth ]
627- padding = space * (padding_depth + 4 )
628- node_name_centered = _node .node_name .center (padding_depth )
632+ node_name_centered = _node .node_name .center (padding_depths [_node .depth ])
629633
630634 children = list (_node .children )
631635 if not len (children ):
632636 node_str = f"{ style_branch } { node_name_centered .rstrip ()} "
633637 return [node_str ], 0
634638
635639 result , result_nrow , result_idx = [], [], []
636- node_str = f"""{ style_branch } { node_name_centered } { style_branch } """
640+ if intermediate_node_name :
641+ node_str = f"""{ style_branch } { node_name_centered } { style_branch } """
642+ else :
643+ node_str = f"""{ style_branch } { style_branch } { style_branch } """
644+ padding = space * len (node_str )
637645 for idx , child in enumerate (children ):
638646 result_child , result_branch_idx = _hprint_branch (child )
639647 result .extend (result_child )
0 commit comments