160 likes | 601 Views
Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of rotations. 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)
E N D
Okasaki’s Insertion Method for Red/Black balancingA step-by-step procedure for maintaining balance through application of rotations CS-2851Dr. Mark L. Hornick
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-2851Dr. Mark L. Hornick
Rules of Red/Black Trees • Every node is either red or black • A Node is a non-null leaf • A NIL is a null leaf • The root node is always 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-2851Dr. Mark L. Hornick
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 20 35 10 30 5 37 28 CS-2851Dr. Mark L. Hornick
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 20 35 10 5 15 12 CS-2851Dr. Mark L. Hornick
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-2851Dr. Mark L. Hornick
Okasaki’s Insertion Method:Red Parent • 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??? Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html CS-2851Dr. Mark L. Hornick
Okasaki’s Insertion Method:LL/RR cases of Red parent • 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 Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html CS-2851Dr. Mark L. Hornick
Okasaki’s Insertion Method:LR/RL cases of Red parent • 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 Rotate z right Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html CS-2851Dr. Mark L. Hornick
After rotation, we’re two steps closer to the root • 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 Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html CS-2851Dr. Mark L. Hornick
Exercise Add 279 to this tree: CS-2851Dr. Mark L. Hornick