@@ -79,14 +79,14 @@ <h2 class="section-title" id="header-functions">Functions</h2>
7979< div class ="desc "> < p > Calculates the depth (height) of a binary tree.</ p > </ div >
8080</ dd >
8181< dt id ="printBin.mapNodesToCodes "> < code class ="name flex ">
82- < span > def < span class ="ident "> mapNodesToCodes</ span > </ span > (< span > node, valueFillChar, unitSize, code='', memo=None )</ span >
82+ < span > def < span class ="ident "> mapNodesToCodes</ span > </ span > (< span > node, valueFillChar, unitSize, code='')</ span >
8383</ code > </ dt >
8484< dd >
8585< details class ="source ">
8686< summary >
8787< span > Expand source code</ span >
8888</ summary >
89- < pre > < code class ="python "> def mapNodesToCodes(node, valueFillChar, unitSize, code="", memo=None ):
89+ < pre > < code class ="python "> def mapNodesToCodes(node, valueFillChar, unitSize, code=""):
9090 """
9191 Recursively maps all nodes to their binary path codes.
9292
@@ -98,12 +98,15 @@ <h2 class="section-title" id="header-functions">Functions</h2>
9898 Returns:
9999 Dictionary mapping binary codes to centered node values
100100 """
101- if memo is None:
102- memo = {}
103- if node:
104- memo[code] = center(node.val, unitSize=unitSize, fillChar=valueFillChar)
105- mapNodesToCodes(node.left, valueFillChar, unitSize, code + "0", memo)
106- mapNodesToCodes(node.right, valueFillChar, unitSize, code + "1", memo)
101+ memo = {}
102+
103+ def recurse(node, code):
104+ if node:
105+ memo[code] = center(node.val, unitSize=unitSize, fillChar=valueFillChar)
106+ recurse(node.left, code + "0")
107+ recurse(node.right, code + "1")
108+
109+ recurse(node, code)
107110 return memo</ code > </ pre >
108111</ details >
109112< div class ="desc "> < p > Recursively maps all nodes to their binary path codes.</ p >
@@ -185,10 +188,28 @@ <h2 id="returns">Returns</h2>
185188 if prevValueIndexes is not None:
186189 for i in range(0, len(prevValueIndexes), 2):
187190 if i + 1 < len(prevValueIndexes):
188- parentCol = (prevValueIndexes[i] + prevValueIndexes[i + 1]) // 2
189- for col in range(prevValueIndexes[i] + 1, prevValueIndexes[i + 1]):
191+ leftChildCol = prevValueIndexes[i]
192+ rightChildCol = prevValueIndexes[i + 1]
193+ parentCol = (leftChildCol + rightChildCol) // 2
194+
195+ # Fill columns between children, except parent position
196+ for col in range(leftChildCol + 1, rightChildCol):
190197 if col != parentCol:
191198 mat[level][col] = center("", unitSize=unitSize, fillChar=connectorFillChar)
199+
200+ # Special handling for child positions if unitSize > 1
201+ if unitSize > 1 and connectors is not None:
202+ # Left child: fill chars positioned to the right of where / appears
203+ # The connector / is centered, so it appears at position unitSize // 2
204+ # We want fill chars after it
205+ leftFill = " " * (unitSize // 2 + 1) + connectorFillChar * (unitSize - unitSize // 2 - 1)
206+ mat[level][leftChildCol] = leftFill
207+
208+ # Right child: fill chars positioned to the left of where \ appears
209+ # The connector \ is centered, so it appears at position unitSize // 2
210+ # We want fill chars before it
211+ rightFill = connectorFillChar * (unitSize // 2) + " " * (unitSize - unitSize // 2)
212+ mat[level][rightChildCol] = rightFill
192213
193214 prevValueIndexes = valueIndexes
194215
@@ -243,7 +264,7 @@ <h2 id="args">Args</h2>
243264 """
244265 Converts a binary tree into a string representation for visualization.
245266
246- Args:Y
267+ Args:
247268 node: The root node of the tree to visualize
248269 depth: The depth of the tree (-1 for auto-calculation)
249270 valueFillChar: Character for padding node values (e.g., "_5_")
@@ -262,14 +283,23 @@ <h2 id="args">Args</h2>
262283 return "\n".join("".join(mat[i]) for i in range(0, len(mat), 2))</ code > </ pre >
263284</ details >
264285< div class ="desc "> < p > Converts a binary tree into a string representation for visualization.</ p >
265- < p > Args:Y
266- node: The root node of the tree to visualize
267- depth: The depth of the tree (-1 for auto-calculation)
268- valueFillChar: Character for padding node values (e.g., "< em > 5</ em > ")
269- connectorFillChar: Character for filling horizontal gaps between node pairs
270- unitSize: Size for centering values
271- removeEmpty: Whether to remove empty leading columns
272- connectors: Two-character string for connectors (e.g., "/" or "||"), None to skip connector rows</ p > </ div >
286+ < h2 id ="args "> Args</ h2 >
287+ < dl >
288+ < dt > < strong > < code > node</ code > </ strong > </ dt >
289+ < dd > The root node of the tree to visualize</ dd >
290+ < dt > < strong > < code > depth</ code > </ strong > </ dt >
291+ < dd > The depth of the tree (-1 for auto-calculation)</ dd >
292+ < dt > < strong > < code > valueFillChar</ code > </ strong > </ dt >
293+ < dd > Character for padding node values (e.g., "< em > 5</ em > ")</ dd >
294+ < dt > < strong > < code > connectorFillChar</ code > </ strong > </ dt >
295+ < dd > Character for filling horizontal gaps between node pairs</ dd >
296+ < dt > < strong > < code > unitSize</ code > </ strong > </ dt >
297+ < dd > Size for centering values</ dd >
298+ < dt > < strong > < code > removeEmpty</ code > </ strong > </ dt >
299+ < dd > Whether to remove empty leading columns</ dd >
300+ < dt > < strong > < code > connectors</ code > </ strong > </ dt >
301+ < dd > Two-character string for connectors (e.g., "/" or "||"), None to skip connector rows</ dd >
302+ </ dl > </ div >
273303</ dd >
274304</ dl >
275305</ section >
0 commit comments