Fast Algorithm to Quickly Find the Range a Number Belongs to in a Set of Ranges?

后端 未结 5 1616
情书的邮戳
情书的邮戳 2020-11-29 00:53

The Scenario

I have several number ranges. Those ranges are not overlapping - as they are not overlapping, the logical consequence is that no number can be part of

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 01:31

    As an alternative to O(log n) balanced binary search trees (BST), you could consider building a bitwise (compressed) trie. I.e. a prefix tree on the bits of the numbers you're storing.

    This gives you O(w)-search, insert and delete performance; where w = number of bits (e.g. 32 or 64 minus whatever power of 2 your ranges were based on).

    Not saying that it'll perform better or worse, but it seems like a true alternative in the sense it is different from BST but still has good theoretic performance and allows for predecessor queries just like BST.

提交回复
热议问题