We still use the tree definition in previous post:
type Element = int
type TreeType =
| EmptyTree
| Tree of TreeType * Element * TreeType
let t = Tree (Tree(EmptyTree, 5, EmptyTree), 1 ,Tree(EmptyTree, 3, EmptyTree))
Now we define the function to explore this structure to a sequence.
let rec f context =
match context with
| [] -> None
| head::tail ->
match head with
| EmptyTree -> f tail
| Tree(l,n,r) -> Some(n, tail@[l;r])
let a = [t] |> Seq.unfold f
printfn "%A" a
I'd not say this approach a good functional way. Also, this is tree in order transverse, is there a way to do other two way to transverse a tree?
I'd not say this approach a good functional way. Also, this is tree in order transverse, is there a way to do other two way to transverse a tree?