示例#1
0
文件: RBTree.cs 项目: rassilon/NModel
 internal RBNode treeMaximum(RBNode x)
 {
     while (x.right != NIL)
     {
         x = x.right;
     }
     return(x);
 }
示例#2
0
文件: RBTree.cs 项目: rassilon/NModel
 internal RBNode(RBTree.Color color, IComparable item, RBNode p, RBNode left, RBNode right)
 {
     this.color = color;
     this.p     = p;
     this.left  = left;
     this.right = right;
     this.item  = item;
 }
示例#3
0
文件: RBTree.cs 项目: juhan/NModel
 internal RBNode(RBTree.Color color, IComparable item, RBNode p, RBNode left, RBNode right)
 {
     this.color=color;
     this.p=p;
     this.left=left;
     this.right=right;
     this.item=item;
 }
示例#4
0
文件: RBTree.cs 项目: rassilon/NModel
 internal RBNode treeMinimum(RBNode x)
 {
     while (x.left != NIL)
     {
         x = x.left;
     }
     return(x);
 }
示例#5
0
文件: RBTree.cs 项目: rassilon/NModel
        internal RBNode find(RBNode x, IComparable i)
        {
            while (x != NIL && i.CompareTo(x.item) != 0)
            {
                x = i.CompareTo(x.item) < 0?x.left:x.right;
            }

            return(x);
        }
示例#6
0
文件: RBTree.cs 项目: rassilon/NModel
        void insertPrivate(RBNode x)
        {
            count++;
            x.color = Color.R;
            while (x != root && x.p.color == Color.R)
            {
                if (x.p == x.p.p.left)
                {
                    RBNode y = x.p.p.right;
                    if (y.color == Color.R)
                    {
                        x.p.color   = Color.B;
                        y.color     = Color.B;
                        x.p.p.color = Color.R;
                        x           = x.p.p;
                    }
                    else
                    {
                        if (x == x.p.right)
                        {
                            x = x.p;
                            LeftRotate(x);
                        }
                        x.p.color   = Color.B;
                        x.p.p.color = Color.R;
                        RightRotate(x.p.p);
                    }
                }
                else
                {
                    RBNode y = x.p.p.left;
                    if (y.color == Color.R)
                    {
                        x.p.color   = Color.B;
                        y.color     = Color.B;
                        x.p.p.color = Color.R;
                        x           = x.p.p;
                    }
                    else
                    {
                        if (x == x.p.left)
                        {
                            x = x.p;
                            RightRotate(x);
                        }
                        x.p.color   = Color.B;
                        x.p.p.color = Color.R;
                        LeftRotate(x.p.p);
                    }
                }
            }

            root.color = Color.B;

            //checkTheTree();
        }
示例#7
0
文件: RBTree.cs 项目: rassilon/NModel
        internal RBNode delete(IComparable i)
        {
            RBNode n = find(i);

            if (n != NIL)
            {
                count--;
                return(deleteTree(n));
            }
            return(NIL);
        }
示例#8
0
文件: RBTree.cs 项目: rassilon/NModel
        internal void insertIfUniq(IComparable v)
        {
            RBNode x = TreeInsertIfUniq(v); //TreeInsertIfUniq(root,v);

            if (x == NIL)
            {
                return;
            }

            insertPrivate(x);
        }
示例#9
0
        internal int del_min()
        {
            if (isEmpty())
            {
                throw new InvalidOperationException("deleting from an empty queue");
            }

            RBNode node = tree.treeMinimum();

            tree.deleteTree(node);
            return((node.item as PQMember).o);
        }
示例#10
0
        /// <summary>
        /// sets the object priority to c
        /// </summary>
        internal void set_priority(int o, int oldPriority, int newPriority)
        {
            PQMember pqm = new PQMember(o, oldPriority);

            RBNode node = tree.delete(pqm);

            if (node == tree.NIL)
            {
                throw new InvalidOperationException("in set_priority");
            }

            pqm.priority = newPriority;

            tree.insertIfUniq(pqm);
        }
示例#11
0
文件: RBTree.cs 项目: rassilon/NModel
        internal RBNode treeSuccessor(RBNode x)
        {
            if (x.right != NIL)
            {
                return(treeMinimum(x.right));
            }
            RBNode y = x.p;

            while (y != NIL && x == y.right)
            {
                x = y;
                y = y.p;
            }
            return(y);
        }
