Visualize a tree: Difference between revisions

m
(→‎{{header|BBC BASIC}}: Restored the inadvertently deleted Batch File version)
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", {}), Node("Lambda", {})})})¬
xNode("Lambda", & xs{})})})
set strTrees to unlines({"(NB – view in mono-spaced font)\n\n", ¬
Line 312 ⟶ 315:
drawTree2(false, true, tree2)})
set the clipboard to strTrees
log strTrees
end run
 
Line 338 ⟶ 341:
end |λ|
end script
set levelWidths to foldl(levelMax, {}, init(levels(measuredTree)))¬
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)¬
(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
end conS
-- 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)¬
((root of wsTree) as list)
set _x to replicateString(w - nChars, "─") & x
on conS(x)
-- LEAF NODE ------------------------------------
if 0 = lng then
Tuple3({}, _x, {})
script
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)¬
fghOverLMR)
else
-- NODE WITH CHILDREN -------------------------
script treeFix
on cFix(x)
script
on |λ|(xs)
x & xs
end |λ|
end script
end |λ|cFix
on |λ|(l, m, r)
compose(stringsFromLMR, ¬
|λ|(conScFix(l), conScFix(m), conScFix(r)) of fghOverLMR)¬
fghOverLMR)
end |λ|
end script
Line 442 ⟶ 455:
tell lmrFromStrings
set tupleLMR to |λ|(intercalate(sep, ¬
{|λ|(item 1 of lmrs) of (|λ|(" ", "┌", "│") of treeFix)} & ¬
map (|λ|(" ", "", "│") of treeFix, init(tail(lmrs)))} & ¬
{|λ|(item -1 of lmrs) of map(|λ|("│", "", " ") of treeFix)})), ¬
init(tail(lmrs))) & ¬
{|λ|(item -1 of lmrs) of ¬
(|λ|("│", "└", " ") of treeFix)}))
end tell
|λ|(tupleLMR) of (|λ|(indented, linked, indented) of fghOverLMR)¬
(|λ|(indented, linked, indented) of fghOverLMR)
end if
end |λ|
Line 454 ⟶ 471:
end script
set treeLines to |λ|(|λ|(measuredTree) of foldr(lmrBuild, 0, levelWidths)) of stringsFromLMR¬
foldr(lmrBuild, 0, levelWidths)) of stringsFromLMR
if blnPruned then
script notEmpty
Line 474 ⟶ 492:
 
 
-- GENERIC -----------------------------------------------------------
 
-- Node :: a -> [Tree a] -> Tree a
9,655

edits