コード例 #1
0
ファイル: RedBlackTree.cs プロジェクト: war-man/Common
        private RedBlackNode <T> DeleteRebalanceLeft(RedBlackNode <T> root, ref bool done)
        {
            // Rotation to reduce the red sibling case to the easier to handle black sibling case
            RedBlackNode <T> parent  = root;
            RedBlackNode <T> sibling = root.Right;

            if (IsNodeRed(sibling))
            {
                root    = root.RotateLeft();
                sibling = parent.Right;
            }

            if (sibling != null)
            {
                if (!IsNodeRed(sibling.Left) && !IsNodeRed(sibling.Right))
                {
                    if (IsNodeRed(parent))
                    {
                        done = true;
                    }

                    parent.SetColour(Colour.Black);
                    sibling.SetColour(Colour.Red);
                }
                else
                {
                    bool parentIsRed = parent.IsRed;
                    bool sameRoot    = root == parent;

                    if (IsNodeRed(sibling.Right))
                    {
                        parent = parent.RotateLeft();
                    }
                    else
                    {
                        parent.Right = parent.Right.RotateRight();
                        parent       = parent.RotateLeft();
                    }

                    parent.SetIsRed(parentIsRed);
                    parent.Left.SetColour(Colour.Black);
                    parent.Right.SetColour(Colour.Black);

                    if (sameRoot)
                    {
                        root = parent;
                    }
                    else
                    {
                        root.Left = parent;
                    }

                    done = true;
                }
            }

            return(root);
        }
コード例 #2
0
ファイル: RedBlackTree.cs プロジェクト: war-man/Common
        /// <summary>
        /// Deals with the case where after inserting a node to the right we have a red node as a right sub-child of its parent and has another red node as a left subchild.
        /// </summary>
        private RedBlackNode <T> Insert_Case3_RightLeftReds(RedBlackNode <T> root)
        {
            root.Right = root.Right.RotateRight();
            root       = root.RotateLeft();

            return(root);
        }
コード例 #3
0
ファイル: RedBlackTree.cs プロジェクト: war-man/Common
        /// <summary>
        /// Deals with the case where after an insert to the right we have two red nodes as right sub-children of each-other
        /// </summary>
        private RedBlackNode <T> Insert_Case2_TwoRightReds(RedBlackNode <T> root)
        {
            if (IsNodeRed(root.Right.Right))
            {
                root = root.RotateLeft();
            }
            else if (IsNodeRed(root.Right.Left))
            {
                root = Insert_Case3_RightLeftReds(root);
            }

            return(root);
        }