Efficient data structure for fast random access, search, insertion and deletion

前端 未结 8 2121
灰色年华
灰色年华 2021-01-31 11:11

I\'m looking for a data structure (or structures) that would allow me keep me an ordered list of integers, no duplicates, with indexes and values in the same range.

I ne

8条回答
  •  旧巷少年郎
    2021-01-31 11:50

    I would use a red-black tree to map keys to values. This gives you O(log(n)) for 1, 3, 4. It also maintains the keys in sorted order.

    For 2, I would use a hash table to map values to keys, which gives you O(1) performance. It also adds O(1) overhead for keeping the hash table updated when adding and deleting keys in the red-black tree.

提交回复
热议问题