private bool Delete(ref Node currentNode, Interval element)
        {
            if (currentNode == sentinel)
            {
                return false;
            }

            bool result = false;

            if (currentNode.key.CompareTo(element.Start) < 0)
            {
                result = this.Delete(ref currentNode.right, element);
            }
            else if (currentNode.key.CompareTo(element.Start) > 0)
            {
                result = this.Delete(ref currentNode.left, element);
            }
            else
            {
                FindSuccessorValue(ref currentNode);
                result = true;
            }

            if (result)
            {
                currentNode.isRebalanced = true;
            }

            this.DecreaseLevel(ref currentNode);
            this.Skew(ref currentNode);
            this.Skew(ref currentNode.right);

            if (currentNode.right != sentinel)
            {
                this.Skew(ref currentNode.right.right);
            }

            this.Split(ref currentNode);
            this.Split(ref currentNode.right);

            return result;
        }
 public bool Remove(Interval element)
 {
     return this.Delete(ref this.root, element);
 }
 public Interval FindOverlappingInterval(Interval element)
 {
     return this.SearchOverlappingInterval(ref this.root, element);
 }
 internal Node(Interval value, Node sentinel)
 {
     this.level = 1;
     this.key = value.Start;
     this.left = sentinel;
     this.right = sentinel;
     this.highestSubtreeEndpoint = value.End;
     this.value = value;
 }
 public bool Add(Interval element)
 {
     return this.Insert(ref this.root, element);
 }
        private Interval SearchOverlappingInterval(ref Node currentNode, Interval interval)
        {
            if (currentNode == sentinel)
            {
                return null;
            }
            else if ((interval.Start >= currentNode.value.Start && interval.Start <= currentNode.value.End) ||
                (interval.End >= currentNode.value.Start && interval.End <= currentNode.value.End))
            {
                // Intersects
                return currentNode.value;
            }

            if (interval.Start <= currentNode.left.HighestSubtreeEndpoint)
            {
                return this.SearchOverlappingInterval(ref currentNode.left, interval);
            }
            else
            {
                return this.SearchOverlappingInterval(ref currentNode.right, interval);
            }
        }
        private bool Insert(ref Node currentNode, Interval element)
        {
            bool result = false;

            if (currentNode == sentinel)
            {
                currentNode = new Node(element, sentinel);
                return true;
            }

            if (currentNode.key.CompareTo(element.Start) < 0)
            {
                result = this.Insert(ref currentNode.right, element);
            }
            else if (currentNode.key.CompareTo(element.Start) > 0)
            {
                result = this.Insert(ref currentNode.left, element);
            }
            else
            {
                result = false;
            }

            if (result)
            {
                currentNode.isRebalanced = true;
                this.Skew(ref currentNode);
                this.Split(ref currentNode);
            }

            return result;
        }