O'Reilly logo

Mastering F# by Suhaib Fahad, Alfonso García-Caro Núñez

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required