示例#1
0
        public void Remove(TKey key, TValue value)
        {
            if (root == null)
            {
                return;
            }
            RBTreeNode <TKey, TValue> ptr = Find(key);

            if (ptr == null)
            {
                return;
            }
            ptr.Value.Remove(value);
            if (ptr.Value.Size == 0)
            {
                Remove(ptr);
            }
        }
示例#2
0
 private RBTreeNode <TKey, TValue> Replace(RBTreeNode <TKey, TValue> x)
 {
     if (x.left != null && x.right != null)
     {
         return(Successor(x.left));
     }
     if (x.left == null && x.right == null)
     {
         return(null);
     }
     if (x.left != null)
     {
         return(x.left);
     }
     else
     {
         return(x.right);
     }
 }
示例#3
0
        private bool ChangeColor(RBTreeNode <T> inputNode)
        {
            if (!inputNode.IfIsRed && null != inputNode.LeftNode && null != inputNode.RightNode && inputNode.LeftNode.IfIsRed && inputNode.RightNode.IfIsRed)
            {
                inputNode.LeftNode.ChangeColor();
                inputNode.RightNode.ChangeColor();

                if (inputNode != RootNode)
                {
                    inputNode.ChangeColor();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            return(false);
        }
示例#4
0
 public void Clear() => root = null;
示例#5
0
        private void FixDoubleBlack(RBTreeNode <TKey, TValue> x)
        {
            if (x == root)
            {
                return;
            }
            RBTreeNode <TKey, TValue> bro    = x.Brother();
            RBTreeNode <TKey, TValue> parent = x.parent;

            if (bro == null)
            {
                FixDoubleBlack(parent);
            }
            else
            {
                if (bro.color == RBTreeNode <TKey, TValue> .Color.RED)
                {
                    parent.color = RBTreeNode <TKey, TValue> .Color.RED;
                    bro.color    = RBTreeNode <TKey, TValue> .Color.BLACK;
                    if (bro.isOnLeft())
                    {
                        Rotate(parent, Direction.RIGHT);
                    }
                    else
                    {
                        Rotate(parent, Direction.LEFT);
                    }
                    FixDoubleBlack(x);
                }
                else
                {
                    if (bro.hasRedChild())
                    {
                        if (bro.left != null && bro.left.color == RBTreeNode <TKey, TValue> .Color.RED)
                        {
                            if (bro.isOnLeft())
                            {
                                bro.left.color = bro.color;
                                bro.color      = parent.color;
                                Rotate(parent, Direction.RIGHT);
                            }
                            else
                            {
                                bro.left.color = parent.color;
                                Rotate(bro, Direction.RIGHT);
                                Rotate(parent, Direction.LEFT);
                            }
                        }
                        else
                        {
                            if (bro.isOnLeft())
                            {
                                bro.right.color = parent.color;
                                Rotate(bro, Direction.LEFT);
                                Rotate(parent, Direction.RIGHT);
                            }
                            else
                            {
                                bro.right.color = bro.color;
                                bro.color       = parent.color;
                                Rotate(parent, Direction.LEFT);
                            }
                        }
                        parent.color = RBTreeNode <TKey, TValue> .Color.BLACK;
                    }
                    else
                    {
                        bro.color = RBTreeNode <TKey, TValue> .Color.RED;
                        if (parent.color == RBTreeNode <TKey, TValue> .Color.BLACK)
                        {
                            FixDoubleBlack(parent);
                        }
                        else
                        {
                            parent.color = RBTreeNode <TKey, TValue> .Color.BLACK;
                        }
                    }
                }
            }
        }
示例#6
0
        private void Remove(RBTreeNode <TKey, TValue> v)
        {
            RBTreeNode <TKey, TValue> u = Replace(v);
            bool uvBlack = ((u == null || u.color == RBTreeNode <TKey, TValue> .Color.BLACK) &&
                            (v.color == RBTreeNode <TKey, TValue> .Color.BLACK));
            RBTreeNode <TKey, TValue> parent = v.parent;

            if (u == null)
            {
                if (v == root)
                {
                    root = null;
                }
                else
                {
                    if (uvBlack)
                    {
                        FixDoubleBlack(v);
                    }
                    else
                    {
                        if (v.Brother() != null)
                        {
                            v.Brother().color = RBTreeNode <TKey, TValue> .Color.RED;
                        }
                    }
                    if (v.isOnLeft())
                    {
                        parent.left = null;
                    }
                    else
                    {
                        parent.right = null;
                    }
                }
                v = null; // delete v;
                return;
            }
            if (v.left == null || v.right == null)
            {
                if (v == root)
                {
                    v.key  = u.key;
                    v.list = u.list;
                    v.left = v.right = null;
                    u      = null; // delete u;
                }
                else
                {
                    if (v.isOnLeft())
                    {
                        parent.left = u;
                    }
                    else
                    {
                        parent.right = u;
                    }
                    v        = null; // delete v;
                    u.parent = parent;
                    if (uvBlack)
                    {
                        FixDoubleBlack(u);
                    }
                    else
                    {
                        u.color = RBTreeNode <TKey, TValue> .Color.BLACK;
                    }
                }
                return;
            }
            Swap(ref u.key, ref v.key);
            Swap(ref u.list, ref v.list);
            Remove(u);
        }