示例#12
0
文件: RBTree.cs 项目: rassilon/NModel
        public bool MoveNext()
        {
            if (tree.isEmpty())
            {
                return(false);
            }

            if (initialState == true)
            {
                initialState = false;
                c            = tree.treeMinimum();
            }
            else
            {
                c = tree.treeSuccessor(c);
            }
            return(c != tree.NIL);
        }
示例#13
0
文件: RBTree.cs 项目: rassilon/NModel
        void checkColors(RBNode x)
        {
            if (x == NIL)
            {
                return;
            }
            //property 3
            if (x.color == Color.R)
            {
                if (x.left.color == Color.R || x.right.color == Color.R)
                {
                    throw new InvalidOperationException("colors");
                }
            }

            checkColors(x.left);
            checkColors(x.right);
        }
示例#14
0
文件: RBTree.cs 项目: rassilon/NModel
        void checkBlacks(RBNode node, int nnow, ref int blacks)
        {
            if (node != NIL)
            {
                RBNode l = node.left, r = node.right;

                if (l != NIL)
                {
                    if ((l.item as IComparable).CompareTo(node.item) >= 0)
                    {
                        throw new InvalidOperationException("order");
                    }
                }

                if (r != NIL)
                {
                    if ((r.item as IComparable).CompareTo(node.item) <= 0)
                    {
                        throw new InvalidOperationException("order");
                    }
                }
            }

            if (node == NIL)
            {
                if (blacks != nnow + 1 && blacks != -1)
                {
                    throw new InvalidOperationException("blacks");
                }

                blacks = nnow + 1;
            }
            else if (node.color == Color.B)
            {
                checkBlacks(node.left, nnow + 1, ref blacks);
                checkBlacks(node.right, nnow + 1, ref blacks);
            }
            else
            {
                checkBlacks(node.left, nnow, ref blacks);
                checkBlacks(node.right, nnow, ref blacks);
            }
        }
示例#15
0
文件: RBTree.cs 项目: juhan/NModel
 void deleteFixup(RBNode x)
 {
     while(x!=root && x.color==Color.B)
     {
         if(x==x.p.left)
         {
             RBNode w=x.p.right;
             if(w.color==Color.R)
             {
                 w.color=Color.B;
                 x.p.color=Color.R;
                 LeftRotate(x.p);
                 w=x.p.right;
             }
             if(w.left.color==Color.B && w.right.color==Color.B)
             {
                 w.color=Color.R;
                 x=x.p;
             }
             else
             {
                 if (w.right.color==Color.B)
                   {
                       w.left.color=Color.B;
                       w.color=Color.R;
                       RightRotate(w);
                       w=x.p.right;
                   }
                 w.color=x.p.color;
                 x.p.color=Color.B;
                 w.right.color=Color.B;
                 LeftRotate(x.p);
                 x=root;}
         }
         else
         {
             RBNode w=x.p.left;
             if(w.color==Color.R)
             {
                 w.color=Color.B;
                 x.p.color=Color.R;
                 RightRotate(x.p);
                 w=x.p.left;
             }
             if(w.right.color==Color.B && w.left.color==Color.B)
             {
                 w.color=Color.R;
                 x=x.p;
             }
             else
             {
                 if (w.left.color==Color.B)
                 {
                     w.right.color=Color.B;
                     w.color=Color.R;
                     LeftRotate(w);
                     w=x.p.left;
                 }
                 w.color=x.p.color;
                 x.p.color=Color.B;
                 w.left.color=Color.B;
                 RightRotate(x.p);
                 x=root;
             }
         }
     }
     x.color=Color.B;
 }
示例#16
0
文件: RBTree.cs 项目: juhan/NModel
        void checkColors(RBNode x)
        {
            if(x==NIL)
                return;
            //property 3
            if(x.color==Color.R)
                if(x.left.color==Color.R || x.right.color==Color.R)
                    throw new InvalidOperationException("colors");

            checkColors(x.left);
            checkColors(x.right);
        }
