Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        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
            var parent  = root;
            var 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.SetColor(Color.Black);
                    sibling.SetColor(Color.Red);
                }
                else
                {
                    var parentIsRed = parent.IsRed;
                    var sameRoot    = root == parent;

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

                    parent.SetIsRed(parentIsRed);
                    parent.Left.SetColor(Color.Black);
                    parent.Right.SetColor(Color.Black);

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

                    done = true;
                }
            }

            return(root);
        }
Beispiel #3
0
        /// <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);
        }