Exemple #1
0
        private void RightRotate(RBNodeIndexAccess x)
        {
            RBNodeIndexAccess y = x._left;

            x._left = y._right;

            if (y._right != _null)
            {
                y._right._parent = x;
            }
            y._parent = x._parent;
            RBNodeIndexAccess xParent = x._parent;

            if (xParent == _null)
            {
                _root = y;
            }
            else if (x == xParent._right)
            {
                xParent._right = y;
            }
            else
            {
                xParent._left = y;
            }
            y._right  = x;
            x._parent = y;
            y._size   = x._size;
            x._size   = x._left._size + x._right._size + 1;
        }
Exemple #2
0
        public void RB_Delete(RBNodeIndexAccess node)
        {
            if (node == null || _count == 0)
            {
                return;
            }
            _count--;
            RBNodeIndexAccess z = node;
            RBNodeIndexAccess y;

            if (z._left == _null || z._right == _null)
            {
                y = z;
            }
            else
            {
                y = GetSuccessor(z);
            }
            RBNodeIndexAccess parent = y._parent;
            RBNodeIndexAccess x;

            if (y._left != _null)
            {
                x = y._left;
            }
            else
            {
                x = y._right;
            }
            x._parent = y._parent;
            if (y._parent == _null)
            {
                _root = x;
            }
            else if (y == y._parent._left)
            {
                y._parent._left = x;
            }
            else
            {
                y._parent._right = x;
            }
            if (y != z)
            {
                z.Key = y.Key;
                // copy of additional data
            }
            if (parent != _null)
            {
                while (parent != _null)
                {
                    parent._size--;
                    parent = parent._parent;
                }
            }
            if (y._color == RBColor.Black)
            {
                RB_Delete_Fixup(x);
            }
        }
Exemple #3
0
        public object OS_Select(int index)
        {
            if (index < 0 || index > _count)
            {
                return(null);
            }
            index++;
            RBNodeIndexAccess param = _root;
            int r = param._left._size + 1;

            while (index != r)
            {
                if (index < r)
                {
                    param = param._left;
                }
                else
                {
                    param = param._right;
                    index = index - r;
                }
                r = param._left._size + 1;
            }
            return(param.Key);
        }
Exemple #4
0
        private void Insert(RBNodeIndexAccess node)
        {
            RBNodeIndexAccess current     = _root;
            RBNodeIndexAccess parent      = _null;
            IComparable       insertedKey = (IComparable)node.Key;

            while (current != _null)
            {
                current._size++;
                parent = current;
                if (insertedKey.CompareTo(current.Key) < 0)
                {
                    current = current._left;
                }
                else
                {
                    current = current._right;
                }
            }
            node._parent = parent;
            if (parent == _null)
            {
                _root = node;
                return;
            }
            if (insertedKey.CompareTo(parent.Key) < 0)
            {
                parent._left = node;
            }
            else
            {
                parent._right = node;
            }
        }
Exemple #5
0
        public RBNodeIndexAccess GetEqualOrMore(IComparable searchKey, IComparable key2)
        {
            if (_root == _null)
            {
                return(null);
            }

            RBNodeIndexAccess x    = _root;
            RBNodeIndexAccess next = _null;
            //IComparable searchKey = (IComparable)key1;
            int iCompare;

            while (x != _null)
            {
                iCompare = searchKey.CompareTo(x.Key);
                if (iCompare == 0)
                {
                    return(x);
                }
                if (iCompare > 0)
                {
                    x = x._right;
                }
                else
                {
                    next = x;
                    x    = x._left;
                }
            }
            if (next == _null)
            {
                return(null);
            }
            return(next);
        }
Exemple #6
0
 private RBNodeIndexAccess()
 {
     _size   = 0;
     _parent = this;
     _left   = this;
     _right  = this;
 }
Exemple #7
0
 internal RBNodeIndexAccess(IComparable key, RBNodeIndexAccess NullNode)
 {
     _key    = key;
     _size   = 1;
     _parent = NullNode;
     _left   = NullNode;
     _right  = NullNode;
 }
Exemple #8
0
        public void RB_Insert(IComparable key)
        {
            RBNodeIndexAccess node = new RBNodeIndexAccess(key, _null);

            Insert(node);
            RB_Insert(node);
            _count++;
        }
Exemple #9
0
 public RedBlackTreeWithIndexAccess( )
 {
     _null         = RBNodeIndexAccess.GetNull();
     _null.Key     = "sentinel";
     _null._size   = 0;
     _null._left   = _null;
     _null._right  = _null;
     _null._parent = _null;
     _root         = _null;
 }
