Skip to content

Instantly share code, notes, and snippets.

@anthonyquizon
Last active April 12, 2025 09:39
Show Gist options
  • Save anthonyquizon/fd7201ff3e90f30933818cf5770bb291 to your computer and use it in GitHub Desktop.
Save anthonyquizon/fd7201ff3e90f30933818cf5770bb291 to your computer and use it in GitHub Desktop.
#
# Example of how to render an ascii tree from a parent vector
#
Draw⇐{
d←(⊢∾1+⊏⟜⥊)´⌽p←𝕩 # p: parent vector, d: depth vector
w←{∾⍟(1-˜≡)(𝕨(⊣⊔˜⊏)p)⊸(⊢∾¨⊏˜)⌾((𝕩∊𝕨⊏p)⊸/)𝕩}´⌽d⊔↕≠p # order depths depending on parent groups
n←(1+w⊏p)⊸∧˘(1+⌈´d)↑w=⌜w⊏d # matrix of ordered depth path cut off
n↩(0≠∨˝n)⊸/˘n↩>(↕≠)⊸({«⍟𝕨 𝕩}˘)n # shift columns under parent node and remove empty space
s←0<∊⊸∧˘n⋄e←0<(⌽∘∊∘⌽)⊸∧˘n⋄h←s+`∘-˘e⋄m←0<h∧n⋄v←s∧e # s: start, e: ends, h: horizontals, m: middles, v: verticals
b←" ─┬├┐│"⊏˜(0⥊˜≠⍉n)∾1↓⌈´h‿m‿s‿e‿vר1+↕5 # branches - nullify root layer. 1: horizontal, 2: middle, 3: start, 4: end, 5: vertical,
o←∾˘(0⊸≢)◶" "‿"·"¨n # convert nodes to characters
•Out˘b{𝕨∾(@+10)∾𝕩}˘o # combine branches and nodes and print
}
Draw 0‿0‿1
Draw 0‿0‿0‿0
Draw 0‿0‿1‿0
Draw 0‿0‿1‿1‿0
Draw 0‿0‿1‿0‿1
Draw 0‿0‿0‿2‿2‿2
Draw 0‿0‿0‿2‿2‿2‿0
Draw 0‿0‿0‿2‿2‿2‿0‿0
Draw 0‿0‿1‿2‿0‿2‿0‿4‿2‿2‿4
Draw 0‿0‿1‿2‿0‿1‿0‿4‿1
Draw 0‿0‿1‿2‿3‿3‿3
Draw 0‿0‿1‿2‿3‿4‿4‿4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment