mnemonist icon indicating copy to clipboard operation
mnemonist copied to clipboard

TreeMap

Open pbadenski opened this issue 5 years ago • 2 comments

Any plans to add TreeMap (or similar), ie. Map that preserves insertion order, but at the same time gives very good of performance of operations .

pbadenski avatar Jan 09 '20 15:01 pbadenski

Hello @pbadenski

Any plans to add TreeMap (or similar), ie. Map that preserves insertion order,

I am not sure to know what you mean. ES6 Map already preserves insertion order (using linked nodes in the hashmap rather than using a tree in most implementation if I recall correctly).

but at the same time gives very good of performance of operations .

So I guess there is one kind of operation of the ES6 Map which is not fast enough for you. Could you give one of your use case so I can understand? I could very well add a TreeMap to the library but I would like to know what kind of use cases it could solve in JS that isn't handled by something else already.

Yomguithereal avatar Jan 09 '20 21:01 Yomguithereal

Just to revive an old conversation. I think @pbadenski is asking to add a similar data structure to java's TreeMap. The primary use case (that I've seen) is an efficient query the nearest key (floor or ceil) for a given search such as in floorKey and ceilKey. To do this you'd need to maintain an ordered list of the keys and binary search. I think the target efficiency is O(log n) insertion and O(log n) search (floor, ceil) and O(1) exact match.

I'd be happy to implement this since I ran into this today. Is there an ordered list data structure that can be used?

booi avatar Mar 30 '22 06:03 booi