Exemple #10
0
 private void RB_Insert(RBNodeIndexAccess x)
 {
     x._color = RBColor.Red;
     while (x != _root && x._parent._color == RBColor.Red)
     {
         RBNodeIndexAccess xParent       = x._parent;
         RBNodeIndexAccess xParentParent = xParent._parent;
         if (xParent == xParentParent._left)
         {
             RBNodeIndexAccess y = xParentParent._right;
             if (/*y != _null && */ y._color == RBColor.Red)
             {
                 xParent._color       = RBColor.Black;
                 y._color             = RBColor.Black;
                 xParentParent._color = RBColor.Red;
                 x = xParentParent;
             }
             else if (x == xParent._right)
             {
                 x = xParent;
                 LeftRotate(x);
             }
             else
             {
                 xParent._color       = RBColor.Black;
                 xParentParent._color = RBColor.Red;
                 RightRotate(xParentParent);
             }
         }
         else
         {
             RBNodeIndexAccess y = xParentParent._left;
             if (/*y != _null && */ y._color == RBColor.Red)
             {
                 xParent._color       = RBColor.Black;
                 y._color             = RBColor.Black;
                 xParentParent._color = RBColor.Red;
                 x = xParentParent;
             }
             else if (x == xParent._left)
             {
                 x = xParent;
                 RightRotate(x);
             }
             else
             {
                 xParent._color       = RBColor.Black;
                 xParentParent._color = RBColor.Red;
                 LeftRotate(xParentParent);
             }
         }
     }
     _root._color = RBColor.Black;
 }
Exemple #11
0
 private RBNodeIndexAccess GetMinimum(RBNodeIndexAccess x)
 {
     if (_root == _null)
     {
         return(null);
     }
     while (x._left != _null)
     {
         x = x._left;
     }
     return(x);
 }
Exemple #12
0
 private RBNodeIndexAccess GetMaximum(RBNodeIndexAccess x)
 {
     if (_root == _null)
     {
         return(null);
     }
     while (x._right != _null)
     {
         x = x._right;
     }
     return(x);
 }
Exemple #13
0
        public RBNodeIndexAccess GetNext(RBNodeIndexAccess x)
        {
            RBNodeIndexAccess node = GetSuccessor(x);

            if (node != _null)
            {
                return(node);
            }
            else
            {
                return(null);
            }
        }
Exemple #14
0
        public RBNodeIndexAccess GetPrevious(RBNodeIndexAccess x)
        {
            RBNodeIndexAccess node = GetPredecessor(x);

            if (node != _null)
            {
                return(node);
            }
            else
            {
                return(null);
            }
        }
Exemple #15
0
        private void InOrderEnum(INodeFetcherIndexAccess fetcher, RBNodeIndexAccess node)
        {
            fetcher.NodeFetched(node);

            if (node._left != _null)
            {
                InOrderEnum(fetcher, node._left);
            }
            if (node._right != _null)
            {
                InOrderEnum(fetcher, node._right);
            }
        }
Exemple #16
0
        private RBNodeIndexAccess GetSuccessor(RBNodeIndexAccess x)
        {
            if (x._right != _null)
            {
                return(GetMinimum(x._right));
            }
            RBNodeIndexAccess y = x._parent;

            while (y != _null && x == y._right)
            {
                x = y;
                y = y._parent;
            }
            return(y);
        }
Exemple #17
0
        private RBNodeIndexAccess GetPredecessor(RBNodeIndexAccess x)
        {
            if (x._left != _null)
            {
                return(GetMaximum(x._left));
            }
            RBNodeIndexAccess y = x._parent;

            while (y != _null && x == y._left)
            {
                x = y;
                y = y._parent;
            }
            return(y);
        }
Exemple #18
0
        public bool SearchOrInsert(IComparable insertedKey, out RBNodeIndexAccess foundOrNew)
        {
            RBNodeIndexAccess current = _root;
            RBNodeIndexAccess parent  = _null;

            while (current != _null)
            {
                parent = current;
                int iCompare = insertedKey.CompareTo(current.Key);
                if (iCompare < 0)
                {
                    current = current._left;
                }
                else if (iCompare > 0)
                {
                    current = current._right;
                }
                else if (iCompare == 0)
                {
                    foundOrNew = current;
                    return(true);
                }
            }
            _count++;
            foundOrNew         = new RBNodeIndexAccess(insertedKey, _null);
            foundOrNew._parent = parent;
            if (parent == _null)
            {
                _root = foundOrNew;
                RB_Insert(foundOrNew);
                return(false);
            }
            if (insertedKey.CompareTo(parent.Key) < 0)
            {
                parent._left = foundOrNew;
            }
            else
            {
                parent._right = foundOrNew;
            }
            while (parent != _null)
            {
                parent._size++;
                parent = parent._parent;
            }
            RB_Insert(foundOrNew);
            return(false);
        }
Exemple #19
0
        public RBNodeIndexAccess Search(IComparable searchKey)
        {
            RBNodeIndexAccess x = _root;
            int iCompare;

            while (x != _null)
            {
                iCompare = searchKey.CompareTo(x.Key);
                if (iCompare == 0)
                {
                    return(x);
                }
                x = (iCompare < 0) ? x._left : x._right;
            }
            return(null);
        }
