Slavik9510

Untitled

Nov 1st, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1.             Node parent = null;
  2.             Node grandParent = null;
  3.  
  4.             while ((node != Root) && (node.Color != Color.Black) && (node.Parent.Color == Color.Red))
  5.             {
  6.                 parent = node.Parent;
  7.                 grandParent = node.Parent.Parent;
  8.  
  9.                 /*  Case : A
  10.                     Parent of pt is left child
  11.                     of Grand-parent of pt */
  12.                 if (parent == grandParent.Left)
  13.                 {
  14.  
  15.                     Node uncle = grandParent.Right;
  16.  
  17.                     /* Case : 1
  18.                        The uncle of pt is also red
  19.                        Only Recoloring required */
  20.                     if (uncle != null && uncle.Color == Color.Red)
  21.                     {
  22.                         grandParent.Color = Color.Red;
  23.                         parent.Color = Color.Black;
  24.                         uncle.Color = Color.Black;
  25.                         node = grandParent;
  26.                     }
  27.                     else
  28.                     {
  29.                         /* Case : 2
  30.                            pt is right child of its parent
  31.                            Left-rotation required */
  32.                         if (node == parent.Right)
  33.                         {
  34.                             LeftRotate(parent);
  35.                             node = parent;
  36.                             parent = node.Parent;
  37.                         }
  38.  
  39.                         /* Case : 3
  40.                            pt is left child of its parent
  41.                            Right-rotation required */
  42.                         RightRotate(grandParent);
  43.                         (parent.Color, grandParent.Color) = (grandParent.Color, parent.Color);
  44.                         node = parent;
  45.                     }
  46.                 }
  47.                 /* Case : B
  48.                    Parent of pt is right child
  49.                    of Grand-parent of pt */
  50.                 else
  51.                 {
  52.                     Node uncle = grandParent.Left;
  53.  
  54.                     /*  Case : 1
  55.                         The uncle of pt is also red
  56.                         Only Recoloring required */
  57.                     if ((uncle != null) && (uncle.Color == Color.Red))
  58.                     {
  59.                         grandParent.Color = Color.Red;
  60.                         parent.Color = Color.Black;
  61.                         uncle.Color = Color.Black;
  62.                         node = grandParent;
  63.                     }
  64.                     else
  65.                     {
  66.                         /* Case : 2
  67.                            pt is left child of its parent
  68.                            Right-rotation required */
  69.                         if (node == parent.Left)
  70.                         {
  71.                             RightRotate(parent);
  72.                             node = parent;
  73.                             parent = node.Parent;
  74.                         }
  75.  
  76.                         /* Case : 3
  77.                            pt is right child of its parent
  78.                            Left-rotation required */
  79.                         LeftRotate(grandParent);
  80.                         (parent.Color, grandParent.Color) = (grandParent.Color, parent.Color);
  81.                         node = parent;
  82.                     }
  83.                 }
  84.             }
  85.             Root.Color = Color.Black;
Advertisement
Add Comment
Please, Sign In to add comment