Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Node parent = null;
- Node grandParent = null;
- while ((node != Root) && (node.Color != Color.Black) && (node.Parent.Color == Color.Red))
- {
- parent = node.Parent;
- grandParent = node.Parent.Parent;
- /* Case : A
- Parent of pt is left child
- of Grand-parent of pt */
- if (parent == grandParent.Left)
- {
- Node uncle = grandParent.Right;
- /* Case : 1
- The uncle of pt is also red
- Only Recoloring required */
- if (uncle != null && uncle.Color == Color.Red)
- {
- grandParent.Color = Color.Red;
- parent.Color = Color.Black;
- uncle.Color = Color.Black;
- node = grandParent;
- }
- else
- {
- /* Case : 2
- pt is right child of its parent
- Left-rotation required */
- if (node == parent.Right)
- {
- LeftRotate(parent);
- node = parent;
- parent = node.Parent;
- }
- /* Case : 3
- pt is left child of its parent
- Right-rotation required */
- RightRotate(grandParent);
- (parent.Color, grandParent.Color) = (grandParent.Color, parent.Color);
- node = parent;
- }
- }
- /* Case : B
- Parent of pt is right child
- of Grand-parent of pt */
- else
- {
- Node uncle = grandParent.Left;
- /* Case : 1
- The uncle of pt is also red
- Only Recoloring required */
- if ((uncle != null) && (uncle.Color == Color.Red))
- {
- grandParent.Color = Color.Red;
- parent.Color = Color.Black;
- uncle.Color = Color.Black;
- node = grandParent;
- }
- else
- {
- /* Case : 2
- pt is left child of its parent
- Right-rotation required */
- if (node == parent.Left)
- {
- RightRotate(parent);
- node = parent;
- parent = node.Parent;
- }
- /* Case : 3
- pt is right child of its parent
- Left-rotation required */
- LeftRotate(grandParent);
- (parent.Color, grandParent.Color) = (grandParent.Color, parent.Color);
- node = parent;
- }
- }
- }
- Root.Color = Color.Black;
Advertisement
Add Comment
Please, Sign In to add comment