A B+ tree has the leaf nodes linked together. Viewing the pointer structure of a B+ tree as directed graph its not cyclic. But ignoring the directions of pointers and viewing it as undirected the leaf nodes linked together creates cycles in the graph.
In Haskell how could a leaf be constructed as the child of a parent internal node and simultaneously the next link from the adjacent leaf node. How could one do this with Haskell's algebraic datatypes? It seems that Haskell ADT in general make cyclic like structures difficult to express.
Here is an idea for (immutable / "mutable"-by-reconstruction / zipperable) ADT representation (involving immutable vectors):
This should be internal, so that improper usage of raw constructors, accessors or pattern-matching wouldn't break the tree.
Keys
,Values
,Childs
length control can be added (with run-time checks or possibly with GADTs and such).And for an interface:
Then this kind of a tree:
can be built as
This technique known as "tying the knot". Leaves can be cycled:
or doubly-linked (assuming
_leafPrev
and correspondingleaf
function):Fully mutable representation is also possible with mutable vectors and mutable references:
and so on, basically the same, but using
IORef
andIOVector
, working inIO
monad.