Exemplo n.º 1
0
 private void FixUp(TreeNode node)
 {
     while (node != Root && node.Color == 0)
     {
         if (node == node.Parent.LeftChild)
         {
             var w = node.Parent.RightChild;
             if (w.Color == 1)
             {
                 w.Color           = 0;
                 node.Parent.Color = 1;
                 LeftTurn(node.Parent);
                 w = node.Parent.RightChild;
             }
             if (w.LeftChild.Color == 0 && w.RightChild.Color == 0)
             {
                 w.Color = 1;
                 node    = node.Parent;
             }
             else
             {
                 if (w.RightChild.Color == 0)
                 {
                     w.LeftChild.Color = 0;
                     w.Color           = 1;
                     RightTurn(w);
                     w = node.Parent.RightChild;
                 }
                 w.Color            = node.Parent.Color;
                 node.Parent.Color  = 0;
                 w.RightChild.Color = 0;
                 LeftTurn(node.Parent);
                 node = Root;
             }
         }
         else
         {
             var w = node.Parent.LeftChild;
             if (w.Color == 1)
             {
                 w.Color           = 0;
                 node.Parent.Color = 1;
                 RightTurn(node.Parent);
                 w = node.Parent.LeftChild;
             }
             if (w.RightChild.Color == 0 && w.LeftChild.Color == 0)
             {
                 w.Color = 1;
                 node    = node.Parent;
             }
             else
             {
                 if (w.LeftChild.Color == 0)
                 {
                     w.RightChild.Color = 0;
                     w.Color            = 1;
                     LeftTurn(w);
                     w = node.Parent.LeftChild;
                 }
                 w.Color           = node.Parent.Color;
                 node.Parent.Color = 0;
                 w.LeftChild.Color = 0;
                 RightTurn(node.Parent);
                 node = Root;
             }
         }
     }
     node.Color = 0;
 }
Exemplo n.º 2
0
 public void Insert(TreeNode node)
 {
     if (Root == null)
     {
         Root            = node;
         Root.Color      = 0;
         Root.LeftChild  = null;
         Root.RightChild = null;
         Root.Parent     = null;
     }
     else
     {
         var addedNode = SearchNull(Root, node);
         while (addedNode != Root && addedNode.Parent.Color == 1)
         {
             if (addedNode.Parent == addedNode.Parent.Parent.LeftChild)  // если нашло левый лист nil
             {
                 var uncle = addedNode.Parent.Parent.RightChild;         // y - дядя
                 if (uncle != null && uncle.Color == 1)
                 {
                     addedNode.Parent.Color        = 0;
                     uncle.Color                   = 0;
                     addedNode.Parent.Parent.Color = 1;
                     addedNode = addedNode.Parent.Parent;
                 }
                 else                                                      //дядя черный
                 {
                     if (addedNode == addedNode.Parent.RightChild)
                     {
                         addedNode = addedNode.Parent;
                         LeftTurn(addedNode);
                     }
                     addedNode.Parent.Color        = 0;
                     addedNode.Parent.Parent.Color = 1;
                     RightTurn(addedNode.Parent.Parent);
                 }
             }
             else
             {
                 var y = addedNode.Parent.Parent.LeftChild;
                 if (y != null && y.Color == 1)
                 {
                     addedNode.Parent.Color = 0;
                     y.Color = 0;
                     addedNode.Parent.Parent.Color = 1;
                     addedNode = addedNode.Parent.Parent;
                 }
                 else
                 {
                     if (addedNode == addedNode.Parent.Parent.LeftChild)
                     {
                         addedNode = addedNode.Parent;
                         RightTurn(addedNode);
                     }
                     addedNode.Parent.Color        = 0;
                     addedNode.Parent.Parent.Color = 1;
                     LeftTurn(addedNode.Parent.Parent);
                 }
             }
         }
     }
     Root.Color = 0;
 }