Ejemplo n.º 1
0
        private void RightRotate(RBTNode node)
        {
            var leftNode = node.Left;

            leftNode.Right = node.Right;

            if (leftNode.Right != NIL)
            {
                leftNode.Right.Parent = node;
            }

            leftNode.Parent = node.Parent;

            if (node.Parent == NIL)
            {
                Root = leftNode;
            }
            else if (node == node.Parent.Right)
            {
                node.Parent.Right = leftNode;
            }
            else
            {
                node.Parent.Left = leftNode;
            }

            leftNode.Right = node;
            node.Parent    = leftNode;
        }
Ejemplo n.º 2
0
        private void LeftRotate(RBTNode node)
        {
            var rightNode = node.Right;

            rightNode.Right = node.Left;

            if (rightNode.Left != NIL)
            {
                rightNode.Left.Parent = node;
            }

            rightNode.Parent = node.Parent;

            if (node.Parent == NIL)
            {
                Root = rightNode;
            }
            else if (node == node.Parent.Left)
            {
                node.Parent.Left = rightNode;
            }
            else
            {
                node.Parent.Right = rightNode;
            }

            rightNode.Left = node;
            node.Parent    = rightNode;
        }
Ejemplo n.º 3
0
 public RBTNode Maximum(RBTNode node)
 {
     while (node.Right != NIL)
     {
         node = node.Right;
     }
     return(node);
 }
Ejemplo n.º 4
0
 public RBTNode Minimum(RBTNode node)
 {
     while (node.Left != NIL)
     {
         node = node.Left;
     }
     return(node);
 }
Ejemplo n.º 5
0
        private void InsertFixup(RBTNode node)
        {
            while (node.Parent.Color.IsRed())
            {
                if (node.Parent == node.Parent.Parent.Left)
                {
                    var uncleNode = node.Parent.Parent.Right;

                    if (uncleNode.Color.IsRed())
                    {
                        node.Parent.SetBlack();
                        uncleNode.SetBlack();
                        node.Parent.Parent.SetRed();
                        node = node.Parent.Parent;
                    }
                    else
                    {
                        if (node == node.Parent.Right)
                        {
                            node = node.Parent;
                            LeftRotate(node);
                        }

                        node.Parent.SetBlack();
                        node.Parent.Parent.SetRed();
                        RightRotate(node.Parent.Parent);
                    }
                }
                else
                {
                    var uncleNode = node.Parent.Parent.Left;

                    if (uncleNode.Color.IsRed())
                    {
                        node.Parent.SetBlack();
                        uncleNode.SetBlack();
                        node.Parent.Parent.SetRed();
                        node = node.Parent.Parent;
                    }
                    else
                    {
                        if (node == node.Parent.Left)
                        {
                            node = node.Parent;
                            RightRotate(node);
                        }

                        node.Parent.SetBlack();
                        node.Parent.Parent.SetRed();
                        LeftRotate(node.Parent.Parent);
                    }
                }
                Root.SetBlack();
            }
        }
Ejemplo n.º 6
0
 public RBTNode Search(RBTNode node, int key)
 {
     while (node != NIL && key != node.Key)
     {
         if (key < node.Key)
         {
             node = node.Left;
         }
         else
         {
             node = node.Right;
         }
     }
     return(node);
 }
Ejemplo n.º 7
0
        public RBTNode Predeccessor(RBTNode node)
        {
            if (node.Left != NIL)
            {
                return(Maximum(node.Left));
            }

            var prevnode = node.Parent;

            while (prevnode != NIL && node != prevnode.Left)
            {
                node     = prevnode;
                prevnode = prevnode.Parent;
            }
            return(prevnode);
        }
Ejemplo n.º 8
0
        public RBTNode Successor(RBTNode node)
        {
            if (node.Right != NIL)
            {
                return(Minimum(node.Right));
            }

            var prevnode = node.Parent;

            while (prevnode != NIL && node != prevnode.Right)
            {
                node     = prevnode;
                prevnode = prevnode.Parent;
            }
            return(prevnode);
        }
