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.




// version 1.2.0
 
import java.util.Random
 
class Stem(var str: String? = null, var next: Stem? = null)
 
const val SDOWN = "  |"
const val SLAST = "  `"
const val SNONE = "   "
 
val rand = Random()
 
fun tree(root: Int, head: Stem?) {
    val col = Stem()
    var head2 = head
    var tail = head
    while (tail != null) {
        print(tail.str)
        if (tail.next == null) break
        tail = tail.next
    }
    println("--$root")
    if (root <= 1) return
    if (tail != null && tail.str == SLAST) tail.str = SNONE
    if (tail == null) {
        head2 = col
        tail = head2
    }
    else {
        tail.next = col
    }
    var root2 = root
    while (root2 != 0) { // make a tree by doing something random
        val r = 1 + rand.nextInt(root2)
        root2 -= r
        col.str = if (root2 != 0) SDOWN else SLAST
        tree(r, head2)
    }
    tail.next = null
}
 
fun main(args: Array<String>) {
    val n = 8
    tree(n, null)
}