MCPcopy
hub / github.com/joowani/binarytree / pprint

Method pprint

binarytree/__init__.py:630–676  ·  view source on GitHub ↗

Pretty-print the binary tree. :param index: If set to True (default: False), display level-order_ indexes using the format: ``{index}{delimiter}{value}``. :type index: bool :param delimiter: Delimiter character between the node index and the node valu

(self, index: bool = False, delimiter: str = "-")

Source from the content-addressed store, hash-verified

628 return digraph
629
630 def pprint(self, index: bool = False, delimiter: str = "-") -> None:
631 """Pretty-print the binary tree.
632
633 :param index: If set to True (default: False), display level-order_
634 indexes using the format: ``{index}{delimiter}{value}``.
635 :type index: bool
636 :param delimiter: Delimiter character between the node index and
637 the node value (default: '-').
638 :type delimiter: str
639
640 **Example**:
641
642 .. doctest::
643
644 >>> from binarytree import Node
645 >>>
646 >>> root = Node(1) # index: 0, value: 1
647 >>> root.left = Node(2) # index: 1, value: 2
648 >>> root.right = Node(3) # index: 2, value: 3
649 >>> root.left.right = Node(4) # index: 4, value: 4
650 >>>
651 >>> root.pprint()
652 <BLANKLINE>
653 __1
654 / \\
655 2 3
656 \\
657 4
658 <BLANKLINE>
659 >>> root.pprint(index=True) # Format: {index}-{value}
660 <BLANKLINE>
661 _____0-1_
662 / \\
663 1-2_ 2-3
664 \\
665 4-4
666 <BLANKLINE>
667
668 .. note::
669 If you do not need level-order_ indexes in the output string, use
670 :func:`binarytree.Node.__str__` instead.
671
672 .. _level-order:
673 https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search
674 """
675 lines = _build_tree_string(self, 0, index, delimiter)[0]
676 print("\n" + "\n".join((line.rstrip() for line in lines)))
677
678 def validate(self) -> None:
679 """Check if the binary tree is malformed.

Callers 2

pprint_defaultFunction · 0.80
pprint_with_indexFunction · 0.80

Calls 1

_build_tree_stringFunction · 0.85

Tested by

no test coverage detected