Class IntervalTree<T extends Interval>

  • Type Parameters:
    T - - the type of Interval this tree contains
    All Implemented Interfaces:
    Iterable<T>

    public class IntervalTree<T extends Interval>
    extends Object
    implements Iterable<T>
    The following class is adapted from: a balanced binary-search tree keyed by Interval objects.

    The underlying data-structure is a red-black tree largely implemented from CLRS (Introduction to Algorithms, 2nd edition) with the interval-tree extensions mentioned in section 14.3

    Author:
    Mason M Lai
    • Constructor Detail

      • IntervalTree

        public IntervalTree()
        Constructs an empty IntervalTree.
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Whether this IntervalTree is empty or not.
      • size

        public int size()
        The number of intervals stored in this IntervalTree.
      • contains

        public boolean contains​(T t)
        Whether or not this IntervalTree contains the given Interval.
        Parameters:
        t - - the Interval to search for
      • get

        public T get​(int s,
                     int e)
        Whether or not this IntervalTree contains the given Interval.
        Parameters:
        s - - the starting time of the Interval to search for
        e - - the ending time of the Interval to search for
      • minimum

        public Optional<T> minimum()
        The minimum value in this IntervalTree
        Returns:
        an Optional containing, if it exists, the minimum value in this IntervalTree; otherwise (i.e., if this is empty), an empty Optional.
      • maximum

        public Optional<T> maximum()
        The maximum value in this IntervalTree
        Returns:
        an Optional containing, if it exists, the maximum value in this IntervalTree; otherwise (i.e., if this is empty), an empty Optional.
      • overlappers

        public Iterator<T> overlappers​(int start,
                                       int end)
        An Iterator over the Intervals in this IntervalTree that overlap the given Interval
        Parameters:
        start - - the starting point of the overlapping Interval
        end - - the ending point of the overlapping Interval
      • forAllBelow

        public void forAllBelow​(int lb,
                                Consumer<T> ex)
      • forAllAbove

        public void forAllAbove​(int ub,
                                Consumer<T> ex)
      • insert

        public boolean insert​(T t)
        Inserts the given value into the IntervalTree.

        This method constructs a new Node containing the given value and places it into the tree. If the value already exists within the tree, the tree remains unchanged.

        Parameters:
        t - - the value to place into the tree
        Returns:
        if the value did not already exist, i.e., true if the tree was changed, false if it was not
      • delete

        public boolean delete​(T t)
        Deletes the given value from this IntervalTree.

        If the value does not exist, this IntervalTree remains unchanged.

        Parameters:
        t - - the Interval to delete from the tree
        Returns:
        whether or not an Interval was removed from this IntervalTree