public void Build() { if (!Synced) { lock (this) { Head = new IntervalNode(Intervals); Synced = true; } } }
/// <summary> /// Build an interval tree /// </summary> /// <param name="intervals"></param> public void Build(IEnumerable <Interval> intervals) { if (intervals.Count() == 0) { Center = 0; return; } // Calculate median point Center = Interval.GetMedian(intervals); // Split intervals to the left, right, and intersecting "center" List <Interval> left = new List <Interval>(); List <Interval> right = new List <Interval>(); List <Interval> intersecting = new List <Interval>(); foreach (Interval interval in intervals) { if (interval.OneBasedEnd < Center) { left.Add(interval); } if (interval.OneBasedStart > Center) { right.Add(interval); } else { intersecting.Add(interval); } } IntervalsCenter = intersecting; // Recurse if (left.Count > 0) { LeftNode = new IntervalNode(left); } if (right.Count > 0) { RightNode = new IntervalNode(right); } }
/// <summary> /// Instantiate a new interval tree with a list of intervals /// </summary> /// <param name="intervals"></param> public IntervalTree(IEnumerable <Interval> intervals) { Head = new IntervalNode(intervals); Intervals = new List <Interval>(intervals); Synced = true; }