F# Binary Tree and Tree Traversal So for an side exercise on exercism I implemented a BinarySearchTree. 
I was confident in implementing the creation of the binary tree. But I'm very unsure about the traversal of the tree. So I came up with two implementations of the traversal algorithm - which one is the "better" solution?

I'm learning about F# because I want to learn Functional Programming. This is code review - so any criticism is welcome!

module BinarySearchTree 

type Node&lt;'a&gt; =
    { Left: Node&lt;'a&gt; option
      Right: Node&lt;'a&gt; option
      Data: 'a }

let left (node: Node&lt;'a&gt;)  = node.Left

let right (node: Node&lt;'a&gt;) = node.Right

let data (node: Node&lt;'a&gt;) : 'a = node.Data

let rec insertNode (root: Node&lt;'a&gt; option) (nextValue: 'a) : Node&lt;'a&gt; =
    match root with
    | Some root -&gt; if nextValue &lt;= root.Data then { root with Left = Some(insertNode root.Left nextValue) }
                   elif nextValue &gt; root.Data then { root with Right = Some(insertNode root.Right nextValue) }
                   else root
    | None -&gt; { Left=None; Right=None; Data=nextValue }

let create (items: 'a list) : Node&lt;'a&gt; =
     items
     |&gt; List.fold (fun (acc: Option&lt;Node&lt;'a&gt;&gt;) (item: 'a) -&gt; Some(insertNode acc item)) None
     |&gt; function
        | None -&gt; failwith "Failed to construct binary tree. You must pass a valid item list."
        | Some root -&gt; root  

let sortedData (node: Node&lt;'a&gt;) : ('a list) =
    let rec traverse (acc: 'a list) (node: Node&lt;'a&gt;)  =     
        match (node.Left, node.Right) with
        | Some left, Some right -&gt; seq {
                                   yield! node.Data::acc
                                   yield! (traverse [] left)
                                   yield! (traverse [] right)}
                                   |&gt; Seq.toList
        | Some left, None -&gt; traverse (node.Data::acc) left
        | None, Some right -&gt; traverse (node.Data::acc) right
        | None, None -&gt; node.Data::acc

    List.sort (traverse [] node)


And the alternative implementation of sortedData: 

let sortedData (node: Node) : (int list) =



 let rec traverse (acc: int list) (node: Node)  =     
        match (node.Left, node.Right) with
        | Some left, Some right -&gt; (node.Data::acc)@(traverse [] left)@(traverse [] right) 
        | Some left, None -&gt; traverse (node.Data::acc) left
        | None, Some right -&gt; traverse (node.Data::acc) right
        | None, None -&gt; node.Data::acc

    List.sort (traverse [] node)
In general in F# we try to minimize the use of explicit type declaration on function arguments:

let left (node: Node&lt;'a&gt;) = node.Left


becomes

let left node = node.Left


etc.



The function insertNode violates the single responsibility principle in that if root =  None then it creates a new tree/root node instead of actual inserting the value.

Personal I wouldn't allow root to be Option/None

Instead I would provide a function called newTree taking a 'a value as argument and returning a Node&lt;'a&gt;




  let rec insertNode (root: Node&lt;'a&gt; option) (nextValue: 'a) : Node&lt;'a&gt; = ...


In general you should have object in question of the module as the last argument of a function in order to make use of the pipe operator:

let rec insertNode value root =...

root |&gt; BinarySearchTree.insertNode value




sortedData seems overly complicated - maybe from the use of List.sort. In principle it's just a depth-first-search (dfs), where you for each node can concatenate the values in the left child node with the value of the node itself concatenated with the values in the right child node:

For a list it could be:

let toList root =
    let rec dfs optNode =
        match optNode with
        | Some node -&gt; (dfs (left node))@(data node::dfs (right node))
        | None -&gt; []

    dfs (Some root)


Or if you prefer a Sequence (which I do):

let toSeq root =
    let rec dfs optNode =
        match optNode with
        | Some node -&gt; seq {
            yield! dfs (left node)
            yield data node
            yield! dfs (right node)
            }
        | None -&gt; Seq.empty

    dfs (Some root)


I call the functions toList and toSeq. Maybe toOrderedList/Seq are better names.



You could consider to extend the List-module with a toBinaryTree items instead of the create items function. That way it is possible to write:

let data = [ 5; 8; 1; 5; 1; 2; 2; 4; 6; 3 ]
let root = data |&gt; List.toBinaryTree


which may be more intuitive



In the create items function you should consider to sort the items and do a binary insertion in order to create a balanced tree, because if you provide a list of items that is sorted then your function will merely produce a singly linked list of nodes using only one of the child nodes instead of a tree.

You should consider to provide a function that can balance an existing tree.



As an alternative to the Node&lt;'a&gt; type as a record type, you could consider to define it as a discriminated union type with the following definition:

type Node&lt;'a&gt; =
    | Empty
    | Node of Data:'a * Left:Node&lt;'a&gt; * Right:Node&lt;'a&gt;


That is more in line with the functional style of F# and your functions become somewhat more simple and intuitive:

module BinaryTree =

let data = function Empty -&gt; failwith "Empty Tree" | Node (d, _, _) -&gt; d
let left = function Empty -&gt; Empty | Node (_, l, _) -&gt; l
let right = function Empty -&gt; Empty | Node (_, _, r) -&gt; r

let first root = 
    let rec goLeft node =
        match node with
        | Empty -&gt; None
        | Node (d, ln, _) -&gt;
            match ln with
            | Empty -&gt; Some d
            | Node _ -&gt; goLeft ln

    goLeft root

let last root = 
    let rec goRight node =
        match node with
        | Empty -&gt; None
        | Node (d, _, rn) -&gt;
            match rn with
            | Empty -&gt; Some d
            | Node _ -&gt; goRight rn

    goRight root

let rec insert data root =
    match root with
    | Empty -&gt; Node(data, Empty, Empty)
    | Node (d, l, r) -&gt;
        match data &lt;= d with
        | true -&gt; Node (d, l |&gt; insert data, r)
        | false -&gt; Node (d, l, r |&gt; insert data)

let toOrderedSeq root =
    let rec dfs node =
        match node with
        | Empty -&gt; Seq.empty
        | Node (d, ln, rn) -&gt; seq {
                yield! dfs ln
                yield d
                yield! dfs rn
            }

    dfs root


module List =
    let toBinaryTree data = data |&gt; List.fold (fun n x -&gt; n |&gt; BinaryTree.insert x) Empty


As seen, the use of option is unnecessary and the insert function is consistent, because an Empty node as root is also a Node&lt;'a&gt;/Tree - just without any elements.