Beispiel #1
0
 private void Insert(int data, NodeRB node, NodeRB parent)
 {
     if (node.Key == null)
     {
         node.Key = data;
         node.Sum++;
         node.Color  = RBColor.Red;
         node.Parent = parent;
         InsertBalanser(node);
         return;
     }
     if (node.Key == data)
     {
         node.Sum++;
         return;
     }
     if (data < node.Key)
     {
         if (node.Left == null)
         {
             node.Left = new NodeRB();
         }
         Insert(data, node.Left, node);
         //balanser
     }
     else
     {
         if (node.Right == null)
         {
             node.Right = new NodeRB();
         }
         Insert(data, node.Right, node);
         //balanser
     }
 }
Beispiel #2
0
 public void Insert(int data)
 {
     if (Tree == null)
     {
         Tree       = new NodeRB();
         Tree.Key   = data;
         Tree.Color = RBColor.Black;
         Tree.Sum++;
         return;
     }
     if (Tree.Key == data)
     {
         Tree.Sum++;
         return;
     }
     if (data < Tree.Key)
     {
         if (Tree.Left == null)
         {
             Tree.Left = new NodeRB();
         }
         Insert(data, Tree.Left, Tree);
         //rebalans
     }
     else
     {
         if (Tree.Right == null)
         {
             Tree.Right = new NodeRB();
         }
         Insert(data, Tree.Right, Tree);
         //rebalans
     }
 }
Beispiel #3
0
 private void InsertBalanser(NodeRB node)
 {
     while (node != Tree && node.Parent.Color == RBColor.Red)
     {
         if (node.Parent == node.Parent.Parent.Left)
         {
             NodeRB Right_Uncle = node.Parent.Parent.Right;
             if (Right_Uncle != null && Right_Uncle.Color == RBColor.Red) //uncle - red
             {
                 node.Parent.Color        = RBColor.Black;
                 Right_Uncle.Color        = RBColor.Black;
                 node.Parent.Parent.Color = RBColor.Red;
                 node = node.Parent.Parent;
             }
             else   //uncle - black
             {
                 if (node == node.Parent.Right)
                 {
                     node = node.Parent;
                     LeftRotate(node);
                 }
                 node.Parent.Color        = RBColor.Black;
                 node.Parent.Parent.Color = RBColor.Red;
                 RightRotate(node.Parent.Parent);
             }
         }
         else //mirorr
         {
             NodeRB Left_Uncle = node.Parent.Parent.Left;
             if (Left_Uncle != null && Left_Uncle.Color == RBColor.Red) // uncle - red
             {
                 node.Parent.Color        = RBColor.Black;
                 Left_Uncle.Color         = RBColor.Black;
                 node.Parent.Parent.Color = RBColor.Red;
                 node = node.Parent.Parent;
             }
             else
             {
                 if (node == node.Parent.Left)
                 {
                     node = node.Parent;
                     RightRotate(node);
                 }
                 node.Parent.Color        = RBColor.Black;
                 node.Parent.Parent.Color = RBColor.Red;
                 LeftRotate(node.Parent.Parent);
             }
         }
     }
     Tree.Color = RBColor.Black;
 }
Beispiel #4
0
 private NodeRB FindMax(NodeRB node)
 {
     if (node == null)
     {
         return(null);
     }
     if (node.Right != null)
     {
         return(FindMax(node.Right));
     }
     else
     {
         return(node);
     }
 }
Beispiel #5
0
 private NodeRB FindMin(NodeRB node)
 {
     if (node == null)
     {
         return(null);
     }
     if (node.Left != null)
     {
         return(FindMin(node.Left));
     }
     else
     {
         return(node);
     }
 }
Beispiel #6
0
 private NodeRB Find(int data, NodeRB node)
 {
     if (node == null)
     {
         return(null);
     }
     if (node.Key == data)
     {
         return(node);
     }
     if (data < node.Key)
     {
         return(Find(data, node.Left));
     }
     return(Find(data, node.Right));
 }
Beispiel #7
0
            public void Delete(int data)
            {
                NodeRB DelItem = Find(data);

                if (DelItem == null)
                {
                    Console.WriteLine("ERROR - item not faund");
                }
                else
                {
                    if (DelItem.Sum > 1)
                    {
                        DelItem.Sum--;
                    }
                    else
                    {
                        Delete(DelItem);
                    }
                }
            }
Beispiel #8
0
            private void LeftRotate(NodeRB X)
            {
                NodeRB Y = X.Right;

                X.Right = Y.Left;

                if (Y.Left != null)
                {
                    Y.Left.Parent = X;
                }

                if (Y != null)
                {
                    Y.Parent = X.Parent;
                }



                if (X.Parent == null)
                {
                    Tree = Y;
                }
                else
                {
                    if (X.Parent.Left == X)
                    {
                        X.Parent.Left = Y;
                    }

                    if (X.Parent.Right == X)
                    {
                        X.Parent.Right = Y;
                    }
                }
                Y.Left = X;

                if (X != null)
                {
                    X.Parent = Y;
                }
            }
Beispiel #9
0
            private void RightRotate(NodeRB Y)
            {
                NodeRB X = Y.Left;

                Y.Left = X.Right;

                if (X != null)
                {
                    X.Parent = Y.Parent;
                }

                if (X.Right != null)
                {
                    X.Right.Parent = Y;
                }

                if (Y.Parent == null)
                {
                    Tree = X;
                }
                else
                {
                    if (Y.Parent.Left == Y)
                    {
                        Y.Parent.Left = X;
                    }

                    if (Y.Parent.Right == Y)
                    {
                        Y.Parent.Right = X;
                    }
                }
                X.Right = Y;

                if (Y != null)
                {
                    Y.Parent = X;
                }
            }