Exemple #20
0
        private int SearchIndex(IComparable searchKey, out RBNodeIndexAccess found)
        {
            RBNodeIndexAccess x    = _root;
            RBNodeIndexAccess prev = _null;
            RBNodeIndexAccess next = _null;
            int  iCompare          = searchKey.CompareTo(x.Key);
            bool bSearch           = (x != _null && iCompare != 0);

            while (bSearch)
            {
                if (iCompare < 0)
                {
                    if (x._left == _null)
                    {
                        prev = x;
                    }
                    x = x._left;
                }
                else
                {
                    if (x._right == _null)
                    {
                        next = x;
                    }
                    x = x._right;
                }
                if (x != _null)
                {
                    iCompare = searchKey.CompareTo(x.Key);
                }
                bSearch = (x != _null && iCompare != 0);
            }
            if (x == _null)
            {
                if (prev == _null)
                {
                    found = next;
                    return(1);
                }
                found = prev;
                return(-1);
            }
            found = x;
            return(0);
        }
Exemple #21
0
        public int OS_Rank(RBNodeIndexAccess node)
        {
            if (node == null || node._left == null)
            {
                return(-1);
            }

            int r = node._left._size + 1;
            RBNodeIndexAccess y = node;

            while (y != _root)
            {
                if (y == y._parent._right)
                {
                    r = r + y._parent._left._size + 1;
                }
                y = y._parent;
            }
            return(r - 1);
        }
Exemple #22
0
 private void InorderPrint(RBNodeIndexAccess node)
 {
     /*
      * if ( node != _null )
      * {
      *  if ( node.Key.Equals( _root.Key ) )
      *  {
      *      Trace.WriteLine("___ root ___");
      *      Console.WriteLine("___ root ___");
      *  }
      *  node.Print();
      *  if ( node.Key.Equals( _root.Key ) )
      *  {
      *      Trace.WriteLine("___ root ___");
      *      Console.WriteLine("___ root ___");
      *  }
      *
      *  if ( node._left != _null )
      *  {
      *      Trace.Indent();
      *      Trace.WriteLine("go left ___ begin");
      *      Console.WriteLine("go left ___ begin");
      *      InorderPrint( node._left );
      *      Trace.WriteLine("go left ___ end");
      *      Console.WriteLine("go left ___ end");
      *      Trace.Unindent();
      *  }
      *  if ( node._right != _null )
      *  {
      *      Trace.Indent();
      *      Trace.WriteLine("go right ___ begin");
      *      Console.WriteLine("go right ___ begin");
      *      InorderPrint( node._right );
      *      Trace.WriteLine("go right ___ end");
      *      Console.WriteLine("go right ___ end");
      *      Trace.Unindent();
      *  }
      * }
      */
 }
Exemple #23
0
        public int OS_Rank(IComparable k)
        {
            RBNodeIndexAccess x = Search(k);

            return(OS_Rank(x));
        }
Exemple #24
0
 internal RBRange(RedBlackTreeWithIndexAccess tree, IComparable key1, IComparable key2)
 {
     RBNodeIndexAccess node1 = tree.Search(key1);
     int index1 = tree.OS_Rank(node1);
 }
Exemple #25
0
 private void RB_Delete_Fixup(RBNodeIndexAccess x)
 {
     while (x != _root && x._color == RBColor.Black)
     {
         RBNodeIndexAccess w;
         if (x == x._parent._left)
         {
             w = x._parent._right;
             if (w._color == RBColor.Red)
             {
                 w._color         = RBColor.Black;
                 x._parent._color = RBColor.Red;
                 LeftRotate(x._parent);
                 w = x._parent._right;
             }
             if (w._left._color == RBColor.Black && w._right._color == RBColor.Black)
             {
                 w._color = RBColor.Red;
                 x        = x._parent;
             }
             else if (w._right._color == RBColor.Black)
             {
                 w._left._color = RBColor.Black;
                 w._color       = RBColor.Red;
                 RightRotate(w);
                 w = x._parent._right;
             }
             else
             {
                 w._color         = x._parent._color;
                 x._parent._color = RBColor.Black;
                 w._right._color  = RBColor.Black;
                 LeftRotate(x._parent);
                 x = _root;
             }
         }
         else
         {
             w = x._parent._left;
             if (w._color == RBColor.Red)
             {
                 w._color         = RBColor.Black;
                 x._parent._color = RBColor.Red;
                 RightRotate(x._parent);
                 w = x._parent._left;
             }
             if (w._right._color == RBColor.Black && w._left._color == RBColor.Black)
             {
                 w._color = RBColor.Red;
                 x        = x._parent;
             }
             else if (w._left._color == RBColor.Black)
             {
                 w._right._color = RBColor.Black;
                 w._color        = RBColor.Red;
                 LeftRotate(w);
                 w = x._parent._left;
             }
             else
             {
                 w._color         = x._parent._color;
                 x._parent._color = RBColor.Black;
                 w._left._color   = RBColor.Black;
                 RightRotate(x._parent);
                 x = _root;
             }
         }
     }
     x._color = RBColor.Black;
 }