I am working on an assignment in Haskell and had a question about the implementation of a binary search tree that I was given. The book I am using to learn Haskell uses the following implementation for a binary tree:
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
This definition makes a lot of sense to me as it shows that a tree is either an empty tree or an element that contains a value and two trees. However, the implementation of a binary search tree that I was given in my assignment is as followed:
data BT a b
= Leaf
| Branch (BT a b) a b (BT a b)
deriving (Eq, Show)
In this implementation does each branch represent a pair of nodes that either is made up of more branches or leaves? Also what advantages/disadvantages would this implementation have over the traditional one? Any help would be greatly appreciated!
BTis either aLeaf(not containing any values), or aBranchthat contains two suchBTsubtrees, and anaand abvalue.data BT a = Leaf | Branch (BT a) a a (BT a)would be more likely.n+1children for a node withnvalues.ais the search key andbis the actual data. That would suggest functions likeinsert :: Ord a => a -> b -> BT a b -> BT a bandfind :: Ord a => a -> BT a b -> Maybe b.