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!
BT
is either aLeaf
(not containing any values), or aBranch
that contains two suchBT
subtrees, and ana
and ab
value. – Willem Van Onsemdata BT a = Leaf | Branch (BT a) a a (BT a)
would be more likely. – chepnern+1
children for a node withn
values. – chepnera
is the search key andb
is the actual data. That would suggest functions likeinsert :: Ord a => a -> b -> BT a b -> BT a b
andfind :: Ord a => a -> BT a b -> Maybe b
. – chepner