示例#1
0
        public bool Remove(T item)
        {
            ModuleProc PROC   = new ModuleProc(this.DYN_MODULE_NAME, "Remove");
            bool       result = default(bool);

            try
            {
                SearchResult r = this.Search(item);
                if (r.Node == null)
                {
                    // key not found
                    return(false);
                }
                RbtreeNode  x       = r.Node;
                RbtreeNode  z       = null;
                RbtreeNode  y       = x;
                RbtreeColor y_color = y.Color;

                // left is null
                if (x.Left == null)
                {
                    z = x.Right;
                    this.Transplant(x, z);
                }
                else if (x.Right == null)
                {
                    z = x.Left;
                    this.Transplant(x, z);
                }
                else
                {
                    y       = x.Right.Minimum;
                    y_color = y.Color;
                    z       = y.Right;

                    if (y.Parent != x)
                    {
                        this.Transplant(y, y.Right);
                        y.Right        = x.Right;
                        y.Right.Parent = y;
                    }
                    else
                    {
                        z.Parent = y;
                    }

                    this.Transplant(x, y);
                    y.Left        = x.Left;
                    y.Left.Parent = y;
                    y.Color       = x.Color;
                }

                if (y_color == Rbtree <T> .RbtreeColor.Black)
                {
                    //fix up
                    x = z;
                    while (x != null &&
                           x != _rootNode &&
                           x.IsBlack)
                    {
                        if (x.p.Left == x)
                        {
                            RbtreeNode w = x.p.Right;
                            if (w.IsRed)
                            {
                                w.SetColorBlack();
                                x.p.SetColorRed();
                                this.RotateLeft(x.p);
                                w = x.p.Right;
                            }
                            if (w.Left != null && w.Right != null)
                            {
                                if (w.Left.IsBlack && w.Right.IsBlack)
                                {
                                    w.SetColorRed();
                                    x = x.p;
                                }
                                else if (w.Right.IsBlack)
                                {
                                    w.Left.SetColorBlack();
                                    w.SetColorRed();
                                    this.RotateRight(w);
                                    w = x.p.Right;
                                }
                                w.Color = w.p.Color;
                                x.p.SetColorBlack();
                                w.Right.SetColorBlack();
                                this.RotateLeft(x.p);
                                x = _rootNode;
                            }
                        }
                        else
                        {
                            RbtreeNode w = x.p.Left;
                            if (w.IsRed)
                            {
                                w.SetColorBlack();
                                x.p.SetColorRed();
                                this.RotateRight(x.p);
                                w = x.p.Left;
                            }
                            if (w.Left != null && w.Right != null)
                            {
                                if (w.Left.IsBlack && w.Right.IsBlack)
                                {
                                    w.SetColorRed();
                                    x = x.p;
                                }
                                else if (w.Left.IsBlack)
                                {
                                    w.Right.SetColorBlack();
                                    w.SetColorRed();
                                    this.RotateLeft(w);
                                    w = x.p.Left;
                                }
                                w.Color = w.p.Color;
                                x.p.SetColorBlack();
                                w.Left.SetColorBlack();
                                this.RotateRight(x.p);
                                x = _rootNode;
                            }
                        }
                    }

                    if (x != null)
                    {
                        x.SetColorBlack();
                    }
                }
                _count--;
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return(result);
        }
示例#2
0
        public void Add(T item)
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "Add");

            try
            {
                SearchResult lookup = this.Search(item);
                if (lookup.Node != null)
                {
                    return;
                }

                // new node
                lookup.Node = new Rbtree <T> .RbtreeNode()
                {
                    Color  = Rbtree <T> .RbtreeColor.Red,
                    Left   = null,
                    Right  = null,
                    Value  = item,
                    Parent = lookup.ParentNode
                };
                RbtreeNode x      = lookup.Node;
                RbtreeNode p      = lookup.ParentNode;
                bool       isLeft = lookup.IsLeft;
                _count++;

                if (p != null)
                {
                    if (isLeft)
                    {
                        if (p == _minNode)
                        {
                            _minNode = x;
                        }
                    }
                    else
                    {
                        if (p == _maxNode)
                        {
                            _maxNode = x;
                        }
                    }
                    p[isLeft] = x;
                }
                else
                {
                    _minNode  = x;
                    _maxNode  = x;
                    _rootNode = x;
                }

                // rotations
                while (((p = x.Parent) != null) &&
                       p.IsRed)
                {
                    RbtreeNode g = p.Parent;

                    // if parent is at left of grandpa
                    if (p == g.Left)
                    {
                        RbtreeNode u = g.Right;

                        // if uncle is red
                        if ((u != null) && (u.IsRed))
                        {
                            p.SetColorBlack();
                            u.SetColorBlack();
                            g.SetColorRed();
                            x = g;
                        }
                        else
                        {
                            if (x == p.Right)
                            {
                                this.RotateLeft(p);
                                x = p;
                                p = x.Parent;
                            }
                            p.SetColorBlack();
                            g.SetColorRed();
                            this.RotateRight(g);
                        }
                    }
                    else // right of parent
                    {
                        RbtreeNode u = g.Left;

                        // if uncle is red
                        if ((u != null) && (u.IsRed))
                        {
                            p.SetColorBlack();
                            u.SetColorBlack();
                            g.SetColorRed();
                            x = g;
                        }
                        else
                        {
                            if (x == p.Left)
                            {
                                this.RotateRight(p);
                                x = p;
                                p = x.Parent;
                            }
                            p.SetColorBlack();
                            g.SetColorRed();
                            this.RotateLeft(g);
                        }
                    }
                }

                // root always black
                _rootNode.Color = Rbtree <T> .RbtreeColor.Black;
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }