Skip to content

Instantly share code, notes, and snippets.

@cloudRoutine
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudRoutine/bdef814ce2e89ca95731 to your computer and use it in GitHub Desktop.
Save cloudRoutine/bdef814ce2e89ca95731 to your computer and use it in GitHub Desktop.
Prints Binary Trees in horizontal orientation connected by pipes
(*
Prints Trees Like
-----------------
[Node]
|
|_(1)_[Leaf]
|
|_(1)_[Node]
|
|_(2)_[Node]
| |
| |_(3)_[Leaf]
| |
| |_(3)_[Node]
| |
| |_(4)_[Leaf]
|
|_(2)_[Node]
|
|_(3)_[Node]
| |
| |_(4)_[Leaf]
|
|_(3)_[Leaf]
*)
type BTree =
| Node of BTree * BTree
| Leaf
| Empty
let pipe_print (bt:BTree) : string =
let str = string
let concat (s:string list) = String.Concat s
let ind num s =
if num <= 0
then ""
else String.replicate num s
let rec construct t dep apnd : string =
let lvl = dep+1|>str
let pd = "| "
let ws = " "
match t with
| Node(l,r) when l = Empty ->
concat
[ "[Node]\n";
apnd;"|\n";
apnd;"|_(";lvl;")_"; construct r (dep+1) (concat[apnd;ws]); ]
| Node(l,r) when r = Empty ->
concat
[ "[Node]\n";
apnd;"|\n";
apnd;"|_(";lvl;")_"; construct l (dep+1) (concat[apnd;pd]) ]
| Node(l,r) ->
concat
[ "[Node]\n";
apnd;"|\n";
apnd;"|_(";lvl;")_"; construct l (dep+1) (concat[apnd;pd]);"\n";
apnd;"|\n";
apnd;"|_(";lvl;")_"; construct r (dep+1) (concat[apnd;ws]); ]
| Leaf -> "[Leaf]"
| Empty -> "[Empty]"
"\n" + construct bt 0 ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment