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

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

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

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

                    done = true;
                }
            }

            return(root);
        }
Example #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);
        }