Visualize a tree: Difference between revisions
m
→{{header|AppleScript}}: (tidying)
(→{{header|BBC BASIC}}: Restored the inadvertently deleted Batch File version) |
m (→{{header|AppleScript}}: (tidying)) |
||
Line 286:
-- └ Theta ┼── Kappa
-- └─ Lambda
-- TESTS --------------------------------------------------
on run
set tree to Node(1, ¬
Line 302 ⟶ 304:
Node("Zeta", {Node("Eta", {})}), ¬
Node("Theta", ¬
{Node("Iota", {}), Node("Kappa", {}),
set strTrees to unlines({"(NB – view in mono-spaced font)\n\n", ¬
Line 312 ⟶ 315:
drawTree2(false, true, tree2)})
set the clipboard to strTrees
end run
Line 338 ⟶ 341:
end |λ|
end script
set levelWidths to foldl(levelMax, {},
init(levels(measuredTree)))
-- Lefts, Mid, Rights
script lmrFromStrings
on |λ|(xs)
set {ls, rs} to items 2 thru -2 of
(splitAt((length of xs) div 2, xs) as list)
Tuple3(ls, item 1 of rs, rest of rs)
end |λ|
Line 379 ⟶ 384:
end script
end leftPad
on conS(x)▼
script▼
on |λ|(xs)▼
▲ x & xs
end |λ|▼
end script▼
-- lmrBuild main
Line 395 ⟶ 392:
set xs to nest of wsTree
set lng to length of xs
set {nChars, x} to items 2 thru -2 of
((root of wsTree) as list)
set _x to replicateString(w - nChars, "─") & x
-- LEAF NODE ------------------------------------
if 0 = lng then
Tuple3({}, _x, {})
else if 1 = lng then
-- NODE WITH SINGLE CHILD ---------------------
set indented to leftPad(1 + w)
script lineLinked
Line 407 ⟶ 409:
end script
|λ|(|λ|(item 1 of xs) of mf) of ¬
(|λ|(indented, lineLinked, indented) of
fghOverLMR)
else
-- NODE WITH CHILDREN -------------------------
script treeFix
on cFix(x)
script
▲ on |λ|(xs)
x & xs
end |λ|
▲ end script
on |λ|(l, m, r)
compose(stringsFromLMR, ¬
|λ|(
fghOverLMR)
end |λ|
end script
Line 442 ⟶ 455:
tell lmrFromStrings
set tupleLMR to |λ|(intercalate(sep, ¬
{|λ|(item 1 of lmrs) of
init(tail(lmrs))) & ¬
{|λ|(item -1 of lmrs) of ¬
(|λ|("│", "└", " ") of treeFix)}))
end tell
|λ|(tupleLMR) of
(|λ|(indented, linked, indented) of fghOverLMR)
end if
end |λ|
Line 454 ⟶ 471:
end script
set treeLines to |λ|(|λ|(measuredTree) of
foldr(lmrBuild, 0, levelWidths)) of stringsFromLMR
if blnPruned then
script notEmpty
Line 474 ⟶ 492:
-- GENERIC
-- Node :: a -> [Tree a] -> Tree a
|