data BTree a = Leaf a | Branch (BTree a) (BTree a)
    deriving (Eq, Show)

btMin :: Ord a => BTree a -> a
btMin (Leaf l)       = l
btMin (Branch t s)   = min (btMin t) (btMin s)

btMax :: Ord a => BTree a -> a
btMax (Leaf l)       = l
btMax (Branch t s)   = max (btMax t) (btMax s)

isSorted :: Ord a => BTree a -> Bool
isSorted (Leaf y)     = True
isSorted (Branch t s) = isSorted t && isSorted s && btMax t <= btMin s

insert :: Ord a => a -> BTree a -> BTree a

insert x (Leaf y) | x <= y    = Branch (Leaf x) (Leaf y)
insert x (Leaf y) | otherwise = Branch (Leaf y) (Leaf x)

insert x (Branch t s) | x <= btMax t  = Branch (insert x t) s
insert x (Branch t s) | otherwise     = Branch t (insert x s)

