/// <summary> /// return a node which overlaps item /// returns a pointer to an element x in the interval tree T such that x:int overlaps interval i , or a pointer to the sentinel T:nil if no such element /// </summary> /// <param name="item"></param> /// <returns></returns> public IntervalTreeNode Search(Interval item) { IntervalTreeNode x = root; while (x != null && !x.Interval.Overlap(item)) { if (x.Left != null && x.Left.Max >= item.Low) { x = x.Left; } else { x = x.Right; } } return(x); }
/// <summary> /// Red Black insert. adds the element x, who has interval attribute. /// </summary> /// <param name="key"></param> /// <param name="val"></param> public void Insert(Interval item) { Debug.Assert(item != null); IntervalTreeNode node = null, inserted = new IntervalTreeNode(item); count++; IntervalTreeNode x = root; while (x != null) { node = x; x = item.Low.CompareTo(node.Interval.Low) < 0 ? x.Left : x.Right; } inserted.Parent = node; //first insert a node if (node == null) { root = inserted; } else if (inserted.Interval.Low < node.Interval.Low) { node.Left = inserted; } else { node.Right = inserted; } inserted.Left = inserted.Right = null; inserted.Color = Color.RED; //fix up the insert node, so red black tree reserve InsertFixup(inserted); }