1 / 11

Red/Black vs AVL trees

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)

kaida
Download Presentation

Red/Black vs AVL trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Okasaki’s Insertion Method for Red/Black balancingA step-by-step procedure for maintaining balance through application of rotations CS-2851Dr. 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-2851Dr. Mark L. Hornick

  3. 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

  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 20 35 10 30 5 37 28 CS-2851Dr. 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 20 35 10 5 15 12 CS-2851Dr. 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-2851Dr. Mark L. Hornick

  7. 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

  8. 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

  9. 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

  10. 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

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

More Related