Example #1
0
        /// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public void Add(T item)
        {
            grandParent      = header;
            parent           = grandParent;
            current          = parent;
            nullNode.Element = item;
            while (current.Element.CompareTo(item) != 0)
            {
                RBNode <T> greatParent = grandParent;
                grandParent = parent;
                parent      = current;
                current     = item.CompareTo(current.Element) < 0 ? current.Left : current.Right;

                if (current.Left.Color == Color.Red && current.Right.Color == Color.Red)
                {
                    HandleReorient(item);
                }
            }

            if (current != nullNode)
            {
                return;
            }
            current = new RBNode <T>(item, nullNode, nullNode);

            if (item.CompareTo(parent.Element) < 0)
            {
                parent.Left = current;
            }
            else
            {
                parent.Right = current;
            }
            HandleReorient(item);
        }
Example #2
0
        private RBNode <T> RotateWithLeftChild(RBNode <T> node)
        {
            var k1 = node.Left;

            node.Left = k1.Right;
            k1.Right  = node;
            return(k1);
        }
Example #3
0
        private RBNode <T> RotateWithRightChild(RBNode <T> k1)
        {
            RBNode <T> k2 = k1.Right;

            k1.Right = k2.Left;
            k2.Left  = k1;
            return(k2);
        }
Example #4
0
 ///<summary>
 ///</summary>
 ///<param name="element"></param>
 public RBTree(T element)
 {
     current        = new RBNode <T>();
     parent         = new RBNode <T>();
     grandParent    = new RBNode <T>();
     greatParent    = new RBNode <T>();
     nullNode       = new RBNode <T>();
     nullNode.Left  = nullNode;
     nullNode.Right = nullNode;
     header         = new RBNode <T>(element)
     {
         Left = nullNode, Right = nullNode
     };
 }
Example #5
0
 private RBNode <T> Rotate(T item, RBNode <T> Parent)
 {
     if (item.CompareTo(Parent.Element) < 0)
     {
         return
             (Parent.Left =
                  item.CompareTo(Parent.Left.Element) < 0
                 ? RotateWithLeftChild(Parent.Left)
                 : RotateWithRightChild(Parent.Left));
     }
     return
         (Parent.Right =
              item.CompareTo(Parent.Right.Element) < 0
             ? RotateWithLeftChild(Parent.Right)
             : RotateWithRightChild(Parent.Right));
 }
Example #6
0
        private IEnumerable <ItemColor <T> > InOrden(RBNode <T> Current)
        {
            if (Current.Left != null)
            {
                InOrden(Current.Left);
            }

            yield return(new ItemColor <T> {
                Element = Current.Element, Color = Current.Color
            });

            if (Current.Right != null)
            {
                InOrden(Current.Right);
            }
        }
Example #7
0
        private void HandleReorient(T item)
        {
            current.Color       = Color.Red;
            current.Left.Color  = Color.Black;
            current.Right.Color = Color.Black;
            if (parent.Color == Color.Red)
            {
                grandParent.Color = Color.Red;

                int x_1 = item.CompareTo(grandParent.Element);
                int x_2 = item.CompareTo(parent.Element);

                if (x_1 < 0 && x_1 != x_2)
                {
                    current       = Rotate(item, grandParent);
                    current.Color = Color.Black;
                }
                header.Right.Color = Color.Black;
            }
        }