Last active
May 1, 2025 11:20
-
-
Save lamg/a10a289e597e6d667b3251ecff7b6d01 to your computer and use it in GitHub Desktop.
Print a tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the trick: | |
// each node must be connected directly to its direct children (with visible characters) | |
// and indirectly (through indentation) to its children's descendants | |
type Tree<'a> = | |
| Leaf of 'a | |
| Branch of 'a * list<Tree<'a>> | |
let printTree (t: Tree<'a>) = | |
let connectIndent (isLast: bool) (child: string, grandChild: string list) = | |
let childConn, colConn = if isLast then "└── ", " " else "├── ", "│ " | |
let connected = childConn + child | |
let indented = grandChild |> List.map (fun x -> colConn + x) | |
connected :: indented | |
let rec treeToLines t = | |
match t with | |
| Branch(v, xs) -> | |
let l = Seq.length xs | |
let root = v.ToString() | |
let children = | |
xs | |
|> List.mapi (fun i c -> treeToLines c |> connectIndent (i = l - 1)) | |
|> List.concat | |
root, children | |
| Leaf v -> v.ToString(), [] | |
let r, chl = treeToLines t | |
r :: chl | |
let tree = Branch(10, [ Branch(15, [ Leaf 12; Leaf 18 ]) ]) | |
for x in printTree tree do | |
printfn $"{x}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment