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 Leaf
s), 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)