Exemple #1
0
 private void CheckBalance()
 {
     if (!this.IsRoot() && this.Parent.IsRed())
     {
         if (this.IsFatherLeftofGrandFather())
         {
             RBTreeNode <T> rightUncle = this.GetRightUncle();
             if (rightUncle != this.tree.NIL && rightUncle != null && rightUncle.IsRed())
             {
                 this.ColorGrandFather(RBTreeNode <T> .COLOR.RED);
                 rightUncle.Color  = RBTreeNode <T> .COLOR.BLACK;
                 this.Parent.Color = RBTreeNode <T> .COLOR.BLACK;
                 RBTreeNode <T> grandFather = this.GetGrandFather();
                 if (grandFather != null)
                 {
                     grandFather.CheckBalance();
                 }
             }
             else
             {
                 if (this == this.Parent.Right)
                 {
                     this.RotateLeft(this.Parent);
                 }
                 this.ColorFather(RBTreeNode <T> .COLOR.BLACK);
                 this.ColorGrandFather(RBTreeNode <T> .COLOR.RED);
                 if (this.Parent.Parent != null)
                 {
                     this.RotateRight(this.Parent.Parent);
                 }
             }
         }
         else
         {
             RBTreeNode <T> leftUncle = this.GetLeftUncle();
             if (leftUncle != this.tree.NIL && leftUncle != null && leftUncle.IsRed())
             {
                 this.ColorGrandFather(RBTreeNode <T> .COLOR.RED);
                 leftUncle.Color   = RBTreeNode <T> .COLOR.BLACK;
                 this.Parent.Color = RBTreeNode <T> .COLOR.BLACK;
                 RBTreeNode <T> grandFather2 = this.GetGrandFather();
                 if (grandFather2 != null)
                 {
                     grandFather2.CheckBalance();
                 }
             }
             else
             {
                 if (this == this.Parent.Left)
                 {
                     this.RotateRight(this.Parent);
                 }
                 this.ColorFather(RBTreeNode <T> .COLOR.BLACK);
                 this.ColorGrandFather(RBTreeNode <T> .COLOR.RED);
                 if (this.Parent.Parent != null)
                 {
                     this.RotateLeft(this.Parent.Parent);
                 }
             }
         }
     }
     this.tree.root.Color = RBTreeNode <T> .COLOR.BLACK;
 }
Exemple #2
0
 private void RBDeleteFixUp(RBTreeNode <T> x)
 {
     while (x != this.tree.root && x.IsBlack())
     {
         if (x == x.Parent.Left)
         {
             RBTreeNode <T> right = x.Parent.Right;
             if (right.IsRed())
             {
                 right.Color    = RBTreeNode <T> .COLOR.BLACK;
                 x.Parent.Color = RBTreeNode <T> .COLOR.RED;
                 this.RotateLeft(x.Parent);
                 right = x.Parent.Right;
             }
             if (right.Left.IsBlack() && right.Right.IsBlack())
             {
                 right.Color = RBTreeNode <T> .COLOR.RED;
                 x           = x.Parent;
             }
             else
             {
                 if (right.Right.IsBlack())
                 {
                     right.Left.Color = RBTreeNode <T> .COLOR.BLACK;
                     right.Color      = RBTreeNode <T> .COLOR.RED;
                     this.RotateRight(right);
                     right = x.Parent.Right;
                 }
                 right.Color       = x.Parent.Color;
                 x.Parent.Color    = RBTreeNode <T> .COLOR.BLACK;
                 right.Right.Color = RBTreeNode <T> .COLOR.BLACK;
                 this.RotateLeft(x.Parent);
                 x = this.tree.root;
             }
         }
         else
         {
             RBTreeNode <T> left = x.Parent.Left;
             if (left.IsRed())
             {
                 left.Color     = RBTreeNode <T> .COLOR.BLACK;
                 x.Parent.Color = RBTreeNode <T> .COLOR.RED;
                 this.RotateRight(x.Parent);
                 left = x.Parent.Left;
             }
             if (left.Right.IsBlack() && left.Left.IsBlack())
             {
                 left.Color = RBTreeNode <T> .COLOR.RED;
                 x          = x.Parent;
             }
             else
             {
                 if (left.Left.IsBlack())
                 {
                     left.Right.Color = RBTreeNode <T> .COLOR.BLACK;
                     left.Color       = RBTreeNode <T> .COLOR.RED;
                     this.RotateRight(left);
                     left = x.Parent.Left;
                 }
                 left.Color      = x.Parent.Color;
                 x.Parent.Color  = RBTreeNode <T> .COLOR.BLACK;
                 left.Left.Color = RBTreeNode <T> .COLOR.BLACK;
                 this.RotateRight(x.Parent);
                 x = this.tree.root;
             }
         }
     }
     x.Color = RBTreeNode <T> .COLOR.BLACK;
 }
Exemple #3
0
        private void CheckBalance()
        {
            if (!IsRoot() && Parent.IsRed())
            {
                if (IsFatherLeftofGrandFather())
                {
                    RBTreeNode <T> rightUncle = GetRightUncle();
                    if (rightUncle != tree.NIL && rightUncle != null && rightUncle.IsRed())
                    {
                        ColorGrandFather(COLOR.RED);
                        rightUncle.Color = COLOR.BLACK;
                        Parent.Color     = COLOR.BLACK;
                        RBTreeNode <T> grandFather = GetGrandFather();
                        if (grandFather != null)
                        {
                            grandFather.CheckBalance();
                        }
                    }
                    else
                    {
                        if (this == Parent.Right)
                        {
                            RotateLeft(Parent);
                        }

                        ColorFather(COLOR.BLACK);
                        ColorGrandFather(COLOR.RED);
                        if (Parent.Parent != null)
                        {
                            RotateRight(Parent.Parent);
                        }
                    }
                }
                else
                {
                    RBTreeNode <T> leftUncle = GetLeftUncle();
                    if (leftUncle != tree.NIL && leftUncle != null && leftUncle.IsRed())
                    {
                        ColorGrandFather(COLOR.RED);
                        leftUncle.Color = COLOR.BLACK;
                        Parent.Color    = COLOR.BLACK;
                        RBTreeNode <T> grandFather2 = GetGrandFather();
                        if (grandFather2 != null)
                        {
                            grandFather2.CheckBalance();
                        }
                    }
                    else
                    {
                        if (this == Parent.Left)
                        {
                            RotateRight(Parent);
                        }

                        ColorFather(COLOR.BLACK);
                        ColorGrandFather(COLOR.RED);
                        if (Parent.Parent != null)
                        {
                            RotateLeft(Parent.Parent);
                        }
                    }
                }
            }

            tree.root.Color = COLOR.BLACK;
        }