Last active
August 29, 2015 14:03
-
-
Save cloudRoutine/bdef814ce2e89ca95731 to your computer and use it in GitHub Desktop.
Prints Binary Trees in horizontal orientation connected by pipes
This file contains 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
(* | |
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