6. • 1. Perform standard BST insertion and make the color of newly
inserted nodes as RED.
2. If x is root, change color of x as BLACK (Black height of
complete tree increases by 1).
3. Do following if color of x’s parent is not BLACK or x is not
root.
A. If x’s uncle is RED (Grand parent must have been black)
i. Change color of parent and uncle as BLACK.
ii. color of grand parent as RED.
iii. Change x = x’s grandparent, repeat steps 2 and 3 for new x.
7.
8. B. If x’s uncle is BLACK, then there can be four configurations for
x, x’s parent (p) and x’s grandparent (g) (This is similar to AVL
Tree)
i. Determine the configuration:
• Case 1. Left Left Case (p is left child of g and x is left child
of p).
• Case 2. Left Right Case (p is left child of g and x is right
child of p).
• Case 3. Right Right Case.
• Case 4. Right Left Case.
ii. Change x = x’s parent, repeat steps 2 and 3 for new x.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22. • We have detected a need for balance when X is red and his parent too.
• • If X has a red uncle: colour the parent and uncle black, and grandparent red.
• Then replace X by grandparent to see if X’s parent is red.
• • If X is a left child and has a black uncle: colour the parent black and the grandparent red, then
rotateRight(X.parent.parent)
• • If X is a right child and has a black uncle, then rotateLeft(X.parent) and grandparent red, then
rotateRight(X.parent.parent)