An implementation of a Centered Interval Tree for efficient search in a set of intervals. See https://en.wikipedia.org/wiki/Interval_tree . The tree functions as a set, meaning that it will not store an interval more than once. More forma
| 37 | * @param <T> The type for the start and end point of the interval |
| 38 | */ |
| 39 | public class IntervalTree<T extends Comparable<? super T>> extends AbstractSet<Interval<T>> { |
| 40 | |
| 41 | /** |
| 42 | * The root of the current interval tree. It is {@code null} initially, when the tree is |
| 43 | * empty and may change as the result of adding or removing intervals to the tree. |
| 44 | */ |
| 45 | TreeNode<T> root; |
| 46 | |
| 47 | /** |
| 48 | * The size of the interval tree, or the amount of intervals stored in it. |
| 49 | */ |
| 50 | int size; |
| 51 | |
| 52 | /** |
| 53 | * Adds an interval to the tree. If the interval is empty, it is rejected and not |
| 54 | * stored in the tree. This operation may cause a rebalancing of the tree, which |
| 55 | * in turn may cause intervals to be {@link TreeNode#assimilateOverlappingIntervals(TreeNode) assimilated}. |
| 56 | * This is why this operation may run in {@code O(n)} worst-case time, even though |
| 57 | * on average it should run in {@code O(logn)} due to the nature binary trees. |
| 58 | * |
| 59 | * @param interval The interval to be added to the tree. |
| 60 | * @return {@code true}, if the tree has been modified as a result of the operation, |
| 61 | * or {@code false} otherwise. |
| 62 | */ |
| 63 | @Override |
| 64 | public boolean add(Interval<T> interval){ |
| 65 | if (interval.isEmpty()) |
| 66 | return false; |
| 67 | int sizeBeforeOperation = size; |
| 68 | root = TreeNode.addInterval(this, root, interval); |
| 69 | return size == sizeBeforeOperation; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Searches for and returns all intervals stored in the tree, that contain a given |
| 74 | * query point. This operation is guaranteed to run in {@code O(logn + k)}, where |
| 75 | * {@code n} is the size of the tree and {@code k} is the size of the returned set, |
| 76 | * provided that the time complexity of iterating over the intervals stored in each |
| 77 | * visited node is amortized {@code O(1)}. This assumption is met for the current |
| 78 | * implementation of {@link TreeNode}, where {@link TreeSet}s are used. |
| 79 | * |
| 80 | * @param point The query point. |
| 81 | * @return A set containing all intervals from the tree, intersecting the query point. |
| 82 | */ |
| 83 | public Set<Interval<T>> query(T point){ |
| 84 | return TreeNode.query(root, point, new HashSet<Interval<T>>()); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Searches for and returns all intervals stored in the tree, that intersect a given |
| 89 | * query interval. This operation is guaranteed to run in {@code O(logn + k)}, where |
| 90 | * {@code n} is the size of the tree and {@code k} is the size of the returned set, |
| 91 | * provided that the time complexity of iterating over the intervals stored in each |
| 92 | * visited node is amortized {@code O(1)}. This assumption is met for the current |
| 93 | * implementation of {@link TreeNode}, where {@link TreeSet}s are used. |
| 94 | * |
| 95 | * @param interval The query interval. |
| 96 | * @return A set containing all intervals from the tree, intersecting the query interval. |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…