Data structure for non-overlapping ranges within a single dimension

前端 未结 8 1417
再見小時候
再見小時候 2020-12-30 11:26

I need a data structure that can store non-overlapping ranges within a single dimension. The entire range of the dimension need not be completely covered.

An example

8条回答
  •  佛祖请我去吃肉
    2020-12-30 12:05

    1. For non-overlapping intervals you could just sort you intervals with starting point. When you add a new interval to this structure, you could just check that start and end points do not belong to this interval set. To check whether some point X belong interval set you could use binary search to find the nearest start point and check that X belongs it's interval. This approach is not so optimal for modify operations.

    2. You could look at Interval tree structure - for non-overlapping intervals it has optimal query and modify operations.

提交回复
热议问题