A red-black tree has some constraints:
- A node is either red or black.
- The root is black.
- All leaves are black.
- Both children of every red node are black.
- Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
Using the declarations in RBTree.hs, all these five constraints are implemented.
This seems to have a serious efficiency problem: searching for a key requires a three-way pattern match (Leaf/RedBranch/BlackBranch) at each node, rather than just a two-way match (Leaf/Branch) as in a standard red-black tree. Is there a way to avoid this without compromising type safety? Or would it be better to switch to 2-3-4 trees, whose invariant is easier to represent through types?