private RedBlackNode <T> DeleteRebalanceLeft(RedBlackNode <T> root, ref bool done) { // Rotation to reduce the red sibling case to the easier to handle black sibling case RedBlackNode <T> parent = root; RedBlackNode <T> sibling = root.Right; if (IsNodeRed(sibling)) { root = root.RotateLeft(); sibling = parent.Right; } if (sibling != null) { if (!IsNodeRed(sibling.Left) && !IsNodeRed(sibling.Right)) { if (IsNodeRed(parent)) { done = true; } parent.SetColour(Colour.Black); sibling.SetColour(Colour.Red); } else { bool parentIsRed = parent.IsRed; bool sameRoot = root == parent; if (IsNodeRed(sibling.Right)) { parent = parent.RotateLeft(); } else { parent.Right = parent.Right.RotateRight(); parent = parent.RotateLeft(); } parent.SetIsRed(parentIsRed); parent.Left.SetColour(Colour.Black); parent.Right.SetColour(Colour.Black); if (sameRoot) { root = parent; } else { root.Left = parent; } done = true; } } return(root); }
/// <summary> /// Rotates the tree rooted at this node in a clockwise manner and recolours the root and pivot nodes accordingly. /// </summary> /// <returns>The new root of the tree</returns> internal RedBlackNode <T> RotateRight() { RedBlackNode <T> pivot = this.Left; this.Left = pivot.Right; pivot.Right = this; //fix heights pivot.ResetHeight(); this.ResetHeight(); pivot.Right.SetColour(Colour.Red); pivot.SetColour(Colour.Black); return(pivot); }
/// <summary> /// Moves a black root node down to it's two children and colours the root red /// </summary> /// <param name="root"></param> private static void MoveBlackDown(RedBlackNode <T> root) { root.SetColour(Colour.Red); root.Left.SetColour(Colour.Black); root.Right.SetColour(Colour.Black); }