Effective Haskell: Difficult definition of binary tree (p 105, pdf)

The definition of BinaryTree doesn’t allow for an empty tree (the minimum definition is a Leaf with one element) or two elements (next up is a Branch with two Leafs), which makes it difficult, if not impossible, to implement the addElementToIntTree in the exercises.

With the base case being:

addElementToIntTree (Leaf x) y = ...

Returning a Branch would require three elements (x, y, and ?).

With a definition allowing for empty leaves, it would be possible, but not necessarily pretty. For instance:

data BinaryTree a = Empty | Branch (BinaryTree a) a (BinaryTree a)

Thanks for the note! I’ll look at updating the exercise to address that.