/intervaltree

[unmaintained] Interval Tree implementation in Java

Primary LanguageJava

Generic Interval Tree Implementation in Java

I needed a Generic interval tree. Kevin Dolan's was close but did not correctly use Java Generics. There was also an err on inclusive endpoint comparison. I have corrected it and shared for posterity.

Released under the WTFPL.

The following code snippet shows you how to use the library:

IntervalTree<Long, Integer> it = new IntervalTree<>();
 
it.addInterval(0L,10L,1);
it.addInterval(20L,30L,2);
it.addInterval(15L,17L,3);
it.addInterval(25L,35L,4);

List result1 = it.get(5L);
List result2 = it.get(10L);
List result3 = it.get(29L);
List result4 = it.get(5L,15L);

System.out.println("Intervals that contain 5L:");
for(int r : result1)
    System.out.println(r);

System.out.println("Intervals that contain 10L:");
for(int r : result2)
    System.out.println(r);

System.out.println("Intervals that contain 29L:");
for(int r : result3)
    System.out.println(r);
     
System.out.println("Intervals that intersect (5L,15L):");
for(int r : result4)
    System.out.println(r);

This code would output:

Intervals that contain 5L:
1
Intervals that contain 10L:
1
Intervals that contain 29L:
2
4
Intervals that intersect (5L,15L):
3
1