0

I'm trying to find the correct data structure for this use case, but apparently do not have the correct terminology for searching.

I have many copies of the following structure:

struct Item {
  var start: Int
  var end: Int
  var type: Int
}

The ranges represented in Item are non-overlapping and contiguous.

I need to be able to:

  • Query which Item is present at a given Int index
  • Walk forwards/backwards from the located Item

The collection of Items will be updated regularly, either as a complete re-build or by appending to the end. Mid-collection inserts will likely be rare enough to warrant a rebuild from scratch.

Can anyone please point me to a suitable structure or at perhaps the correct terminology for me to continue researching myself?

Ben
  • 542
  • 1
  • 5
  • 12

1 Answers1

1

If the items are non-overlapping and contiguous, you do not need the end (as it's implied by the next start), and you can just use any data structure you'd use for sorted data, e.g. trees or lists.

In the general case (= with overlaps), you'd probably use an interval tree.

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51