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); }