I need a simple tree like this, with all the fixings –
type 'a Tree =
| Leaf of 'a
| Branch of 'a Tree list
There’s got to be something like this available already with nice add, remove, map, filter, fold functions etc, but I can’t find it. I don’t even see one from OCaml that I can port… Guess I could write one myself if necessary.
EDIT: Changed structure of tree to be more obvious.
I think the difficulty is that a simple tree (with say
add Tree Tree) would be used by no one. Without specifying a more concrete type of tree you would have to implement all of those methods by scanning, tanking performance.Additionally in place updates of immutable trees are very expensive, since there are few shared data structures in the typical design.
Finally, immutable trees have to be completely rewritten every time if you allow any kind of back tracking.