Пример #1
0
        /// <summary>
        /// Find an exact match for the date time range - the search ends after the first match
        /// </summary>
        /// <param name="range">The datetime range to find</param>
        /// <returns>The first node matching the range, or null if there is no match</returns>
        public T FindExact(DateTimeRange range)
        {
            IntervalNode <T> node = root;

            while (node != null)
            {
                if (node.Interval.Equals(range))
                {
                    return(node.Data);
                }

                if (node.Left != null && range.Start < node.Interval.Start && range.Start < node.Left.Max)
                {
                    node = node.Left;
                }
                else if (node.Right != null && range.End >= node.Interval.Start && range.Start <= node.Right.Max)
                {
                    node = node.Right;
                }
                else
                {
                    node = null;
                }
            }

            return(default(T));
        }
Пример #2
0
        private void FindAll(IntervalNode <T> node, DateTimeRange range, IntervalTreeMatch match, List <T> result)
        {
            if (node == null)
            {
                return;
            }

            if (match == IntervalTreeMatch.Contained && range.Contains(node.Interval))
            {
                result.Add(node.Data);
            }
            else if (match == IntervalTreeMatch.ContainedBy && node.Interval.Contains(range))
            {
                result.Add(node.Data);
            }
            else if (match == IntervalTreeMatch.Overlap && range.Overlaps(node.Interval))
            {
                result.Add(node.Data);
            }
            else if (match == IntervalTreeMatch.Exact && range.Equals(node.Interval))
            {
                result.Add(node.Data);
            }

            if (node.Left != null && range.Start < node.Interval.Start && range.Start < node.Left.Max)
            {
                FindAll(node.Left, range, match, result);
            }

            if (node.Right != null && range.End >= node.Interval.Start && range.Start <= node.Right.Max)
            {
                FindAll(node.Right, range, match, result);
            }
        }
Пример #3
0
 private void GetNodeList(IntervalNode <T> node, List <T> result)
 {
     if (node == null)
     {
         return;
     }
     GetNodeList(node.Left, result);
     result.Add(node.Data);
     GetNodeList(node.Right, result);
 }
Пример #4
0
 private void Insert(IntervalNode <T> node)
 {
     InsertNode(root, node, 1);
     if (root == null)
     {
         root     = node;
         numNodes = 1;
         maxDepth = 1;
     }
 }
Пример #5
0
        private void InsertNode(IntervalNode <T> parent, IntervalNode <T> node, int depth)
        {
            if (parent == null)
            {
                return;
            }

            if (parent.Max <= node.Max)
            {
                parent.Max = node.Max;
            }

            if (maxDepth < depth)
            {
                maxDepth = depth;
            }

            if (node.Interval.Start < parent.Interval.Start)
            {
                if (parent.Left == null)
                {
                    parent.Left = node;
                    numNodes++;
                }
                else
                {
                    InsertNode(parent.Left, node, depth + 1);
                }
            }
            else
            {
                if (parent.Right == null)
                {
                    parent.Right = node;
                    numNodes++;
                }
                else
                {
                    InsertNode(parent.Right, node, depth + 1);
                }
            }
        }