protected override RedBlackNode <TK, TV> Delete(RedBlackNode <TK, TV> node, TK key)
        {
            if (key.CompareTo(node.Key) < 0)
            {
                if (!IsRed(node.left) && !IsRed(node.left.left))
                {
                    node = MoveRedLeft(node);
                }

                node.left = Delete(node.left, key);
            }
            else
            {
                if (IsRed(node.left))
                {
                    node = RotateRight(node);
                }

                if (key.CompareTo(node.Key) == 0 && node.right == null)
                {
                    return(null);
                }

                if (!IsRed(node.right) && !IsRed(node.right.right))
                {
                    node = MoveRedRight(node);
                }

                if (key.CompareTo(node.Key) == 0)
                {
                    RedBlackNode <TK, TV> temp = node;
                    node       = GetMin(temp.right);
                    node.color = temp.color;
                    node.left  = temp.left;
                    node.right = DeleteMin(temp.right);
                    node.size  = SizeOfNode(node.left) + SizeOfNode(node.right) + 1;
                }
                else
                {
                    node.right = Delete(node.right, key);
                }
            }

            return(Balance(node));
        }
        protected virtual RedBlackNode <TK, TV> Balance(RedBlackNode <TK, TV> node)
        {
            if (IsRed(node.right) && !IsRed(node.left))
            {
                node = RotateLeft(node);
            }
            if (IsRed(node.left) && IsRed(node.left.left))
            {
                node = RotateRight(node);
            }
            if (IsRed(node.left) && IsRed(node.right))
            {
                FlipColors(node);
            }

            node.size = SizeOfNode(node.left) + SizeOfNode(node.right) + 1;

            return(node);
        }
        protected override RedBlackNode <TK, TV> DeleteMax(RedBlackNode <TK, TV> node)
        {
            if (IsRed(node.left))
            {
                node = RotateRight(node);
            }

            if (node.right == null)
            {
                return(null);
            }

            if (!IsRed(node.right) && !IsRed(node.right.left))
            {
                node = MoveRedRight(node);
            }

            node.right = DeleteMax(node.right);

            return(Balance(node));
        }
        /*******************************************************************
        *     辅助函数
        *******************************************************************/

        protected override RedBlackNode <TK, TV> Put(RedBlackNode <TK, TV> node, TK key, TV value,
                                                     Func <RedBlackNode <TK, TV> > create)
        {
            if (node == null)
            {
                return(create());
            }

            int cmp = key.CompareTo(node.Key);

            if (cmp < 0)
            {
                node.left = Put(node.left, key, value, create);
            }
            else if (cmp > 0)
            {
                node.right = Put(node.right, key, value, create);
            }
            else
            {
                node.Value = value;
            }

            if (IsRed(node.right) && !IsRed(node.left))
            {
                node = RotateLeft(node);
            }
            if (IsRed(node.left) && IsRed(node.left.left))
            {
                node = RotateRight(node);
            }
            if (IsRed(node.left) && IsRed(node.right))
            {
                FlipColors(node);
            }

            node.size = SizeOfNode(node.left) + SizeOfNode(node.right) + 1;

            return(node);
        }
Exemple #5
0
 public RedBlackNode(TKey key, TValue value, Color color, int size = 1, RedBlackNode <TKey, TValue> left = null,
                     RedBlackNode <TKey, TValue> right             = null) : base(key, value, size, left, right)
 {
     this.color = color;
 }
 /// <summary>
 /// 当一个节点的左右两条链接都是红色链接时,需要转换颜色,
 /// 把当前节点转换成红色,两个直接点转换成黑色
 /// </summary>
 /// <param name="node">要转换的节点</param>
 protected virtual void FlipColors(RedBlackNode <TK, TV> node)
 {
     node.color       = RedBlackNode <TK, TV> .Color.Red;
     node.left.color  = RedBlackNode <TK, TV> .Color.Black;
     node.right.color = RedBlackNode <TK, TV> .Color.Black;
 }
 /// <summary>
 /// 判断连接到当前节点的链接是否为红色
 /// </summary>
 /// <param name="node">要判断的节点</param>
 protected virtual bool IsRed(RedBlackNode <TK, TV> node)
 {
     return(node != null && node.color == RedBlackNode <TK, TV> .Color.Red);
 }