@@ -59,15 +59,15 @@ <h2 class="section-title" id="header-functions">Functions</h2>
5959 Args:
6060 val: The value to center
6161 unitSize: The total width of the output string (uses DEFAULT_UNIT_SIZE if None)
62- fillChar: The character to use for padding (uses DEFAULT_FILL_CHAR "_" if None)
62+ fillChar: The character to use for padding (uses DEFAULT_VALUE_FILL_CHAR "_" if None)
6363
6464 Returns:
6565 A centered string representation of val
6666 """
6767 if unitSize is None:
6868 unitSize = DEFAULT_UNIT_SIZE
6969 if fillChar is None:
70- fillChar = DEFAULT_FILL_CHAR
70+ fillChar = DEFAULT_VALUE_FILL_CHAR
7171 return str(val).center(unitSize, fillChar)</ code > </ pre >
7272</ details >
7373< div class ="desc "> < p > Centers a value within a fixed width string.</ p >
@@ -78,7 +78,7 @@ <h2 id="args">Args</h2>
7878< dt > < strong > < code > unitSize</ code > </ strong > </ dt >
7979< dd > The total width of the output string (uses DEFAULT_UNIT_SIZE if None)</ dd >
8080< dt > < strong > < code > fillChar</ code > </ strong > </ dt >
81- < dd > The character to use for padding (uses DEFAULT_FILL_CHAR "_" if None)</ dd >
81+ < dd > The character to use for padding (uses DEFAULT_VALUE_FILL_CHAR "_" if None)</ dd >
8282</ dl >
8383< h2 id ="returns "> Returns</ h2 >
8484< p > A centered string representation of val</ p > </ div >
@@ -115,14 +115,14 @@ <h2 id="returns">Returns</h2>
115115< p > The depth of the tree (number of levels from root to deepest leaf)</ 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 > fillChar =None,< br > emptyFillChar =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 > gapFillChar =None,< br > unitSize=None,< br > removeEmpty=True)</ 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, fillChar =None, emptyFillChar =None, unitSize=None, removeEmpty=True):
125+ < pre > < code class ="python "> def nodeToMat(node: BinaryNode, depth=-1, valueFillChar =None, gapFillChar =None, unitSize=None, removeEmpty=True):
126126 """
127127 Converts a binary tree into a 2D matrix representation for visualization.
128128
@@ -133,8 +133,8 @@ <h2 id="returns">Returns</h2>
133133 Args:
134134 node: The root node of the tree to visualize
135135 depth: The depth of the tree (-1 for auto-calculation)
136- fillChar : Character for padding node values (uses DEFAULT_FILL_CHAR "_" if None)
137- emptyFillChar : Character for empty cells within valueIndexes (uses DEFAULT_EMPTY_FILL_CHAR "_" if None)
136+ valueFillChar : Character for padding node values (uses DEFAULT_VALUE_FILL_CHAR "_" if None)
137+ gapFillChar : Character for filling gaps between pairs (uses DEFAULT_GAP_FILL_CHAR "_" if None)
138138 unitSize: Size for centering (uses DEFAULT_UNIT_SIZE if None)
139139 removeEmpty: Whether to remove empty leading columns
140140
@@ -143,23 +143,24 @@ <h2 id="returns">Returns</h2>
143143 """
144144 if unitSize is None:
145145 unitSize = DEFAULT_UNIT_SIZE
146- if fillChar is None:
147- fillChar = DEFAULT_FILL_CHAR
148- if emptyFillChar is None:
149- emptyFillChar = DEFAULT_EMPTY_FILL_CHAR
146+ if valueFillChar is None:
147+ valueFillChar = DEFAULT_VALUE_FILL_CHAR
148+ if gapFillChar is None:
149+ gapFillChar = DEFAULT_GAP_FILL_CHAR
150150
151151 if depth == -1:
152152 depth = getDepth(node)
153153
154154 # Register all nodes with their binary path codes
155- tree = register(node, fillChar=fillChar , unitSize=unitSize, code="", mem={})
155+ tree = register(node, valueFillChar=valueFillChar , unitSize=unitSize, code="", mem={})
156156
157157 # Create matrix: (2*depth - 1) rows x (2^depth - 1) columns
158158 # Initialize with space-centered empty cells
159159 mat = [[center("", unitSize=unitSize, fillChar=" ") for _ in range(2 ** depth - 1)] for _ in range(2 * depth - 1)]
160160
161161 # Start with all even column indices (where values can be placed)
162162 valueIndexes = [i for i in range(2 ** depth - 1) if i % 2 == 0]
163+ prev = None
163164
164165 # Build matrix from bottom to top
165166 for level in range(2 * (depth - 1), -1, -1):
@@ -168,11 +169,20 @@ <h2 id="returns">Returns</h2>
168169 for i, index in enumerate(valueIndexes):
169170 mat[level][index] = [center("/", unitSize=unitSize, fillChar=" "), center("\\", unitSize=unitSize, fillChar=" ")][i % 2]
170171
172+ # Fill gaps between pairs in previous level
173+ if prev is not None:
174+ for i in range(0, len(prev), 2):
175+ if i + 1 < len(prev):
176+ # Fill columns between prev[i] and prev[i+1]
177+ for col in range(prev[i] + 1, prev[i + 1]):
178+ mat[level + 1][col] = center("", unitSize=unitSize, fillChar=gapFillChar)
179+
171180 # Calculate parent positions (midpoints between child pairs)
172- newIndexes = []
181+ next = []
173182 for i in range(0, len(valueIndexes) - 1, 2):
174- newIndexes.append((valueIndexes[i] + valueIndexes[i + 1]) // 2)
175- valueIndexes = newIndexes
183+ next.append((valueIndexes[i] + valueIndexes[i + 1]) // 2)
184+ prev = valueIndexes
185+ valueIndexes = next
176186 continue
177187
178188 # Even levels: place node values
@@ -181,7 +191,15 @@ <h2 id="returns">Returns</h2>
181191 codes = ["".join(code) for code in codes]
182192
183193 for i, index in enumerate(valueIndexes):
184- mat[level][index] = tree.get(codes[i], center("", unitSize=unitSize, fillChar=emptyFillChar))
194+ if codes[i] in tree:
195+ mat[level][index] = tree[codes[i]]
196+
197+ # Fill gaps for the last (bottom) level
198+ if prev is not None:
199+ for i in range(0, len(prev), 2):
200+ if i + 1 < len(prev):
201+ for col in range(prev[i] + 1, prev[i + 1]):
202+ mat[2 * (depth - 1)][col] = center("", unitSize=unitSize, fillChar=gapFillChar)
185203
186204 # Remove empty leading columns if requested
187205 if removeEmpty:
@@ -213,10 +231,10 @@ <h2 id="args">Args</h2>
213231< dd > The root node of the tree to visualize</ dd >
214232< dt > < strong > < code > depth</ code > </ strong > </ dt >
215233< dd > The depth of the tree (-1 for auto-calculation)</ dd >
216- < dt > < strong > < code > fillChar </ code > </ strong > </ dt >
217- < dd > Character for padding node values (uses DEFAULT_FILL_CHAR "_" if None)</ dd >
218- < dt > < strong > < code > emptyFillChar </ code > </ strong > </ dt >
219- < dd > Character for empty cells within valueIndexes (uses DEFAULT_EMPTY_FILL_CHAR "_" if None)</ dd >
234+ < dt > < strong > < code > valueFillChar </ code > </ strong > </ dt >
235+ < dd > Character for padding node values (uses DEFAULT_VALUE_FILL_CHAR "_" if None)</ dd >
236+ < dt > < strong > < code > gapFillChar </ code > </ strong > </ dt >
237+ < dd > Character for filling gaps between pairs (uses DEFAULT_GAP_FILL_CHAR "_" if None)</ dd >
220238< dt > < strong > < code > unitSize</ code > </ strong > </ dt >
221239< dd > Size for centering (uses DEFAULT_UNIT_SIZE if None)</ dd >
222240< dt > < strong > < code > removeEmpty</ code > </ strong > </ dt >
@@ -226,29 +244,29 @@ <h2 id="returns">Returns</h2>
226244< p > A 2D list (matrix) representing the tree structure</ p > </ div >
227245</ dd >
228246< dt id ="printBin.nodeToString "> < code class ="name flex ">
229- < span > def < span class ="ident "> nodeToString</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > depth=-1,< br > fillChar =None,< br > emptyFillChar =None,< br > unitSize=None,< br > removeEmpty=True)</ span >
247+ < 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 > gapFillChar =None,< br > unitSize=None,< br > removeEmpty=True)</ span >
230248</ code > </ dt >
231249< dd >
232250< details class ="source ">
233251< summary >
234252< span > Expand source code</ span >
235253</ summary >
236- < pre > < code class ="python "> def nodeToString(node: BinaryNode, depth=-1, fillChar =None, emptyFillChar =None, unitSize=None, removeEmpty=True):
254+ < pre > < code class ="python "> def nodeToString(node: BinaryNode, depth=-1, valueFillChar =None, gapFillChar =None, unitSize=None, removeEmpty=True):
237255 """
238256 Converts a binary tree into a string representation for visualization.
239257
240258 Args:
241259 node: The root node of the tree to visualize
242260 depth: The depth of the tree (-1 for auto-calculation)
243- fillChar : Character for padding node values (uses DEFAULT_FILL_CHAR if None)
244- emptyFillChar : Character for empty cells within valueIndexes (uses DEFAULT_EMPTY_FILL_CHAR if None)
261+ valueFillChar : Character for padding node values (uses DEFAULT_VALUE_FILL_CHAR "_" if None)
262+ gapFillChar : Character for filling gaps between pairs (uses DEFAULT_GAP_FILL_CHAR "_" if None)
245263 unitSize: Size for centering (uses DEFAULT_UNIT_SIZE if None)
246264 removeEmpty: Whether to remove empty leading columns
247265
248266 Returns:
249267 A string representation of the tree with each row on a new line
250268 """
251- mat = nodeToMat(node, depth=depth, fillChar=fillChar, emptyFillChar=emptyFillChar , unitSize=unitSize, removeEmpty=removeEmpty)
269+ mat = nodeToMat(node, depth=depth, valueFillChar=valueFillChar, gapFillChar=gapFillChar , unitSize=unitSize, removeEmpty=removeEmpty)
252270 return "\n".join("".join(row) for row in mat)</ code > </ pre >
253271</ details >
254272< div class ="desc "> < p > Converts a binary tree into a string representation for visualization.</ p >
@@ -258,10 +276,10 @@ <h2 id="args">Args</h2>
258276< dd > The root node of the tree to visualize</ dd >
259277< dt > < strong > < code > depth</ code > </ strong > </ dt >
260278< dd > The depth of the tree (-1 for auto-calculation)</ dd >
261- < dt > < strong > < code > fillChar </ code > </ strong > </ dt >
262- < dd > Character for padding node values (uses DEFAULT_FILL_CHAR if None)</ dd >
263- < dt > < strong > < code > emptyFillChar </ code > </ strong > </ dt >
264- < dd > Character for empty cells within valueIndexes (uses DEFAULT_EMPTY_FILL_CHAR if None)</ dd >
279+ < dt > < strong > < code > valueFillChar </ code > </ strong > </ dt >
280+ < dd > Character for padding node values (uses DEFAULT_VALUE_FILL_CHAR "_" if None)</ dd >
281+ < dt > < strong > < code > gapFillChar </ code > </ strong > </ dt >
282+ < dd > Character for filling gaps between pairs (uses DEFAULT_GAP_FILL_CHAR "_" if None)</ dd >
265283< dt > < strong > < code > unitSize</ code > </ strong > </ dt >
266284< dd > Size for centering (uses DEFAULT_UNIT_SIZE if None)</ dd >
267285< dt > < strong > < code > removeEmpty</ code > </ strong > </ dt >
@@ -271,14 +289,14 @@ <h2 id="returns">Returns</h2>
271289< p > A string representation of the tree with each row on a new line</ p > </ div >
272290</ dd >
273291< dt id ="printBin.register "> < code class ="name flex ">
274- < span > def < span class ="ident "> register</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > fillChar =None,< br > unitSize=None,< br > code='',< br > mem=None)</ span >
292+ < span > def < span class ="ident "> register</ span > </ span > (< span > node: < a title ="printBin.BinaryNode " href ="#printBin.BinaryNode "> BinaryNode</ a > ,< br > valueFillChar =None,< br > unitSize=None,< br > code='',< br > mem=None)</ span >
275293</ code > </ dt >
276294< dd >
277295< details class ="source ">
278296< summary >
279297< span > Expand source code</ span >
280298</ summary >
281- < pre > < code class ="python "> def register(node: BinaryNode, fillChar =None, unitSize=None, code="", mem=None):
299+ < pre > < code class ="python "> def register(node: BinaryNode, valueFillChar =None, unitSize=None, code="", mem=None):
282300 """
283301 Recursively registers all nodes in a tree with their binary path codes.
284302
@@ -289,17 +307,17 @@ <h2 id="returns">Returns</h2>
289307
290308 Args:
291309 node: The current node being processed
292- fillChar : Character used for padding node values (uses DEFAULT_FILL_CHAR "_" if None)
310+ valueFillChar : Character used for padding node values (uses DEFAULT_VALUE_FILL_CHAR "_" if None)
293311 unitSize: Size for centering values (uses DEFAULT_UNIT_SIZE if None)
294312 code: The binary path code for the current node
295313 mem: Dictionary mapping binary codes to centered node values
296314 """
297315 if mem is None:
298316 mem = {}
299317 if node:
300- mem[code] = center(node.val, unitSize=unitSize, fillChar=fillChar )
301- register(node.left, fillChar=fillChar , unitSize=unitSize, code=code + "0", mem=mem)
302- register(node.right, fillChar=fillChar , unitSize=unitSize, code=code + "1", mem=mem)
318+ mem[code] = center(node.val, unitSize=unitSize, fillChar=valueFillChar )
319+ register(node.left, valueFillChar=valueFillChar , unitSize=unitSize, code=code + "0", mem=mem)
320+ register(node.right, valueFillChar=valueFillChar , unitSize=unitSize, code=code + "1", mem=mem)
303321 return mem</ code > </ pre >
304322</ details >
305323< div class ="desc "> < p > Recursively registers all nodes in a tree with their binary path codes.</ p >
@@ -311,8 +329,8 @@ <h2 id="args">Args</h2>
311329< dl >
312330< dt > < strong > < code > node</ code > </ strong > </ dt >
313331< dd > The current node being processed</ dd >
314- < dt > < strong > < code > fillChar </ code > </ strong > </ dt >
315- < dd > Character used for padding node values (uses DEFAULT_FILL_CHAR "_" if None)</ dd >
332+ < dt > < strong > < code > valueFillChar </ code > </ strong > </ dt >
333+ < dd > Character used for padding node values (uses DEFAULT_VALUE_FILL_CHAR "_" if None)</ dd >
316334< dt > < strong > < code > unitSize</ code > </ strong > </ dt >
317335< dd > Size for centering values (uses DEFAULT_UNIT_SIZE if None)</ dd >
318336< dt > < strong > < code > code</ code > </ strong > </ dt >
0 commit comments