CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of rotations
CS-2851 Dr. Mark L. Hornick 2 Red/Black vs AVL trees Every AVL tree is also a Red/Black tree A Red/Black tree is not necessarily an AVL tree AVL tree height is log2(n) Red/Black tree height is ≤2*log2(n+1) Can be worse than AVL, but not much So why use Red/Black trees???
CS-2851 Dr. Mark L. Hornick 3 Rules of Red/Black Trees 1. Every node is either red or black A Node is a non-null leaf A NIL is a null leaf 2. The root node is always black 3. Every leaf (NIL) is black 4. If a node is red, then both its children are black two red nodes may not be adjacent But if a node is black, its children can be red or black 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes
CS-2851 Dr. Mark L. Hornick 4 A valid Red/Black Tree Every node is either red or black The root is black Every leaf (NIL) is black If a node is red, then both its children are black two red nodes may not be adjacent But if a node is black, its children can be red or black For each node, all paths from the node to descendant leaves contain the same number of black nodes
CS-2851 Dr. Mark L. Hornick 5 Not a valid Red/Black Tree Every node is either red or black The root is black Every leaf (NIL) is black If a node is red, then both its children are black two red nodes may not be adjacent But if a node is black, its children can be red or black For each node, all paths from the node to descendant leaves contain the same number of black nodes
CS-2851 Dr. Mark L. Hornick 6 Okasaki’s Insertion Method: No parent or black parent Always insert new nodes as Red First, determine where the new node has to be inserted If tree is empty; insert as root Change color to Black (rule 2) Done If tree is not empty; insert as child of existing node If Parent is Black; leave child Red Done Otherwise…
CS-2851 Dr. Mark L. Hornick 7 Okasaki’s Insertion Method: Red Parent Diagram from Rule 4 was violated: A Red parent’s children must be Black Invoke “fixup” on new child One of four different cases at right Right child of Right parent Left child of Right parent Right child of Left parent Left child of Left parent Note: grandparent is always Black Why???
CS-2851 Dr. Mark L. Hornick 8 Okasaki’s Insertion Method: LL/RR cases of Red parent Diagram from 1 rotation to middle case Left child of left parent Color child black Rotate grandparent (z) right grandparent (z) becomes sibling child Right child of right parent Color child black Rotate grandparent (x) left Grandparent (x) becomes sibling child
CS-2851 Dr. Mark L. Hornick 9 Okasaki’s Insertion Method: LR/RL cases of Red parent Diagram from 2 rotations to middle case Right child of left parent Color parent black Rotate parent (y) left Rotate grandparent (z) right Child becomes parent Grandparent & parent become sibling children Left child of right parent Color parent black Rotate parent (z) right Rotate grandparent (x) left Child becomes parent Grandparent & parent become sibling children Rotate y left Rotate x left Rotate z right
CS-2851 Dr. Mark L. Hornick 10 After rotation, we’re two steps closer to the root Diagram from If y’s new parent is black Done If y is root Color it black Increases the number of black nodes on every path Does not violate property 5 Done Otherwise repeat Invoke “fixup” on y Recurse until y’s parent is black y is root
CS-2851 Dr. Mark L. Hornick 11 Exercise Add 279 to this tree: