Are there any implementations of a purely functional standard binary heap? I know there are lots of interesting heaps eg: Binomial, leftist heap, they all have functional implementation, just wonder is there a way to implement standard binary heap or we have to use Array to implement it, because of the immutable type ? Thanks!
Are there any implementations of a purely functional standard binary heap? I know there
Share
You don’t need an array to implement a heap, you can implement it as a tree structure.
The drawback is that you end up reallocating
O(log N)of the nodes for every heap operation, and you won’t have any of the cache locality of an imperative array-based implementation. Some operations will be difficult with this structure, but since I don’t know what you want to do with the heap I can’t point you in a more specific direction.The reason we have special functional structures like finger trees is to speed up specific operations that you don’t normally perform on heaps, like retrieving the leftmost leaf node. You can use many of the same data structures you learned for imperative languages in Haskell with only changes to the ways they are updated.