Ejemplo n.º 1
0
        private List <Interval> FindIntervals(IntervalTreeNode node, int value)
        {
            if (node == null)
            {
                return(new List <Interval>());
            }

            if (node.Intervals.Count == 0)
            {
                if (node.LeftChild != null && node.LeftChild.RightMaxValue >= value)
                {
                    return(FindIntervals(node.LeftChild, value));
                }
                else if (node.RightChild != null && node.RightChild.LeftMinValue <= value)
                {
                    return(FindIntervals(node.RightChild, value));
                }
                else
                {
                    return(new List <Interval>());
                }
            }
            var result = new List <Interval>();

            if (node.LeftMinValue <= value && node.RightMaxValue >= value)
            {
                foreach (var interval in node.Intervals)
                {
                    if (interval.Bottom <= value && interval.Top >= value)
                    {
                        result.Add(interval);
                    }
                }
            }

            if (node.LeftChild != null && node.LeftChild.RightMaxValue > value)
            {
                result.AddRange(FindIntervals(node.LeftChild, value));
            }
            else if (node.RightChild != null && node.RightChild.LeftMinValue < value)
            {
                result.AddRange(FindIntervals(node.RightChild, value));
            }

            return(result);
        }
Ejemplo n.º 2
0
        private IntervalTreeNode CreateIntervalTreeNode(List <Interval> intervals, IntervalTreeNode parent)
        {
            if (intervals == null || intervals.Count == 0)
            {
                return(null);
            }

            var min     = intervals.Select(x => x.Bottom).Min();
            var max     = intervals.Select(x => x.Top).Max();
            var avg     = (min + max) / 2;
            var less    = new List <Interval>();
            var more    = new List <Interval>();
            var correct = new List <Interval>();

            SortIntervalsList(intervals, avg, correct, less, more);
            var node = new IntervalTreeNode(correct, parent);

            node.LeftChild  = CreateIntervalTreeNode(less, node);
            node.RightChild = CreateIntervalTreeNode(more, node);
            return(node);
        }
Ejemplo n.º 3
0
 public IntervalTree(List <Interval> intervals)
 {
     _intervals = intervals;
     Root       = CreateIntervalTreeNode(_intervals, null);
 }