A tree structure   (i.e. a rooted, connected acyclic graph)   is often used in programming.

It's often helpful to visually examine such a structure.

There are many ways to represent trees to a reader, such as:

   indented text   (à la unix  tree  command)
   nested HTML tables
   hierarchical GUI widgets
   2D   or   3D   images
   etc.
Task

Write a program to produce a visual representation of some tree.

The content of the tree doesn't matter, nor does the output format, the only requirement being that the output is human friendly.

Make do with the vague term "friendly" the best you can.




--
-- demo\rosetta\VisualiseTree.exw
--
with javascript_semantics
                        
-- To the theme tune of the Milk Tray Ad iyrt,
-- All because the Windows console hates utf8:
constant TL = '\#DA', -- aka '┌'
         VT = '\#B3', -- aka '│'
         BL = '\#C0', -- aka '└'
         HZ = '\#C4', -- aka '─'
         HS = "\#C4"  -- (string version of HZ)

function w1252_to_utf8(string s)
    if platform()!=WINDOWS then
        s = substitute_all(s,{ TL, VT, BL, HZ},
                             {"┌","│","└","─"})
    end if
    return s
end function
--</hates utf8>

procedure visualise_tree(object tree, string root=HS)
    if atom(tree) then
        puts(1,"<empty>\n")
    else
        object {v,l,r} = tree
        integer g = root[$]
        if sequence(l) then
            root[$] = iff(g=TL or g=HZ?' ':VT)
            visualise_tree(l,root&TL)
        end if
        root[$] = g
        printf(1,"%s%d\n",{w1252_to_utf8(root),v})
        if sequence(r) then
            root[$] = iff(g=TL?VT:' ')
            visualise_tree(r,root&BL)
        end if
    end if
end procedure

function rand_tree(integer low, integer high)
    for i=1 to 2 do
        integer v = rand(high-low+1)-1+low
        if v!=low
        and v!=high then
            return {v,rand_tree(low,v),rand_tree(v,high)}
        end if
    end for
    return 0
end function

object tree = rand_tree(0,20)   -- (can be 0, <1% chance)

visualise_tree(tree)
--pp(tree,{pp_Nest,10})
{} = wait_key()