Beispiel #10
0
            private void Delete(NodeRB node)
            {
                NodeRB  DeletingNode = node;
                NodeRB  ZamenaItem;
                RBColor FirstColor = node.Color;

                bool?myPosition;

                if (node.Parent == null)
                {
                    myPosition = null;
                }
                else if (node.Parent.Left == node)
                {
                    myPosition = false;
                }
                else
                {
                    myPosition = true;
                }
                //1
                if (node.Left == null && node.Right == null)
                {
                    if (myPosition == null)
                    {
                        node.Key = null;
                        node.Sum = 0;
                    }
                    if (myPosition == false)
                    {
                        node.Parent.Left = null;
                    }
                    if (myPosition == true)
                    {
                        node.Parent.Right = null;
                    }

                    node.Parent = null;
                }

                //2
                if (node.Left != null && node.Right == null)
                {
                    if (myPosition == null)
                    {
                        Tree             = node.Left;
                        node.Left.Parent = null;
                        //node.Left = null;
                    }
                    if (myPosition == false)
                    {
                        node.Parent.Left = node.Left;
                        node.Left.Parent = node.Parent;
                        //node.Parent = null;
                        //node.Left = null;
                    }
                    if (myPosition == true)
                    {
                        node.Parent.Right = node.Left;
                        node.Left.Parent  = node.Parent;
                        //node.Parent = null;
                        //node.Left = null;
                    }
                }
                if (node.Left == null && node.Right != null)
                {
                    if (myPosition == null)
                    {
                        Tree = node.Right;
                        node.Right.Parent = null;
                        //node.Right = null;
                    }
                    if (myPosition == false)
                    {
                        node.Parent.Left  = node.Right;
                        node.Right.Parent = node.Parent;
                        //node.Parent = null;
                        //node.Right = null;
                    }
                    if (myPosition == true)
                    {
                        node.Parent.Right = node.Right;
                        node.Right.Parent = node.Parent;
                        node.Parent       = null;
                        node.Right        = null;
                    }
                }
                //3
                if (node.Left != null && node.Right != null)
                {
                    ZamenaItem = FindMin(node.Right);
                    node.Key   = ZamenaItem.Key;
                    node.Sum   = ZamenaItem.Sum;

                    if (node.Color == RBColor.Red)
                    {
                        node.Color = ZamenaItem.Color;
                        Delete(ZamenaItem);
                        return;
                    }
                    if (node.Color == RBColor.Black && ZamenaItem.Color == RBColor.Red)
                    {
                        node.Color = RBColor.Black;
                        Delete(ZamenaItem);
                        return;
                    }
                    if (node.Color == RBColor.Black && ZamenaItem.Color == RBColor.Black)
                    {
                        DeleteBalanser(node);
                    }
                }
            }
Beispiel #11
0
 private void DeleteBalanser(NodeRB node)
 {
     while (node != null && node != Tree && node.Color == RBColor.Black)
     {
         if (node.Parent.Left == node)
         {
             NodeRB RightBraser = node.Parent.Right;
             //1
             if (node.Parent.Right.Color == RBColor.Red)
             {
                 node.Parent.Color       = RBColor.Red;
                 node.Parent.Right.Color = RBColor.Black;
                 LeftRotate(node.Parent);
                 RightBraser = node.Parent.Right;
             }
             //2
             if (RightBraser.Color == RBColor.Black)
             {
                 if (RightBraser.Left.Color == RBColor.Black && RightBraser.Right.Color == RBColor.Black)
                 {
                     RightBraser.Color = RBColor.Red;
                     node = node.Parent;
                 }
                 if (RightBraser.Left.Color == RBColor.Red && RightBraser.Right.Color == RBColor.Black)
                 {
                     RightBraser.Color      = RBColor.Red;
                     RightBraser.Left.Color = RBColor.Black;
                     RightRotate(RightBraser.Left);
                     RightBraser = node.Parent.Right;
                 }
                 if (RightBraser.Right.Color == RBColor.Red)
                 {
                     RightBraser.Color       = node.Parent.Color;
                     node.Parent.Color       = RBColor.Black;
                     RightBraser.Right.Color = RBColor.Black;
                     LeftRotate(node.Parent);
                     break;
                 }
             }
         }
         else //mirror
         {
             NodeRB LeftBraser = node.Parent.Left;
             //1
             if (node.Parent.Left.Color == RBColor.Red)
             {
                 node.Parent.Color       = RBColor.Red;
                 node.Parent.Right.Color = RBColor.Black;
                 RightRotate(node.Parent);
                 LeftBraser = node.Parent.Left;
             }
             //2
             if (LeftBraser.Color == RBColor.Black)
             {
                 if (LeftBraser.Left.Color == RBColor.Black && LeftBraser.Right.Color == RBColor.Black)
                 {
                     LeftBraser.Color = RBColor.Red;
                     node             = node.Parent;
                 }
                 if (LeftBraser.Right.Color == RBColor.Red && LeftBraser.Left.Color == RBColor.Black)
                 {
                     LeftBraser.Color       = RBColor.Red;
                     LeftBraser.Right.Color = RBColor.Black;
                     LeftRotate(LeftBraser.Right);
                     LeftBraser = node.Parent.Left;
                 }
                 if (LeftBraser.Left.Color == RBColor.Red)
                 {
                     LeftBraser.Color      = node.Parent.Color;
                     node.Parent.Color     = RBColor.Black;
                     LeftBraser.Left.Color = RBColor.Black;
                     RightRotate(node.Parent);
                     break;
                 }
             }
         }
     }
     if (Tree != null)
     {
         Tree.Color = RBColor.Black;
     }
 }