Mini Binary Tree "library" This is my first attempt at learning binary trees. I'm looking for general feedback on how it could be made cleaner, more idiomatic, or more efficient.

data Tree a = ETree | Node (Tree a) (Tree a) a deriving (Show)

tInsert :: Ord o =&gt; o -&gt; Tree o -&gt; Tree o
tInsert x ETree = Node ETree ETree x
tInsert x (Node lT rT a)
    | x &lt; a = Node (tInsert x lT) rT a
    | x &gt; a = Node lT (tInsert x rT) a
    | otherwise = Node lT rT x

-- Folds a list into a tree
tFromListL, tFromListR :: Ord o =&gt; [o] -&gt; Tree o
tFromListL = foldl (flip tInsert) ETree
tFromListR = foldr tInsert ETree

-- Turns a tree into a list
tToList :: Ord o =&gt; Tree o -&gt; [o]
tToList ETree = []
tToList (Node lT rT a) = (tToList lT) ++ [a] ++ (tToList rT)

-- Splits a list roughly in half (as part of balancing)
splitInHalf :: [a] -&gt; ([a],[a])
splitInHalf xs = splitAt (round $ (fromIntegral $ length xs) / 2.0) xs

-- Returns how unbalanced a node is
tUnbalancedBy :: Tree a -&gt; Int
tUnbalancedBy ETree = 0
tUnbalancedBy (Node lT rT _) = absDiff (tDepth lT) (tDepth rT)

-- Arranges a list in such a way that it forms a more balanced tree
balanceList :: [a] -&gt; [a]
balanceList xs = let (fH,sH) = splitInHalf xs in (reverse fH) ++ sH

-- "Inefficient balance"
tIneffBalance :: Ord o =&gt; Tree o -&gt; Tree o
tIneffBalance = tFromListL . balanceList . tToList

-- Finds the min/max values of a tree
tMin, tMax :: Ord o =&gt; Tree o -&gt; o
tMin ETree = error "tMin called on an Empty Tree"
tMin (Node lT _ a) = case lT of
    ETree           -&gt; a
    (Node lT' _ _)  -&gt; tMin lT'

tMax ETree = error "tMax called on an Empty Tree"
tMax (Node _ rT a) = case rT of
    ETree           -&gt; a
    (Node _ rT' _)  -&gt; tMax rT'

-- Find the max depth of a tree 
tDepth :: Tree a -&gt; Int
tDepth ETree = 0
tDepth (Node lT rT _) = 1 + max (tDepth lT) (tDepth rT)

-- Finds how many nodes a tree contains
tSize :: Tree a -&gt; Int
tSize ETree = 0
tSize (Node lT rT _) = 1 + (tSize lT) + (tSize rT)

absDiff :: Int -&gt; Int -&gt; Int
absDiff x y = abs $ x - y

-- Checks if a node is balanced
tIsBalanced :: Tree a -&gt; Bool
tIsBalanced ETree = True
tIsBalanced n
    | tUnbalancedBy n &gt; 2 = False
    | otherwise = True

-- Checks if a value is an element of the tree
tElem :: Ord o =&gt; o -&gt; Tree o -&gt; Bool
tElem x ETree = False
tElem x (Node lT rT a)
    | x &lt; a = tElem x lT
    | x &gt; a = tElem x rT
    | otherwise = True

tDelete :: Ord o =&gt; o -&gt; Tree o -&gt; Tree o
tDelete _ ETree = ETree
tDelete _ n@(Node ETree ETree _) = n -- Or give "Not found" error?
tDelete tD n@(Node lT rT a)
    | tD &lt; a = Node (tDelete tD lT) rT a
    | tD &gt; a = Node lT (tDelete tD rT) a
    | otherwise = case (lT,rT) of
        (ETree,t)   -&gt; t
        (t,ETree)   -&gt; t
        (t,t')      -&gt; let fMin = tMin t' in Node t (tDelete (tMin t') t') fMin


My main concerns are:


The balancing algorithm. I thought it would work beautifully at first, then I realized that it basically just turns the tree into a "V". It's more efficient then a linked list, but still not very balanced
The 2 "tToList*" functions. Should I use foldl or foldr? They seem to act fairly equivalently; although foldl is easier to use because you typically read a list from the left
The "tDelete" function. It works (from my testing), but it looks very ugly. I basically just wrote it case-by-case, which made it very long.
Function Visibility

In the code there's some functions that seem to be intended for internal use only. To hide these from external modules you should have a "proper" module declaration that exports those functions you want to make "public".

module MyTree ( Tree(ETree, Node), tInsert, tFromListL, tFromListR, ... ) where


this allows you to hide (and change) implementation details of your exposed function.

Balancing Your Tree

Your algorithm to balance that tree is something along the lines of \$\mathcal{O}(3 * n)\$. That's bad compared to an almost constant time-effort when balancing in tree form. Then again why are you even exposing this functionality?

If you allow a tree to be unbalanced, you shouldn't expose an operation to balance it and vice versa. You should strongly consider extracting balanced behaviour into either a separate type and using instance to expose a common "interface" or extract this into a completely separate module.

Something like this:

class (Ord a) =&gt; Tree a where
    tInsert, tDelete :: a -&gt; Tree a -&gt; Tree a
    tElem :: a -&gt; Tree a -&gt; Bool
    tIsBalanced :: Tree a -&gt; Bool
    tSize, tDepth :: Tree a -&gt; Int
    tMin, tMax :: Tree a -&gt; a
    tToList :: Tree a -&gt; [a]

data BalancedTree a = Empty | Node (BalancedTree a) (BalancedTree a) a deriving (Show)

instance NormalTree BalancedTree where
-- | because we are an instance of NormalTree we can access tInsert'
    tInsert = balance . tInsert'
-- and so on and so forth

data NormalTree a = Empty | Node (Tree a) (Tree a) a deriving (Show)

instance Tree NormalTree where
    tInsert = tInsert'

    -- | default implementation for insert without balancing
    tInsert' :: a -&gt; Tree a -&gt; Tree a



  Note that I haven't even used this myself and I'm not sure whether
  this works. The code should give you a gist as to how this could /
  should work and isn't even intended to be syntactically correct.


This defines the "interface" of Tree in a class and then uses data types to implement said "interface". 

Errors In Expected Circumstances

When you call tMax or tMin respectively on an ETree (which would be better named as Empty) an error is raised. That's an IMO really ugly and declarative way of solving this. It'd be preferrable to have a pure function. This requires a small change to the type-signature:

tMin, tMax :: Ord o =&gt; Tree o -&gt; Maybe o


Which in turn allows you to do the following:

tMin Empty = Nothing
tMin (Node lT _ a) = case lT of 
    Empty -&gt; a
    (Node lT' _ _) -&gt; tMin lT'


This function is total and pure. As such it's much easier and cleaner to use.