Exemplo n.º 1
0
        private void Transplant(RbtreeNode x, RbtreeNode y)
        {
            RbtreeNode p = x.Parent;

            if (p == null)
            {
                _rootNode = y;
            }
            else
            {
                if (p.Left == x)
                {
                    p.Left = y;
                }
                else
                {
                    p.Right = y;
                }
            }

            if (y != null)
            {
                y.Parent = p;
            }
        }
Exemplo n.º 2
0
        private SearchResult Search(T key)
        {
            ModuleProc   PROC        = new ModuleProc(this.DYN_MODULE_NAME, "DoLookup");
            RbtreeNode   currentNode = _rootNode;
            SearchResult result      = new Rbtree <T> .SearchResult();

            try
            {
                while (currentNode != null)
                {
                    result.Position = _comparer.Compare(currentNode.Value, key);
                    if (result.Position == 0)
                    {
                        result.Node = currentNode;
                        return(result);
                    }

                    result.ParentNode = currentNode;
                    if (result.Position > 0)
                    {
                        currentNode = currentNode.Left;
                    }
                    else
                    {
                        currentNode = currentNode.Right;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            return(result);
        }
Exemplo n.º 3
0
 public Enumerator(Rbtree <T> parent)
 {
     _parent  = parent;
     _node    = _parent._rootNode;
     _stack   = new DoubleLinkedListStack <RbtreeNode>();
     _current = null;
     this.Initialize();
 }
Exemplo n.º 4
0
            private void Initialize()
            {
                RbtreeNode node = _node;

                while (node != null)
                {
                    _stack.Push(node);
                    node = node.Left;
                }
            }
Exemplo n.º 5
0
            public bool MoveNext()
            {
                if (_stack.Count == 0)
                {
                    _current = null;
                    return(false);
                }

                _current = _stack.Pop();
                RbtreeNode node = _current.Right;

                while (node != null)
                {
                    _stack.Push(node);
                    node = node.Left;
                }
                return(true);
            }
Exemplo n.º 6
0
        private void IterateNodes(Func <RbtreeNode, bool> doWork)
        {
            if (_rootNode == null)
            {
                return;
            }
            ModuleProc         PROC = new ModuleProc(this.DYN_MODULE_NAME, "IterateNodes");
            Stack <RbtreeNode> st   = new Stack <RbtreeNode>();

            try
            {
                RbtreeNode node    = _rootNode;
                RbtreeNode current = null;
                while (node != null)
                {
                    st.Push(node);
                    node = node.Left;
                }

                while (st.Count != 0)
                {
                    current = st.Pop();
                    if (doWork != null)
                    {
                        if (!doWork(current))
                        {
                            return;
                        }
                    }

                    node = current.Right;
                    while (node != null)
                    {
                        st.Push(node);
                        node = node.Left;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }
Exemplo n.º 7
0
        private void RotateRight(RbtreeNode node)
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "RotateRight");

            try
            {
                RbtreeNode x      = node;
                RbtreeNode y      = node.Left;
                RbtreeNode parent = x.Parent;

                if (!x.IsRoot)
                {
                    if (x == parent.Left)
                    {
                        parent.Left = y;
                    }
                    else
                    {
                        parent.Right = y;
                    }
                }
                else
                {
                    _rootNode = y;
                }

                y.Parent = parent;
                x.Parent = y;

                x.Left = y.Right;
                if (x.Left != null)
                {
                    x.Left.Parent = x;
                }
                y.Right = x;
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
 public void Clear()
 {
     _rootNode = null;
 }
Exemplo n.º 10
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);
            }
        }