Ejemplo n.º 9
0
        private void Transplant(RBTNode nodeToReplace, RBTNode replacedNode)
        {
            if (nodeToReplace.Parent == NIL)
            {
                Root = replacedNode;
            }
            else if (nodeToReplace == nodeToReplace.Parent.Left)
            {
                nodeToReplace.Parent.Left = replacedNode;
            }
            else
            {
                nodeToReplace.Parent.Right = replacedNode;
            }

            replacedNode.Parent = nodeToReplace.Parent;
        }
Ejemplo n.º 10
0
        public void Insert(int key)
        {
            var     nodeToInsert = new RBTNode(key);
            RBTNode prevnode     = NIL;
            var     node         = Root;

            while (node != NIL)
            {
                prevnode = node;

                if (nodeToInsert.Key < node.Key)
                {
                    node = node.Left;
                }
                else
                {
                    node = node.Right;
                }
            }

            nodeToInsert.Parent = prevnode;

            if (prevnode == NIL)
            {
                Root = nodeToInsert;
            }
            else if (nodeToInsert.Key < prevnode.Key)
            {
                prevnode.Left = nodeToInsert;
            }
            else
            {
                prevnode.Right = nodeToInsert;
            }

            nodeToInsert.Left  = NIL;
            nodeToInsert.Right = NIL;

            InsertFixup(nodeToInsert);
        }
Ejemplo n.º 11
0
        private void DeleteFixup(RBTNode badNode)
        {
            RBTNode brotherNode;

            while (badNode != Root && badNode.Color.IsBlack())
            {
                if (badNode == badNode.Parent.Left)
                {
                    brotherNode = badNode.Parent.Right;

                    if (brotherNode.Color.IsRed())
                    {
                        brotherNode.SetBlack();
                        badNode.Parent.SetRed();
                        LeftRotate(badNode.Parent);
                        brotherNode = badNode.Parent.Right;
                    }

                    if (brotherNode.Left.Color.IsBlack() && brotherNode.Right.Color.IsBlack())
                    {
                        brotherNode.SetRed();
                        badNode = badNode.Parent;
                    }
                    else
                    {
                        if (brotherNode.Right.Color.IsBlack())
                        {
                            brotherNode.Left.SetBlack();
                            brotherNode.SetRed();
                            RightRotate(brotherNode);
                            brotherNode = badNode.Parent.Right;
                        }

                        brotherNode.Color = badNode.Parent.Color;
                        badNode.Parent.SetBlack();
                        LeftRotate(badNode.Parent);
                        badNode = Root;
                    }
                }
                else
                {
                    brotherNode = badNode.Parent.Left;

                    if (brotherNode.Color.IsRed())
                    {
                        brotherNode.SetBlack();
                        badNode.Parent.SetRed();
                        RightRotate(badNode.Parent);
                        brotherNode = badNode.Parent.Left;
                    }

                    if (brotherNode.Left.Color.IsBlack() && brotherNode.Right.Color.IsBlack())
                    {
                        brotherNode.SetRed();
                        badNode = badNode.Parent;
                    }
                    else
                    {
                        if (brotherNode.Right.Color.IsBlack())
                        {
                            brotherNode.Right.SetBlack();
                            brotherNode.SetRed();
                            LeftRotate(brotherNode);
                            brotherNode = badNode.Parent.Left;
                        }

                        brotherNode.Color = badNode.Parent.Color;
                        badNode.Parent.SetBlack();
                        RightRotate(badNode.Parent);
                        badNode = Root;
                    }
                }
            }
            badNode.SetBlack();
        }
Ejemplo n.º 12
0
 public static void SetBlack(this RBTNode node)
 {
     node.Color = Color.Black;
 }
Ejemplo n.º 13
0
 public static void SetRed(this RBTNode node)
 {
     node.Color = Color.Red;
 }