MCPcopy Index your code
hub / github.com/joowani/binarytree / __str__

Method __str__

binarytree/__init__.py:141–175  ·  view source on GitHub ↗

Return the pretty-print string for the binary tree. :return: Pretty-print string. :rtype: str **Example**: .. doctest:: >>> from binarytree import Node >>> >>> root = Node(1) >>> root.left = Node(2) >>> r

(self)

Source from the content-addressed store, hash-verified

139 return "Node({})".format(self.val)
140
141 def __str__(self) -> str:
142 """Return the pretty-print string for the binary tree.
143
144 :return: Pretty-print string.
145 :rtype: str
146
147 **Example**:
148
149 .. doctest::
150
151 >>> from binarytree import Node
152 >>>
153 >>> root = Node(1)
154 >>> root.left = Node(2)
155 >>> root.right = Node(3)
156 >>> root.left.right = Node(4)
157 >>>
158 >>> print(root)
159 <BLANKLINE>
160 __1
161 / \\
162 2 3
163 \\
164 4
165 <BLANKLINE>
166
167 .. note::
168 To include level-order_ indexes in the output string, use
169 :func:`binarytree.Node.pprint` instead.
170
171 .. _level-order:
172 https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search
173 """
174 lines = _build_tree_string(self, 0, False, "-")[0]
175 return "\n" + "\n".join((line.rstrip() for line in lines))
176
177 def __setattr__(self, attr: str, obj: Any) -> None:
178 """Modified version of ``__setattr__`` with extra sanity checking.

Callers

nothing calls this directly

Calls 1

_build_tree_stringFunction · 0.85

Tested by

no test coverage detected