示例#1
0
 //Inserts a RBNode holding a Node item
 public void Insert(long identifier, T item)
 {
     lock (writeLock)
     {
         root       = RBInsert(identifier, root, item);
         root.Color = RB.Black;
         size++;
     }
 }
示例#2
0
        /// <summary>
        /// Insert Algortihm
        /// </summary>
        private RBNode <T> RBInsert(long identifier, RBNode <T> n, T item)
        {
            if (n == null)
            {
                return(new RBNode <T>(identifier, item, null));
            }

            //flip
            if (n.Left != null && n.Right != null &&
                n.Left.Color == RB.Red && n.Right.Color == RB.Red)
            {
                Flip(n);
            }

            //go left branch
            if (identifier < n.ID)
            {
                n.Left        = RBInsert(identifier, n.Left, item);
                n.Left.Parent = n;
                if (n.Color == RB.Red && n.Left.Color == RB.Red && n.Parent.Right == n)
                {
                    n = RotateLeft(n);
                }
                if (n.Left != null && n.Left.Color == RB.Red &&
                    n.Left.Left != null && n.Left.Left.Color == RB.Red)
                {
                    n             = RotateLeft(n);
                    n.Color       = RB.Black;
                    n.Right.Color = RB.Red;
                }
            }

            //go right branch
            else
            {
                n.Right        = RBInsert(identifier, n.Right, item);
                n.Right.Parent = n;
                if (n.Color == RB.Red && n.Right.Color == RB.Red && n.Parent.Left == n)
                {
                    n = RotateRight(n);
                }
                if (n.Right != null && n.Right.Color == RB.Red &&
                    n.Right.Right != null && n.Right.Right.Color == RB.Red)
                {
                    n            = RotateRight(n);
                    n.Color      = RB.Black;
                    n.Left.Color = RB.Red;
                }
            }

            return(n);
        }
示例#3
0
        //Left rotation around node r
        private RBNode <T> RotateLeft(RBNode <T> r)
        {
            RBNode <T> n = r.Left;

            n.Parent = r.Parent;
            r.Left   = n.Right;

            if (r.Left != null)
            {
                r.Left.Parent = r;
            }

            r.Parent = n;
            n.Right  = r;

            return(n);
        }
示例#4
0
 public RBTree()
 {
     this.root = null;
 }
示例#5
0
 //flips node n and it's childs
 private void Flip(RBNode <T> n)
 {
     n.Color       = RB.Red;
     n.Left.Color  = RB.Black;
     n.Right.Color = RB.Black;
 }