RosettaCodeData/Task/Visualize-a-tree/Phix/visualize-a-tree-1.phix

41 lines
1022 B
Plaintext

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)
constant Horizontal = #C4,
Horizontals = "\#C4",
TopLeft = #DA,
Vertical = #B3,
BtmLeft = #C0
procedure visualise_tree(object tree, string root=Horizontals)
if atom(tree) then
puts(1,"<empty>\n")
else
object {v,l,r} = tree
integer g = root[$]
if sequence(l) then
root[$] = iff(g=TopLeft or g=Horizontal?' ':Vertical)
visualise_tree(l,root&TopLeft)
end if
root[$] = g
puts(1,root)
?v
if sequence(r) then
root[$] = iff(g=TopLeft?Vertical:' ')
visualise_tree(r,root&BtmLeft)
end if
end if
end procedure
visualise_tree(tree)