I don\'t know if this is possible but i\'m trying to make an Hashtable of where Interval is a class with 2 integer / long values, a start and an end and i wanted to make so
OldCurmudgeon's solution works perfectly for me, but is very slow to initialise (took 20 mins for 70k entries). If you know your incoming list of items is already ordered (ascending) and has only non overlapping intervals, you can make it initialise in milliseconds by adding and using the following constructor:
public IntervalTree(List intervals, boolean constructorFlagToIndicateOrderedNonOverlappingIntervals) {
if (intervals == null) throw new NullPointerException();
int centerPoint = intervals.size() / 2;
T centerInterval = intervals.get(centerPoint);
this.intervals = new ArrayList();
this.intervals.add(centerInterval);
this.uBound = centerInterval.getEnd();
this.lBound = centerInterval.getStart();
this.center = (this.uBound + this.lBound) / 2;
List toTheLeft = centerPoint < 1 ? Collections.emptyList() : intervals.subList(0, centerPoint);
this.left = toTheLeft.isEmpty() ? null : new IntervalTree(toTheLeft, true);
List toTheRight = centerPoint >= intervals.size() ? Collections.emptyList() : intervals.subList(centerPoint+1, intervals.size());
this.right = toTheRight.isEmpty() ? null : new IntervalTree(toTheRight, true);
}