示例#1
0
 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();
     }
 }
示例#2
0
            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);
                }
            }
示例#3
0
 static bool GetColorSafe(TreeSegment node)
 {
     return(node != null ? node.Color : Black);
 }
示例#4
0
 bool TextSegmentTree.Remove(TreeSegment segment)
 {
     return(InternalRemove(segment));
 }
示例#5
0
 void TextSegmentTree.Add(TreeSegment segment)
 {
     InternalAdd(segment);
 }