Ejemplo n.º 1
0
 /// <summary><para>Swaps the current <see cref="RBNode{T}"/>'s <see cref="Parent"/> with <paramref name="newParent"/>.</para></summary>
 /// <param name="newParent"><para>The replacement for the current <see cref="RBNode{T}"/>'s <see cref="Parent"/>.</para></param>
 private void RotateParents(RBNode <T> newParent)
 {
     newParent.Parent = Parent;
     Parent           = newParent;
     if (this == newParent.Parent?.Left)
     {
         newParent.Parent.Left = newParent;
     }
     else if (this == newParent.Parent?.Right)
     {
         newParent.Parent.Right = newParent;
     }
 }
Ejemplo n.º 2
0
 /// <summary><para>Rotates the tree right with the current <see cref="RBNode{T}"/> as the pivot point.</para></summary>
 public void RotateRight()
 {
     if (Left != null)
     {
         RBNode <T> newParent = Left;
         Left            = newParent.Right;
         newParent.Right = this;
         RotateParents(newParent);
         if (Left != null)
         {
             Left.Parent = this;
         }
     }
 }
Ejemplo n.º 3
0
 /// <summary><para>Rotates the tree left with the current <see cref="RBNode{T}"/> as the pivot point.</para></summary>
 public void RotateLeft()
 {
     if (Right != null)
     {
         RBNode <T> newParent = Right;
         Right          = newParent.Left;
         newParent.Left = this;
         RotateParents(newParent);
         if (Right != null)
         {
             Right.Parent = this;
         }
     }
 }
Ejemplo n.º 4
0
        /// <summary><para>Performs a rebalance of the <see cref="RBTree{T}"/> starting with the current node.</para></summary>
        private void InsertRebalanceCondition3Step2()
        {
            RBNode <T> grandParent = GrandParent;

            if (this == Parent.Left)
            {
                grandParent.RotateRight();
            }
            else if (this == Parent.Right)
            {
                grandParent.RotateLeft();
            }
            Parent.Color      = Colors.Black;
            grandParent.Color = Colors.Red;
        }
Ejemplo n.º 5
0
        /// <summary><para>Performs a rebalance of the <see cref="RBTree{T}"/> starting with the current node.</para></summary>
        private void InsertRebalanceCondition3Step1()
        {
            RBNode <T> nextNode = this;

            if (this == GrandParent?.Left?.Right)
            {
                Parent.RotateLeft();
                nextNode = Left;
            }
            else if (this == GrandParent?.Right?.Left)
            {
                Parent.RotateRight();
                nextNode = Right;
            }
            nextNode.InsertRebalanceCondition3Step2();
        }
Ejemplo n.º 6
0
 /// <summary><para>Replaces the current <see cref="RBNode{T}"/> with <paramref name="replacementNode"/>.</para></summary>
 /// <param name="replacementNode"><para>The <see cref="RBNode{T}"/> or null to replace the current <see cref="RBNode{T}"/> with.</para></param>
 /// <returns><para>The <see cref="RBNode{T}"/> <paramref name="replacementNode"/>.</para></returns>
 public RBNode <T> Replace(RBNode <T> replacementNode = null)
 {
     if (Parent != null)
     {
         if (Parent.Left == this)
         {
             Parent.Left = replacementNode;
         }
         else
         {
             Parent.Right = replacementNode;
         }
     }
     if (replacementNode != null)
     {
         replacementNode.Parent = Parent;
     }
     return(replacementNode);
 }
Ejemplo n.º 7
0
        /// <summary><para>Clones the current <see cref="RBNode{T}"/> and all of its children, returning the cloned result.</para></summary>
        /// <param name="tree"><para>An optional new tree for the <see cref="RBNode{T}"/>.</para></param>
        /// <param name="parent"><para>An optional new parent for the <see cref="RBNode{T}"/>.</para></param>
        /// <returns><para>The cloned <see cref="RBNode{T}"/>.</para></returns>
        public RBNode <T> Clone(RBTree <T> tree = null, RBNode <T> parent = null) => new RBNode <T>
        {
            Color = Color, Parent = parent, Tree = tree ?? Tree, Value = Value, Left = Left, Right = Right
        }

        .CloneChildren();