Exemplo n.º 1
0
        /// <summary>
        /// Rebalances the root after a black node was deleted in the right sub-tree
        /// </summary>
        /// <param name="root"></param>
        /// <param name="done"></param>
        /// <returns></returns>
        private RedBlackNode <T> DeleteRebalanceRight(RedBlackNode <T> root, ref bool done)
        {
            RedBlackNode <T> parent  = root;
            RedBlackNode <T> sibling = root.Left;

            // Rotation to reduce the red sibling case to the easier to handle black sibling case
            if (IsNodeRed(sibling))
            {
                root    = root.RotateRight();
                sibling = parent.Left;
            }

            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.Left))
                    {
                        parent = parent.RotateRight();
                    }
                    else
                    {
                        parent.Left = parent.Left.RotateLeft();
                        parent      = parent.RotateRight();
                    }

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

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

                    done = true;
                }
            }

            return(root);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deals with the case where after inserting a node to the left we have a red node as a left sub-child of its parent and has another red node as a right subchild.
        /// </summary>
        private RedBlackNode <T> Insert_Case3_LeftRightReds(RedBlackNode <T> root)
        {
            root.Left = root.Left.RotateLeft();
            root      = root.RotateRight();

            return(root);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Deals with the case where after an insert to the left we have two red nodes as left sub-children of each-other
        /// </summary>
        private RedBlackNode <T> Insert_Case2_TwoLeftReds(RedBlackNode <T> root)
        {
            if (IsNodeRed(root.Left.Left))
            {
                root = root.RotateRight();
            }
            else if (IsNodeRed(root.Left.Right))
            {
                root = Insert_Case3_LeftRightReds(root);
            }

            return(root);
        }