Advanced data structures

You have learned to use data structures built-in in the FSharp.Core library; now you will learn how to define other advanced data structures on your own in F#.

Binary trees

Binary trees are often used to represent collections of data. For our purpose, a binary tree is an acyclic graph, in which each node has either zero or two children. The top-level node is called the root, and it has no parents, while all other nodes have exactly one parent. A simple way of representing a binary tree is as follows:

    type 'a tree = 
        | Leaf 
        | Node of 'a * 'a tree * 'a tree 

The use of tuples in the Node constructor definition is quite common. We will try to create some objects using this tree in the F# Interactive, as mentioned in the following ...

Get Mastering F# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.