@@ -115,20 +115,20 @@ <h2 id="returns">Returns</h2>
115115< p > Dictionary mapping binary codes to centered node values</ p > </ div >
116116</ dd >
117117< dt id ="printBin.nodeToMat "> < code class ="name flex ">
118- < span > def < span class ="ident "> nodeToMat</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > depth=-1,< br > valueFillChar=None,< br > connectorFillChar=None,< br > unitSize=None,< br > removeEmpty=True)</ span >
118+ < span > def < span class ="ident "> nodeToMat</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > depth=-1,< br > valueFillChar=None,< br > connectorFillChar=None,< br > unitSize=None,< br > removeEmpty=True, < br > connectors='/\\' )</ span >
119119</ code > </ dt >
120120< dd >
121121< details class ="source ">
122122< summary >
123123< span > Expand source code</ span >
124124</ summary >
125- < pre > < code class ="python "> def nodeToMat(node: BinaryNode, depth=-1, valueFillChar=None, connectorFillChar=None, unitSize=None, removeEmpty=True):
125+ < pre > < code class ="python "> def nodeToMat(node: BinaryNode, depth=-1, valueFillChar=None, connectorFillChar=None, unitSize=None, removeEmpty=True, connectors="/\\" ):
126126 """
127127 Converts a binary tree into a 2D matrix representation for visualization.
128128
129129 The matrix includes:
130130 - Even rows (0, 2, 4...): Node values
131- - Odd rows (1, 3, 5...): Connection lines (/ and \\ )
131+ - Odd rows (1, 3, 5...): Connection lines (using connectors )
132132
133133 Args:
134134 node: The root node of the tree to visualize
@@ -137,6 +137,7 @@ <h2 id="returns">Returns</h2>
137137 connectorFillChar: Character for filling horizontal gaps between node pairs
138138 unitSize: Size for centering values
139139 removeEmpty: Whether to remove empty leading columns
140+ connectors: Two-character string for connectors (e.g., "/\\" or "||"), None to skip connector rows
140141 """
141142 if unitSize is None:
142143 unitSize = DEFAULT_UNIT_SIZE
@@ -145,66 +146,56 @@ <h2 id="returns">Returns</h2>
145146 if connectorFillChar is None:
146147 connectorFillChar = DEFAULT_CONNECTOR_FILL_CHAR
147148
149+ if connectors is not None and len(connectors) != 2:
150+ connectors = "/\\"
151+
148152 if depth == -1:
149153 depth = getDepth(node)
150154
151- # Map all nodes to their binary path codes
152155 tree = mapNodesToCodes(node, valueFillChar, unitSize)
153156
154- # Cache frequently used values
155- numCols = 2 ** depth - 1 # Number of leaf positions (columns)
156- numRows = 2 * depth - 1 # Total rows (values + connectors)
157+ numCols = 2 ** depth - 1
158+ numRows = 2 * depth - 1
157159
158- # Initialize matrix with space-centered empty cells
159160 mat = [[center("", unitSize=unitSize, fillChar=" ") for _ in range(numCols)] for _ in range(numRows)]
160161
161- # Start with all even column indices (where values can be placed)
162162 valueIndexes = [i for i in range(numCols) if i % 2 == 0]
163163 prevValueIndexes = None
164164
165- # Build matrix from bottom to top
166165 for level in range(numRows - 1, -1, -1):
167- # Odd levels: place connection characters (/ and \)
168166 if level % 2 != 0:
169- for i, index in enumerate(valueIndexes):
170- mat[level][index] = [center("/", unitSize=unitSize, fillChar=" "),
171- center("\\", unitSize=unitSize, fillChar=" ")][i % 2]
167+ if connectors is not None:
168+ for i, index in enumerate(valueIndexes):
169+ mat[level][index] = [center(connectors[0], unitSize=unitSize, fillChar=" "),
170+ center(connectors[1], unitSize=unitSize, fillChar=" ")][i % 2]
172171
173- # Calculate parent positions (midpoints between child pairs)
174172 nextValueIndexes = []
175173 for i in range(0, len(valueIndexes) - 1, 2):
176174 nextValueIndexes.append((valueIndexes[i] + valueIndexes[i + 1]) // 2)
177175 valueIndexes = nextValueIndexes
178176 continue
179177
180- # Even levels: place node values
181- # Generate all binary codes for current level
182178 codes = list(product(*["01" for _ in range(level // 2)]))
183179 codes = ["".join(code) for code in codes]
184180
185181 for i, index in enumerate(valueIndexes):
186182 if codes[i] in tree:
187183 mat[level][index] = tree[codes[i]]
188184
189- # Fill horizontal gaps between node pairs using connectorFillChar
190185 if prevValueIndexes is not None:
191186 for i in range(0, len(prevValueIndexes), 2):
192187 if i + 1 < len(prevValueIndexes):
193- # Calculate parent position (should not be overwritten)
194188 parentCol = (prevValueIndexes[i] + prevValueIndexes[i + 1]) // 2
195- # Fill columns between children, except parent position
196189 for col in range(prevValueIndexes[i] + 1, prevValueIndexes[i + 1]):
197190 if col != parentCol:
198191 mat[level][col] = center("", unitSize=unitSize, fillChar=connectorFillChar)
199192
200- # Save current valueIndexes for next even level
201193 prevValueIndexes = valueIndexes
202194
203- # Remove empty leading columns if requested
204195 if removeEmpty:
205196 centeredSpace = center("", unitSize=unitSize, fillChar=" ")
206- centeredSlash = center("/", unitSize=unitSize, fillChar=" ")
207- centeredBackslash = center("\\", unitSize=unitSize, fillChar=" ")
197+ centeredSlash = center(connectors[0] if connectors else "/", unitSize=unitSize, fillChar=" ")
198+ centeredBackslash = center(connectors[1] if connectors else "\\", unitSize=unitSize, fillChar=" ")
208199
209200 for i in range(numCols):
210201 remove = all(
@@ -221,7 +212,7 @@ <h2 id="returns">Returns</h2>
221212< div class ="desc "> < p > Converts a binary tree into a 2D matrix representation for visualization.</ p >
222213< p > The matrix includes:
223214- Even rows (0, 2, 4…): Node values
224- - Odd rows (1, 3, 5…): Connection lines (/ and )</ p >
215+ - Odd rows (1, 3, 5…): Connection lines (using connectors )</ p >
225216< h2 id ="args "> Args</ h2 >
226217< dl >
227218< dt > < strong > < code > node</ code > </ strong > </ dt >
@@ -236,17 +227,19 @@ <h2 id="args">Args</h2>
236227< dd > Size for centering values</ dd >
237228< dt > < strong > < code > removeEmpty</ code > </ strong > </ dt >
238229< dd > Whether to remove empty leading columns</ dd >
230+ < dt > < strong > < code > connectors</ code > </ strong > </ dt >
231+ < dd > Two-character string for connectors (e.g., "/" or "||"), None to skip connector rows</ dd >
239232</ dl > </ div >
240233</ dd >
241234< dt id ="printBin.nodeToString "> < code class ="name flex ">
242- < span > def < span class ="ident "> nodeToString</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > depth=-1,< br > valueFillChar=None,< br > connectorFillChar=None,< br > unitSize=None,< br > removeEmpty=True,< br > showConnectors=True )</ span >
235+ < span > def < span class ="ident "> nodeToString</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > depth=-1,< br > valueFillChar=None,< br > connectorFillChar=None,< br > unitSize=None,< br > removeEmpty=True,< br > connectors='/\\' )</ span >
243236</ code > </ dt >
244237< dd >
245238< details class ="source ">
246239< summary >
247240< span > Expand source code</ span >
248241</ summary >
249- < pre > < code class ="python "> def nodeToString(node: BinaryNode, depth=-1, valueFillChar=None, connectorFillChar=None, unitSize=None, removeEmpty=True, showConnectors=True ):
242+ < pre > < code class ="python "> def nodeToString(node: BinaryNode, depth=-1, valueFillChar=None, connectorFillChar=None, unitSize=None, removeEmpty=True, connectors="/\\" ):
250243 """
251244 Converts a binary tree into a string representation for visualization.
252245
@@ -257,15 +250,15 @@ <h2 id="args">Args</h2>
257250 connectorFillChar: Character for filling horizontal gaps between node pairs
258251 unitSize: Size for centering values
259252 removeEmpty: Whether to remove empty leading columns
260- showConnectors: Whether to show connector lines (/ and \) between nodes
253+ connectors: Two-character string for connectors (e.g., "/\\" or "||"), None to skip connector rows
261254 """
262255 mat = nodeToMat(node, depth=depth, valueFillChar=valueFillChar,
263- connectorFillChar=connectorFillChar, unitSize=unitSize, removeEmpty=removeEmpty)
256+ connectorFillChar=connectorFillChar, unitSize=unitSize,
257+ removeEmpty=removeEmpty, connectors=connectors)
264258
265- if showConnectors :
259+ if connectors is not None :
266260 return "\n".join("".join(row) for row in mat)
267261 else:
268- # Only include even rows (node values, skip odd rows with connectors)
269262 return "\n".join("".join(mat[i]) for i in range(0, len(mat), 2))</ code > </ pre >
270263</ details >
271264< div class ="desc "> < p > Converts a binary tree into a string representation for visualization.</ p >
@@ -283,8 +276,8 @@ <h2 id="args">Args</h2>
283276< dd > Size for centering values</ dd >
284277< dt > < strong > < code > removeEmpty</ code > </ strong > </ dt >
285278< dd > Whether to remove empty leading columns</ dd >
286- < dt > < strong > < code > showConnectors </ code > </ strong > </ dt >
287- < dd > Whether to show connector lines (/ and ) between nodes </ dd >
279+ < dt > < strong > < code > connectors </ code > </ strong > </ dt >
280+ < dd > Two-character string for connectors (e.g., "/" or "||"), None to skip connector rows </ dd >
288281</ dl > </ div >
289282</ dd >
290283</ dl >
@@ -326,7 +319,6 @@ <h2 class="section-title" id="header-classes">Classes</h2>
326319 >>> # 4 5 7
327320 """
328321 def __init__(self, val=0, left=None, right=None):
329- # If val is a list, build tree from it
330322 if isinstance(val, list):
331323 if not val or val[0] is None:
332324 raise ValueError("Cannot create tree from empty list or list starting with None")
@@ -341,13 +333,11 @@ <h2 class="section-title" id="header-classes">Classes</h2>
341333 while queue and i < len(val):
342334 node = queue.pop(0)
343335
344- # Add left child
345336 if i < len(val) and val[i] is not None:
346337 node.left = BinaryNode(val[i])
347338 queue.append(node.left)
348339 i += 1
349340
350- # Add right child
351341 if i < len(val) and val[i] is not None:
352342 node.right = BinaryNode(val[i])
353343 queue.append(node.right)
0 commit comments