Exemplo n.º 1
0
 public RBNode(string data, RBNode parent = null)
 {
     this.data   = data;
     _color      = RBColor.Red;
     right       = left = null;
     this.parent = parent;
 }
Exemplo n.º 2
0
 internal RBNode(RBColor color, T item, RBNode <T> p, RBNode <T> left, RBNode <T> right)
 {
     this.color  = color;
     this.parent = p;
     this.left   = left;
     this.right  = right;
     this.Item   = item;
 }
Exemplo n.º 3
0
 /// <summary>
 /// 右部分木のエントリー削除に伴う赤黒木の修正(4つのパターン
 /// 戻り値は修正された木。auzに付加情報を返す
 /// </summary>
 private Node BalanceR(Node t, Trio aux)
 {
     //BalanceLと左右対称
     if (!aux.change)
     {
         return(t);
     }
     else if (IsB(t.lst) && IsR(t.lst.rst))
     {
         RBColor rb = t.color;
         t           = RotateLR(t);
         t.color     = rb;
         t.rst.color = RBColor.B;
         aux.change  = false;
     }
     else if (IsB(t.lst) && IsR(t.lst.lst))
     {
         RBColor rb = t.color;
         t           = RotateR(t);
         t.color     = rb;
         t.lst.color = t.rst.color = RBColor.B;
         aux.change  = false;
     }
     else if (IsB(t.lst))
     {
         RBColor rb = t.color;
         t.color     = RBColor.B;
         t.lst.color = RBColor.R;
         aux.change  = rb == RBColor.B;
     }
     else if (IsR(t.lst))
     {
         t           = RotateR(t);
         t.color     = RBColor.B;
         t.rst.color = RBColor.R;
         t.rst       = BalanceR(t.rst, aux);
         aux.change  = false;
     }
     else
     {
         throw new Exception("(L) This program is buggy");
     }
     return(t);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 左部分木のエントリー削除に伴う赤黒木の修正(4つのパターン
 /// 戻り値は修正された木。auxに付加情報を返す
 /// </summary>
 private Node BalanceL(Node t, Trio aux)
 {
     if (!aux.change)
     {
         return(t);
     }
     else if (IsB(t.rst) && IsR(t.rst.lst))
     {
         RBColor rb = t.color;
         t           = RotateRL(t);
         t.color     = rb;
         t.lst.color = RBColor.B;                        //左側に黒が追加されて修正
         aux.change  = false;
     }
     else if (IsB(t.rst) && IsR(t.rst.rst))
     {
         RBColor rb = t.color;
         t           = RotateL(t);
         t.color     = rb;
         t.lst.color = t.rst.color = RBColor.B;                  //左側に黒が追加されて修正
         aux.change  = false;
     }
     else if (IsB(t.rst))
     {
         RBColor rb = t.color;
         t.color     = RBColor.B;
         t.rst.color = RBColor.R;
         aux.change  = rb == RBColor.B;                  //tが黒の場合は黒が減るので修正フラグを立て木を遡る(親で何とかする
     }
     else if (IsR(t.rst))
     {
         t           = RotateL(t);
         t.color     = RBColor.B;
         t.lst.color = RBColor.R;
         t.lst       = BalanceL(t.lst, aux);             //再帰は必ず一段で終わる
         aux.change  = false;
     }
     else
     {
         throw new Exception("(L) This program is buggy");
     }
     return(t);
 }
Exemplo n.º 5
0
 internal RBNode(RBColor color)
 {
     this.color = color;
 }
Exemplo n.º 6
0
 public RBNode(RBColor color, K key, V value)
 {
     this.color = color;
     this.key   = key;
     this.value = value;
 }
Exemplo n.º 7
0
            private void Delete(NodeRB node)
            {
                NodeRB  DeletingNode = node;
                NodeRB  ZamenaItem;
                RBColor FirstColor = node.Color;

                bool?myPosition;

                if (node.Parent == null)
                {
                    myPosition = null;
                }
                else if (node.Parent.Left == node)
                {
                    myPosition = false;
                }
                else
                {
                    myPosition = true;
                }
                //1
                if (node.Left == null && node.Right == null)
                {
                    if (myPosition == null)
                    {
                        node.Key = null;
                        node.Sum = 0;
                    }
                    if (myPosition == false)
                    {
                        node.Parent.Left = null;
                    }
                    if (myPosition == true)
                    {
                        node.Parent.Right = null;
                    }

                    node.Parent = null;
                }

                //2
                if (node.Left != null && node.Right == null)
                {
                    if (myPosition == null)
                    {
                        Tree             = node.Left;
                        node.Left.Parent = null;
                        //node.Left = null;
                    }
                    if (myPosition == false)
                    {
                        node.Parent.Left = node.Left;
                        node.Left.Parent = node.Parent;
                        //node.Parent = null;
                        //node.Left = null;
                    }
                    if (myPosition == true)
                    {
                        node.Parent.Right = node.Left;
                        node.Left.Parent  = node.Parent;
                        //node.Parent = null;
                        //node.Left = null;
                    }
                }
                if (node.Left == null && node.Right != null)
                {
                    if (myPosition == null)
                    {
                        Tree = node.Right;
                        node.Right.Parent = null;
                        //node.Right = null;
                    }
                    if (myPosition == false)
                    {
                        node.Parent.Left  = node.Right;
                        node.Right.Parent = node.Parent;
                        //node.Parent = null;
                        //node.Right = null;
                    }
                    if (myPosition == true)
                    {
                        node.Parent.Right = node.Right;
                        node.Right.Parent = node.Parent;
                        node.Parent       = null;
                        node.Right        = null;
                    }
                }
                //3
                if (node.Left != null && node.Right != null)
                {
                    ZamenaItem = FindMin(node.Right);
                    node.Key   = ZamenaItem.Key;
                    node.Sum   = ZamenaItem.Sum;

                    if (node.Color == RBColor.Red)
                    {
                        node.Color = ZamenaItem.Color;
                        Delete(ZamenaItem);
                        return;
                    }
                    if (node.Color == RBColor.Black && ZamenaItem.Color == RBColor.Red)
                    {
                        node.Color = RBColor.Black;
                        Delete(ZamenaItem);
                        return;
                    }
                    if (node.Color == RBColor.Black && ZamenaItem.Color == RBColor.Black)
                    {
                        DeleteBalanser(node);
                    }
                }
            }
Exemplo n.º 8
0
        /*
         * Delete node z and replace it with y.  If z has only one child we just replace z with
         * that child.  If z has two children then y should be z's successor.
         * */
        private void RBDelete(Node z)
        {
            Node x;
            //First, set y equal to z
            Node y = z;

            /*
             * We want to store z's original color so when we move a node
             * into z's postion and the value of yOriginalColor is black then we call
             * RBDeleteFixup.
             */
            RBColor yOriginalColor = y.color;

            /*
             * The first two if statements cover when z just has one child.  If
             * so, just replace z with the child
             */
            if (z.left == NIL.Instance())
            {
                x = z.right;
                RBTransplant(z, z.right);
            }
            else if (z.right == NIL.Instance())
            {
                x = z.left;
                RBTransplant(z, z.left);
            }
            else
            {
                /*
                 * z has two children.  First find the successor (the smallest key
                 * that is larger than z.  Move it into z's position by setting it's
                 * parent to z and re-assign child nodes.
                 */
                y = RBTreeMinimum(z.right);
                yOriginalColor = y.color;
                x = y.right;
                if (y.parent == z)
                {
                    x.parent = y;
                }
                else
                {
                    RBTransplant(y, y.right);
                    y.right        = z.right;
                    y.right.parent = y;
                }
                RBTransplant(z, y);
                y.left        = z.left;
                y.left.parent = y;
                y.color       = z.color;
            }

            /*
             * If node y was black, several problems may arise which RBDeleteFixup will fix.
             * */
            if (yOriginalColor == RBColor.BLACK)
            {
                RBDeleteFixup(x);
            }
        }
Exemplo n.º 9
0
            public Node next = null;                    //一つ大きい値のkeyを持つNode

            public Node(RBColor color, K key, Node parent)
            {
                this.color  = color;
                this.key    = key;
                this.parent = parent;
            }