void InternalAdd(TreeSegment node) { if (node == null) { throw new ArgumentNullException("node"); } if (node.segmentTree != null) { throw new InvalidOperationException("Node already attached."); } node.segmentTree = this; int insertionOffset = node.Offset; node.DistanceToMaxEnd = node.Length; if (tree.Root == null) { tree.Count = 1; tree.Root = (T)node; node.TotalLength = node.DistanceToPrevNode; return; } if (insertionOffset < tree.Root.TotalLength) { var n = SearchNode(ref insertionOffset); node.TotalLength = node.DistanceToPrevNode = insertionOffset; n.DistanceToPrevNode -= insertionOffset; tree.InsertBefore(n, node); return; } node.DistanceToPrevNode = node.TotalLength = insertionOffset - tree.Root.TotalLength; tree.InsertRight(tree.Root.OuterRight, node); }
void Replace(TreeSegment oldNode, TreeSegment newNode) { if (newNode != null) { newNode.Parent = oldNode.Parent; } if (oldNode.Parent == null) { Root = (T)newNode; } else { if (oldNode.Parent.Left == oldNode) { oldNode.Parent.Left = newNode; } else { oldNode.Parent.Right = newNode; } oldNode.Parent.UpdateAugmentedData(); } }
void DeleteOneChild(TreeSegment node) { // case 1 if (node == null || node.Parent == null) { return; } var parent = node.Parent; var sibling = node.Sibling; if (sibling == null) { return; } // case 2 if (sibling.Color == Red) { parent.Color = Red; sibling.Color = Black; if (node == parent.Left) { RotateLeft(parent); } else { RotateRight(parent); } sibling = node.Sibling; if (sibling == null) { return; } } // case 3 if (parent.Color == Black && sibling.Color == Black && GetColorSafe(sibling.Left) == Black && GetColorSafe(sibling.Right) == Black) { sibling.Color = Red; DeleteOneChild(parent); return; } // case 4 if (parent.Color == Red && sibling.Color == Black && GetColorSafe(sibling.Left) == Black && GetColorSafe(sibling.Right) == Black) { sibling.Color = Red; parent.Color = Black; return; } // case 5 if (node == parent.Left && sibling.Color == Black && GetColorSafe(sibling.Left) == Red && GetColorSafe(sibling.Right) == Black) { sibling.Color = Red; if (sibling.Left != null) { sibling.Left.Color = Black; } RotateRight(sibling); } else if (node == parent.Right && sibling.Color == Black && GetColorSafe(sibling.Right) == Red && GetColorSafe(sibling.Left) == Black) { sibling.Color = Red; if (sibling.Right != null) { sibling.Right.Color = Black; } RotateLeft(sibling); } // case 6 sibling = node.Sibling; if (sibling == null) { return; } sibling.Color = parent.Color; parent.Color = Black; if (node == parent.Left) { if (sibling.Right != null) { sibling.Right.Color = Black; } RotateLeft(parent); } else { if (sibling.Left != null) { sibling.Left.Color = Black; } RotateRight(parent); } }
static bool GetColorSafe(TreeSegment node) { return(node != null ? node.Color : Black); }
bool TextSegmentTree.Remove(TreeSegment segment) { return(InternalRemove(segment)); }
void TextSegmentTree.Add(TreeSegment segment) { InternalAdd(segment); }
public Interval(TreeSegment node, int start, int end) { this.node = node; this.start = start; this.end = end; }