Exemplo n.º 1
0
        public RedBlackTree(T parentValue)
        {
            this.count = 1;

            this.Root = new RedBlackNode <T>(parentValue, NodeColor.Black);
        }
Exemplo n.º 2
0
        private void InsertFixup(RedBlackNode <T> node)
        {
            while (node.Parent.Color == NodeColor.Red)
            {
                if (object.ReferenceEquals(node.Parent, node.Parent.Parent.LeftChild))
                {
                    RedBlackNode <T> y = node.Parent.Parent.RigthChild;

                    if (y.Color == NodeColor.Red)
                    {
                        node.Parent.Color = NodeColor.Black;

                        y.Color = NodeColor.Black;

                        node.Parent.Parent.Color = NodeColor.Red;

                        node = node.Parent.Parent;
                    }
                    else if (object.ReferenceEquals(node, node.Parent.RigthChild))
                    {
                        node = node.Parent;

                        LeftRotate(node);
                    }
                    else
                    {
                        node.Parent.Color = NodeColor.Black;

                        node.Parent.Parent.Color = NodeColor.Red;

                        RigthRotate(node.Parent.Parent);
                    }
                }
                else if (object.ReferenceEquals(node.Parent, node.Parent.Parent.RigthChild))
                {
                    RedBlackNode <T> y = node.Parent.Parent.LeftChild;

                    if (y.Color == NodeColor.Red)
                    {
                        node.Parent.Color = NodeColor.Black;

                        y.Color = NodeColor.Black;

                        node.Parent.Parent.Color = NodeColor.Red;

                        node = node.Parent.Parent;
                    }
                    else if (object.ReferenceEquals(node, node.Parent.LeftChild))
                    {
                        node = node.Parent;

                        RigthRotate(node);
                    }
                    else
                    {
                        node.Parent.Color = NodeColor.Black;

                        node.Parent.Parent.Color = NodeColor.Red;

                        LeftRotate(node.Parent.Parent);
                    }
                }
                if (node.Parent == null)
                {
                    break;
                }
            }

            this.Root.Color = NodeColor.Black;
        }