22
33# Default configuration constants
44DEFAULT_UNIT_SIZE = 3
5- DEFAULT_FILL_CHAR = " "
5+ DEFAULT_FILL_CHAR = "_"
6+ DEFAULT_EMPTY_FILL_CHAR = "_"
67
78
89class BinaryNode :
@@ -79,7 +80,7 @@ def center(val, unitSize=None, fillChar=None):
7980 Args:
8081 val: The value to center
8182 unitSize: The total width of the output string (uses DEFAULT_UNIT_SIZE if None)
82- fillChar: The character to use for padding (uses DEFAULT_FILL_CHAR if None)
83+ fillChar: The character to use for padding (uses DEFAULT_FILL_CHAR "_" if None)
8384
8485 Returns:
8586 A centered string representation of val
@@ -117,7 +118,7 @@ def register(node: BinaryNode, fillChar=None, unitSize=None, code="", mem=None):
117118
118119 Args:
119120 node: The current node being processed
120- fillChar: Character used for padding (uses DEFAULT_FILL_CHAR if None)
121+ fillChar: Character used for padding node values (uses DEFAULT_FILL_CHAR "_" if None)
121122 unitSize: Size for centering values (uses DEFAULT_UNIT_SIZE if None)
122123 code: The binary path code for the current node
123124 mem: Dictionary mapping binary codes to centered node values
@@ -131,7 +132,7 @@ def register(node: BinaryNode, fillChar=None, unitSize=None, code="", mem=None):
131132 return mem
132133
133134
134- def nodeToMat (node : BinaryNode , depth = - 1 , fillChar = None , unitSize = None , removeEmpty = True ):
135+ def nodeToMat (node : BinaryNode , depth = - 1 , fillChar = None , emptyFillChar = None , unitSize = None , removeEmpty = True ):
135136 """
136137 Converts a binary tree into a 2D matrix representation for visualization.
137138
@@ -142,7 +143,8 @@ def nodeToMat(node: BinaryNode, depth=-1, fillChar=None, unitSize=None, removeEm
142143 Args:
143144 node: The root node of the tree to visualize
144145 depth: The depth of the tree (-1 for auto-calculation)
145- fillChar: Character for padding (uses DEFAULT_FILL_CHAR if None)
146+ fillChar: Character for padding node values (uses DEFAULT_FILL_CHAR "_" if None)
147+ emptyFillChar: Character for empty cells within valueIndexes (uses DEFAULT_EMPTY_FILL_CHAR "_" if None)
146148 unitSize: Size for centering (uses DEFAULT_UNIT_SIZE if None)
147149 removeEmpty: Whether to remove empty leading columns
148150
@@ -153,6 +155,8 @@ def nodeToMat(node: BinaryNode, depth=-1, fillChar=None, unitSize=None, removeEm
153155 unitSize = DEFAULT_UNIT_SIZE
154156 if fillChar is None :
155157 fillChar = DEFAULT_FILL_CHAR
158+ if emptyFillChar is None :
159+ emptyFillChar = DEFAULT_EMPTY_FILL_CHAR
156160
157161 if depth == - 1 :
158162 depth = getDepth (node )
@@ -161,7 +165,8 @@ def nodeToMat(node: BinaryNode, depth=-1, fillChar=None, unitSize=None, removeEm
161165 tree = register (node , fillChar = fillChar , unitSize = unitSize , code = "" , mem = {})
162166
163167 # Create matrix: (2*depth - 1) rows x (2^depth - 1) columns
164- mat = [[center ("" , unitSize = unitSize , fillChar = fillChar ) for _ in range (2 ** depth - 1 )] for _ in range (2 * depth - 1 )]
168+ # Initialize with space-centered empty cells
169+ mat = [[center ("" , unitSize = unitSize , fillChar = " " ) for _ in range (2 ** depth - 1 )] for _ in range (2 * depth - 1 )]
165170
166171 # Start with all even column indices (where values can be placed)
167172 valueIndexes = [i for i in range (2 ** depth - 1 ) if i % 2 == 0 ]
@@ -171,7 +176,7 @@ def nodeToMat(node: BinaryNode, depth=-1, fillChar=None, unitSize=None, removeEm
171176 # Odd levels: place connection characters (/ and \)
172177 if level % 2 != 0 :
173178 for i , index in enumerate (valueIndexes ):
174- mat [level ][index ] = [center ("/" , unitSize = unitSize , fillChar = fillChar ), center ("\\ " , unitSize = unitSize , fillChar = fillChar )][i % 2 ]
179+ mat [level ][index ] = [center ("/" , unitSize = unitSize , fillChar = " " ), center ("\\ " , unitSize = unitSize , fillChar = " " )][i % 2 ]
175180
176181 # Calculate parent positions (midpoints between child pairs)
177182 newIndexes = []
@@ -186,18 +191,19 @@ def nodeToMat(node: BinaryNode, depth=-1, fillChar=None, unitSize=None, removeEm
186191 codes = ["" .join (code ) for code in codes ]
187192
188193 for i , index in enumerate (valueIndexes ):
189- mat [level ][index ] = tree .get (codes [i ], center ("" , unitSize = unitSize , fillChar = fillChar ))
194+ mat [level ][index ] = tree .get (codes [i ], center ("" , unitSize = unitSize , fillChar = emptyFillChar ))
190195
191196 # Remove empty leading columns if requested
192197 if removeEmpty :
198+ centeredSpace = center ("" , unitSize = unitSize , fillChar = " " )
199+ centeredSlash = center ("/" , unitSize = unitSize , fillChar = " " )
200+ centeredBackslash = center ("\\ " , unitSize = unitSize , fillChar = " " )
201+
193202 for i in range (2 ** depth - 1 ):
194203 remove = False
195204 if all (
196- mat [j ][i ] in [
197- center ("" , unitSize = unitSize , fillChar = fillChar ),
198- center ("/" , unitSize = unitSize , fillChar = fillChar ),
199- center ("\\ " , unitSize = unitSize , fillChar = fillChar )
200- ] for j in range (2 * depth - 1 )
205+ mat [j ][i ] in [centeredSpace , centeredSlash , centeredBackslash ]
206+ for j in range (2 * depth - 1 )
201207 ):
202208 remove = True
203209 if not remove :
@@ -208,19 +214,20 @@ def nodeToMat(node: BinaryNode, depth=-1, fillChar=None, unitSize=None, removeEm
208214 return mat
209215
210216
211- def nodeToString (node : BinaryNode , depth = - 1 , fillChar = None , unitSize = None , removeEmpty = True ):
217+ def nodeToString (node : BinaryNode , depth = - 1 , fillChar = None , emptyFillChar = None , unitSize = None , removeEmpty = True ):
212218 """
213219 Converts a binary tree into a string representation for visualization.
214220
215221 Args:
216222 node: The root node of the tree to visualize
217223 depth: The depth of the tree (-1 for auto-calculation)
218- fillChar: Character for padding (uses DEFAULT_FILL_CHAR if None)
224+ fillChar: Character for padding node values (uses DEFAULT_FILL_CHAR if None)
225+ emptyFillChar: Character for empty cells within valueIndexes (uses DEFAULT_EMPTY_FILL_CHAR if None)
219226 unitSize: Size for centering (uses DEFAULT_UNIT_SIZE if None)
220227 removeEmpty: Whether to remove empty leading columns
221228
222229 Returns:
223230 A string representation of the tree with each row on a new line
224231 """
225- mat = nodeToMat (node , depth = depth , fillChar = fillChar , unitSize = unitSize , removeEmpty = removeEmpty )
232+ mat = nodeToMat (node , depth = depth , fillChar = fillChar , emptyFillChar = emptyFillChar , unitSize = unitSize , removeEmpty = removeEmpty )
226233 return "\n " .join ("" .join (row ) for row in mat )
0 commit comments