Exemple #1
0
        public void Delete(RedBlackNode <Key, Value> z)
        {
            if (z == null)
            {
                throw new ArgumentNullException(nameof(z));
            }
            var y         = z;
            var desiccate = y.NodeColor;
            RedBlackNode <Key, Value> x;

            if (z.LeftChild == Sentinel)
            {
                x = z.RightChild;
                Transplant(z, z.RightChild);
            }
            else if (z.RightChild == Sentinel)
            {
                x = z.LeftChild;
                Transplant(z, z.LeftChild);
            }
            else
            {
                y         = Minimum(z.RightChild);
                desiccate = y.NodeColor;
                x         = y.RightChild;
                if (y.Parent == z)
                {
                    x.Parent = y;
                }
                else
                {
                    Transplant(y, y.RightChild);
                    y.RightChild        = z.RightChild;
                    y.RightChild.Parent = y;
                }
                Transplant(z, y);
                y.LeftChild        = z.LeftChild;
                y.LeftChild.Parent = y;
                y.NodeColor        = z.NodeColor;
            }
            if (desiccate == Color.Black)
            {
                DeleteFixup(x);
            }
        }
Exemple #2
0
        private void Traverse(RedBlackNode <Key, Value> node)
        {
            if (node.LeftChild != Sentinel)
            {
                Traverse(node.LeftChild);
            }
            traverseNumber++;
            string b = $"Node {node.Index}, Parent";

            if (node.Parent == Sentinel)
            {
                b += " Sentinel";
            }
            else
            {
                b += $" {node.Parent.Index}";
            }
            if (node.LeftChild == Sentinel)
            {
                b += ", Left Sentinel";
            }
            else
            {
                b += $", Left {node.LeftChild.Index}";
            }
            if (node.RightChild == Sentinel)
            {
                b += ", Right Sentinel";
            }
            else
            {
                b += $", Right {node.RightChild.Index}";
            }
            Console.WriteLine(b);
            //node.Print();
            if (node.RightChild != Sentinel)
            {
                Traverse(node.RightChild);
            }
        }
Exemple #3
0
 public RedBlackTree()
 {
     Sentinel           = new RedBlackNode <Key, Value>();
     Root               = Sentinel;
     Sentinel.NodeColor = Color.Black;
 }
Exemple #4
0
        private void DeleteFixup(RedBlackNode <Key, Value> node)
        {
            while (node != Root && node.NodeColor == Color.Black)
            {
                if (node == node.Parent.LeftChild)
                {
                    var sibling = node.Parent.RightChild;
                    if (sibling.NodeColor == Color.Red)
                    {
                        sibling.NodeColor     = Color.Black;
                        node.Parent.NodeColor = Color.Red;
                        LeftRotate(node.Parent);
                        sibling = node.Parent.RightChild;
                    }
                    if (sibling.LeftChild.NodeColor == Color.Black &&
                        sibling.RightChild.NodeColor == Color.Black)
                    {
                        sibling.NodeColor = Color.Red;
                        node = node.Parent;
                    }
                    else
                    {
                        if (sibling.RightChild.NodeColor == Color.Black)
                        {
                            sibling.LeftChild.NodeColor = Color.Black;
                            sibling.NodeColor           = Color.Red;
                            RightRotate(sibling);
                            sibling = node.Parent.RightChild;
                        }
                        sibling.NodeColor            = node.Parent.NodeColor;
                        node.Parent.NodeColor        = Color.Black;
                        sibling.RightChild.NodeColor = Color.Black;
                        LeftRotate(node.Parent);
                        node = Root;
                    }
                }
                else
                {
                    var sibling = node.Parent.LeftChild;
                    if (sibling.NodeColor == Color.Red)
                    {
                        sibling.NodeColor     = Color.Black;
                        node.Parent.NodeColor = Color.Red;
                        RightRotate(node.Parent);
                        sibling = node.Parent.LeftChild;
                    }
                    if (sibling.LeftChild.NodeColor == Color.Black &&
                        sibling.RightChild.NodeColor == Color.Black)
                    {
                        sibling.NodeColor = Color.Red;
                        node = node.Parent;
                    }
                    else
                    {
                        if (sibling.LeftChild.NodeColor == Color.Black)
                        {
                            sibling.RightChild.NodeColor = Color.Black;
                            sibling.NodeColor            = Color.Red;
                            LeftRotate(sibling);
                            sibling = node.Parent.LeftChild;
                        }
                        sibling.NodeColor           = node.Parent.NodeColor;
                        node.Parent.NodeColor       = Color.Black;
                        sibling.LeftChild.NodeColor = Color.Black;
                        RightRotate(node.Parent);
                        node = Root;
                    }
                }

                node.NodeColor = Color.Black;
            }
        }