示例#1
0
        internal bool InsertValue(T newValue)
        {
            RBTreeNode <T> rBTreeNode = this;

            if (tree.NIL != rBTreeNode.Search(rBTreeNode, newValue))
            {
                return(false);
            }

            while (rBTreeNode != tree.NIL)
            {
                if (tree.comparer.Compare(newValue, rBTreeNode.value) <= 0)
                {
                    if (rBTreeNode.Left == tree.NIL)
                    {
                        break;
                    }

                    rBTreeNode = rBTreeNode.Left;
                }
                else
                {
                    if (rBTreeNode.Right == tree.NIL)
                    {
                        break;
                    }

                    rBTreeNode = rBTreeNode.Right;
                }
            }

            if (tree.comparer.Compare(newValue, rBTreeNode.value) <= 0)
            {
                RBTreeNode <T> rBTreeNode2 = new RBTreeNode <T>(tree, newValue);
                rBTreeNode2.Color  = COLOR.RED;
                rBTreeNode.Left    = rBTreeNode2;
                rBTreeNode2.Parent = rBTreeNode;
                rBTreeNode2.Left   = rBTreeNode2.Right = tree.NIL;
                rBTreeNode2.CheckBalance();
            }
            else
            {
                RBTreeNode <T> rBTreeNode3 = new RBTreeNode <T>(tree, newValue);
                rBTreeNode3.Color  = COLOR.RED;
                rBTreeNode.Right   = rBTreeNode3;
                rBTreeNode3.Parent = rBTreeNode;
                rBTreeNode3.Left   = rBTreeNode3.Right = tree.NIL;
                rBTreeNode3.CheckBalance();
            }

            return(true);
        }
示例#2
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;
 }
示例#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;
        }