示例#17
0
文件: RBTree.cs 项目: juhan/NModel
        void checkBlacks(RBNode node, int nnow, ref int blacks)
        {
            if (node!=NIL)
            {
                RBNode l=node.left,r=node.right;

                if(l!=NIL)
                    if(( l.item as IComparable).CompareTo(node.item)>=0)
                        throw new InvalidOperationException("order");

                if(r!=NIL)
                    if( (r.item as IComparable).CompareTo(node.item)<=0)
                        throw new InvalidOperationException("order");

            }

            if(node==NIL)
            {
                if (blacks!=nnow+1 && blacks!=-1)
                    throw new InvalidOperationException("blacks");

                blacks=nnow+1;

            }
            else if(node.color==Color.B)
            {
                checkBlacks(node.left,nnow+1,ref blacks);
                checkBlacks(node.right,nnow+1,ref blacks);
            }
            else
            {
                checkBlacks(node.left,nnow,ref blacks);
                checkBlacks(node.right,nnow,ref blacks);
            }
        }
示例#18
0
文件: RBTree.cs 项目: juhan/NModel
 internal RBNode treeSuccessor(RBNode x)
 {
     if(x.right!=NIL)
         return treeMinimum(x.right);
     RBNode y=x.p;
     while(y!=NIL && x==y.right)
     {
         x=y;
         y=y.p;
     }
     return y;
 }
示例#19
0
文件: RBTree.cs 项目: rassilon/NModel
 internal RBTree()
 {
     root = NIL = new RBNode(Color.B);
 }
示例#20
0
文件: RBTree.cs 项目: juhan/NModel
        public bool MoveNext()
        {
            if(tree.isEmpty())
                return false;

            if(initialState==true)
            {
                initialState=false;
                c=tree.treeMinimum();
            }
            else
            {
                c=tree.treeSuccessor(c);
            }
            return c!=tree.NIL;
        }
示例#21
0
文件: RBTree.cs 项目: juhan/NModel
        void RightRotate(RBNode x)
        {
            RBNode y=x.left;
            x.left=y.right;
            if(y.right!=NIL)
                y.right.p=x;
            y.p=x.p;
            if(x.p==NIL)
                this.root=y;
            else if (x==x.p.right)
                x.p.right=y;
            else
                x.p.left=y;

            y.right=x;
            x.p=y;
        }
示例#22
0
文件: RBTree.cs 项目: juhan/NModel
        internal RBNode deleteTree(RBNode z)
        {
            if(z==NIL)
                throw new ArgumentException("z cannot be NIL");
            RBNode y,x;
            if (z.left == NIL || z.right == NIL)
            {
                /* y has a NIL node as a child */
                y = z;
            }
            else
            {
                /* find tree successor with a NIL node as a child */
                y = z.right;
                while (y.left != NIL) y = y.left;
            }

            /* x is y's only child */
            if (y.left != NIL)
                x = y.left;
            else
                x = y.right;

            x.p=y.p;
            if(y.p==NIL)
                root=x;
            else
            {
                if(y==y.p.left)
                    y.p.left=x;
                else
                    y.p.right=x;
            }
            if(y!=z)
                z.item=y.item;
            if(y.color==Color.B)
                deleteFixup(x);

            //	checkTheTree();

            return y;
        }
示例#23
0
文件: RBTree.cs 项目: rassilon/NModel
 void deleteFixup(RBNode x)
 {
     while (x != root && x.color == Color.B)
     {
         if (x == x.p.left)
         {
             RBNode w = x.p.right;
             if (w.color == Color.R)
             {
                 w.color   = Color.B;
                 x.p.color = Color.R;
                 LeftRotate(x.p);
                 w = x.p.right;
             }
             if (w.left.color == Color.B && w.right.color == Color.B)
             {
                 w.color = Color.R;
                 x       = x.p;
             }
             else
             {
                 if (w.right.color == Color.B)
                 {
                     w.left.color = Color.B;
                     w.color      = Color.R;
                     RightRotate(w);
                     w = x.p.right;
                 }
                 w.color       = x.p.color;
                 x.p.color     = Color.B;
                 w.right.color = Color.B;
                 LeftRotate(x.p);
                 x = root;
             }
         }
         else
         {
             RBNode w = x.p.left;
             if (w.color == Color.R)
             {
                 w.color   = Color.B;
                 x.p.color = Color.R;
                 RightRotate(x.p);
                 w = x.p.left;
             }
             if (w.right.color == Color.B && w.left.color == Color.B)
             {
                 w.color = Color.R;
                 x       = x.p;
             }
             else
             {
                 if (w.left.color == Color.B)
                 {
                     w.right.color = Color.B;
                     w.color       = Color.R;
                     LeftRotate(w);
                     w = x.p.left;
                 }
                 w.color      = x.p.color;
                 x.p.color    = Color.B;
                 w.left.color = Color.B;
                 RightRotate(x.p);
                 x = root;
             }
         }
     }
     x.color = Color.B;
 }
