예제 #1
0
        private RBTreeNode <T> TreeSuccessor(RBTreeNode <T> T)
        {
            if (T == this.tree.NIL)
            {
                return(null);
            }
            if (T.Right != this.tree.NIL)
            {
                return(this.TreeMinimum(T.Right));
            }
            RBTreeNode <T> parent = T.Parent;

            while (parent != null && T == parent.Right)
            {
                T      = parent;
                parent = parent.Parent;
            }
            return(parent);
        }
예제 #2
0
 private bool IsBlack(RBTreeNode <T> T)
 {
     return(T.Color == RBTreeNode <T> .COLOR.BLACK);
 }
예제 #3
0
 private bool IsRed(RBTreeNode <T> T)
 {
     return(T.Color == RBTreeNode <T> .COLOR.RED);
 }
예제 #4
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;
 }
예제 #5
0
        public void Delete(T i)
        {
            RBTreeNode <T> z = this.Search(this.tree.root, i);

            this.RBDelete(z);
        }
예제 #6
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;
 }
예제 #7
0
 private bool IsRoot(RBTreeNode <T> T)
 {
     return(T.Parent == null);
 }
예제 #8
0
        public RedBlackTree()
        {
            this.NIL = RBTreeNode <T> .MakeNilNode(this);

            this.root = null;
        }
예제 #9
0
        public void Delete(T i)
        {
            RBTreeNode <T> z = Search(tree.root, i);

            RBDelete(z);
        }
예제 #10
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;
        }