Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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???

3 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

4 CS-2851 Dr. Mark L. Hornick 4 A valid Red/Black Tree 10 20 35 5 30 28 37 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

5 CS-2851 Dr. Mark L. Hornick 5 Not a valid Red/Black Tree 10 20 35 5 15 12 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

6 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…

7 CS-2851 Dr. Mark L. Hornick 7 Okasaki’s Insertion Method: Red Parent Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html 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???

8 CS-2851 Dr. Mark L. Hornick 8 Okasaki’s Insertion Method: LL/RR cases of Red parent Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html 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

9 CS-2851 Dr. Mark L. Hornick 9 Okasaki’s Insertion Method: LR/RL cases of Red parent Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html 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

10 CS-2851 Dr. Mark L. Hornick 10 After rotation, we’re two steps closer to the root Diagram from http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-tree.html 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

11 CS-2851 Dr. Mark L. Hornick 11 Exercise Add 279 to this tree:


Download ppt "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."

Similar presentations


Ads by Google