示例#24
0
文件: RBTree.cs 项目: rassilon/NModel
        internal RBNode deleteTree(RBNode z)
        {
            if (z == NIL)
            {
                throw new ArgumentException("z cannot be NIL");
            }
            RBNode y, x;

            if (z.left == NIL || z.right == NIL)
            {
                /* y has a NIL node as a child */
                y = z;
            }
            else
            {
                /* find tree successor with a NIL node as a child */
                y = z.right;
                while (y.left != NIL)
                {
                    y = y.left;
                }
            }

            /* x is y's only child */
            if (y.left != NIL)
            {
                x = y.left;
            }
            else
            {
                x = y.right;
            }

            x.p = y.p;
            if (y.p == NIL)
            {
                root = x;
            }
            else
            {
                if (y == y.p.left)
                {
                    y.p.left = x;
                }
                else
                {
                    y.p.right = x;
                }
            }
            if (y != z)
            {
                z.item = y.item;
            }
            if (y.color == Color.B)
            {
                deleteFixup(x);
            }

            //	checkTheTree();

            return(y);
        }
示例#25
0
文件: RBTree.cs 项目: juhan/NModel
        internal RBNode find(RBNode x, IComparable i)
        {
            while(x!=NIL && i.CompareTo(x.item)!=0)
                x=i.CompareTo(x.item)<0?x.left:x.right;

            return x;
        }
示例#26
0
文件: RBTree.cs 项目: juhan/NModel
        void insertPrivate(RBNode x)
        {
            count++;
            x.color=Color.R;
            while(x!=root && x.p.color==Color.R)
            {
                if(x.p==x.p.p.left)
                {
                    RBNode y=x.p.p.right;
                    if(y.color==Color.R)
                    {
                        x.p.color=Color.B;
                        y.color=Color.B;
                        x.p.p.color=Color.R;
                        x=x.p.p;
                    }
                    else
                    {
                        if  (x==x.p.right)
                        {
                            x=x.p;
                            LeftRotate(x);
                        }
                        x.p.color=Color.B;
                        x.p.p.color=Color.R;
                        RightRotate(x.p.p);
                    }
                }
                else
                {
                    RBNode y=x.p.p.left;
                    if(y.color==Color.R)
                    {
                        x.p.color=Color.B;
                        y.color=Color.B;
                        x.p.p.color=Color.R;
                        x=x.p.p;
                    }
                    else
                    {
                        if  (x==x.p.left)
                        {
                            x=x.p;
                            RightRotate(x);
                        }
                        x.p.color=Color.B;
                        x.p.p.color=Color.R;
                        LeftRotate(x.p.p);
                    }
                }

            }

            root.color=Color.B;

            //checkTheTree();
        }
示例#27
0
文件: RBTree.cs 项目: juhan/NModel
 internal RBNode treeMaximum(RBNode x)
 {
     while(x.right!=NIL)
         x=x.right;
     return x;
 }
示例#28
0
文件: RBTree.cs 项目: juhan/NModel
        //TreeInsertIfUniq(RBNode node, IComparable z)
        RBNode TreeInsertIfUniq(IComparable z)
        {
            RBNode y=NIL;
            RBNode x=this.root;

            while(x!=NIL)
            {
                y=x;
                int comp=(x.item as IComparable).CompareTo(z);
                if (comp==0)
                    return NIL;
                x=comp>0? x.left:x.right;
            }

            RBNode nz=new RBNode(Color.B,z,y,NIL,NIL);

            if(y==NIL)
                root=nz;
            else if ( z.CompareTo(y.item)<0)
                y.left=nz;
            else
                y.right=nz;

            return nz;
        }
示例#29
0
文件: RBTree.cs 项目: juhan/NModel
 internal RBNode treeMinimum(RBNode x)
 {
     while(x.left!=NIL)
         x=x.left;
     return x;
 }
示例#30
0
文件: RBTree.cs 项目: juhan/NModel
 internal RBTree()
 {
     root=NIL=new RBNode(Color.B);
 }
示例#31
0
文件: RBTree.cs 项目: rassilon/NModel
        internal void insert(IComparable v)
        {
            RBNode x = TreeInsert(v);//TreeInsert(root,v);

            insertPrivate(x);
        }