Skip to content

Commit 2ec4595

Browse files
committed
Removing memo as arg and cleaning display
1 parent 609580a commit 2ec4595

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

vicutils/printBin.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Default configuration constants
44
DEFAULT_UNIT_SIZE = 3
5-
DEFAULT_VALUE_FILL_CHAR = " " # Character used to pad node values (e.g., "_5_")
5+
DEFAULT_VALUE_FILL_CHAR = "_" # Character used to pad node values (e.g., "_5_")
66
DEFAULT_CONNECTOR_FILL_CHAR = "_" # Character used to fill horizontal gaps between node pairs
77

88

@@ -82,7 +82,7 @@ def getDepth(node: BinaryNode):
8282
return 1 + max(getDepth(node.left), getDepth(node.right))
8383

8484

85-
def mapNodesToCodes(node, valueFillChar, unitSize, code="", memo=None):
85+
def mapNodesToCodes(node, valueFillChar, unitSize, code=""):
8686
"""
8787
Recursively maps all nodes to their binary path codes.
8888
@@ -94,12 +94,15 @@ def mapNodesToCodes(node, valueFillChar, unitSize, code="", memo=None):
9494
Returns:
9595
Dictionary mapping binary codes to centered node values
9696
"""
97-
if memo is None:
98-
memo = {}
99-
if node:
100-
memo[code] = center(node.val, unitSize=unitSize, fillChar=valueFillChar)
101-
mapNodesToCodes(node.left, valueFillChar, unitSize, code + "0", memo)
102-
mapNodesToCodes(node.right, valueFillChar, unitSize, code + "1", memo)
97+
memo = {}
98+
99+
def recurse(node, code):
100+
if node:
101+
memo[code] = center(node.val, unitSize=unitSize, fillChar=valueFillChar)
102+
recurse(node.left, code + "0")
103+
recurse(node.right, code + "1")
104+
105+
recurse(node, code)
103106
return memo
104107

105108

@@ -166,10 +169,28 @@ def nodeToMat(node: BinaryNode, depth=-1, valueFillChar=None, connectorFillChar=
166169
if prevValueIndexes is not None:
167170
for i in range(0, len(prevValueIndexes), 2):
168171
if i + 1 < len(prevValueIndexes):
169-
parentCol = (prevValueIndexes[i] + prevValueIndexes[i + 1]) // 2
170-
for col in range(prevValueIndexes[i] + 1, prevValueIndexes[i + 1]):
172+
leftChildCol = prevValueIndexes[i]
173+
rightChildCol = prevValueIndexes[i + 1]
174+
parentCol = (leftChildCol + rightChildCol) // 2
175+
176+
# Fill columns between children, except parent position
177+
for col in range(leftChildCol + 1, rightChildCol):
171178
if col != parentCol:
172179
mat[level][col] = center("", unitSize=unitSize, fillChar=connectorFillChar)
180+
181+
# Special handling for child positions if unitSize > 1
182+
if unitSize > 1 and connectors is not None:
183+
# Left child: fill chars positioned to the right of where / appears
184+
# The connector / is centered, so it appears at position unitSize // 2
185+
# We want fill chars after it
186+
leftFill = " " * (unitSize // 2 + 1) + connectorFillChar * (unitSize - unitSize // 2 - 1)
187+
mat[level][leftChildCol] = leftFill
188+
189+
# Right child: fill chars positioned to the left of where \ appears
190+
# The connector \ is centered, so it appears at position unitSize // 2
191+
# We want fill chars before it
192+
rightFill = connectorFillChar * (unitSize // 2) + " " * (unitSize - unitSize // 2)
193+
mat[level][rightChildCol] = rightFill
173194

174195
prevValueIndexes = valueIndexes
175196

@@ -195,7 +216,7 @@ def nodeToString(node: BinaryNode, depth=-1, valueFillChar=None, connectorFillCh
195216
"""
196217
Converts a binary tree into a string representation for visualization.
197218
198-
Args:Y
219+
Args:
199220
node: The root node of the tree to visualize
200221
depth: The depth of the tree (-1 for auto-calculation)
201222
valueFillChar: Character for padding node values (e.g., "_5_")

0 commit comments

Comments
 (0)