Lecture 12: AA Trees, Treaps
Total Page:16
File Type:pdf, Size:1020Kb
AA-Trees! The implementation and number of rotation cases in Red-Black Trees is complex! AA-trees:! •! fewer rotation cases so easier to code, especially deletions (eliminates about half of the rotation cases)! •! named after its inventor Arne Andersson (1993)," Lecture 12: AA Trees! an optimization over original definition of Binary B-trees" Treaps! (BB-trees) by Bayer (1971)! AA-trees still have O(log n) searches in the worst- case, although they are slightly less efficient empirically! Demo: http://www.cis.ksu.edu/~howell/viewer/viewer.html! [McCollam]! AA-Tree Ordering Properties! An AA-Tree Example! An AA-Tree is a binary search tree with all the 30 ordering properties of a red-black tree: ! 1.! Every node is colored either red or black! 15 70 2.! The root is black! 50 85 3.! External nodes are black! 5 20 4.! If a node is red, its children must be black! 10 35 60 80 90 5.! All paths from any node to a descendent leaf must contain the same number of black nodes 65 (black-height, not including the node itself)! 40 55 PLUS! No left red children!! 6.! Left children may not be red! Half of red-black tree rotation cases eliminated!! (Which M-way trees are AA-trees equivalent to?)! [McCollam]! [McCollam]! Representation of Balancing Info! Redefinition of “Leaf” ! The level of a node (instead of color) is used as Both the terms leaf and level are redefined:! balancing info! A leaf in an AA-tree is a node with no black •! “red” nodes are simply nodes that located at the same internal-node as children! level as their parents! For the tree on the previous slide:! 30 70 30 70 15 50 60 85 5 10 20 15 50 60 85 35 40 55 65 80 90 all leaf nodes! 5 10 20 35 40 55 65 80 90 [McCollam]! [McCollam]! Redefinition of “Level” ! Implications of Ordering Properties! The level of a node in an AA-tree is:! 1.!Horizontal links are right links! •! leaf nodes are at level 1! •! because only right children may be red! •! red nodes are at the level of their parent! •! black nodes are at one less than the level of their parent! 2.!There may not be double horizontal links! •! as in red-black trees, a black node corresponds to a level change •! because there cannot be double red nodes! in the corresponding 2-3 tree! 30 70 30 70 Level 3! 15 50 60 85 Level 2! 15 50 60 85 5 10 20 35 40 80 90 5 55 65 Level 1! 10 20 35 40 55 65 80 90 [McCollam]! [McCollam]! Implications of Ordering Properties! Implications of Ordering Properties! 3.!Nodes at level 2 or higher must have two children! 5.! Any simple path from a black node to a leaf 4.!If a node does not have a right horizontal link, its contains one black node on each level! two children are at the same level! 30 70 30 70 Level 3! Level 3! Level 2! 15 50 60 85 Level 2! 15 50 60 85 5 5 Level 1! 10 20 35 40 55 65 80 90 Level 1! 10 20 35 40 55 65 80 90 [McCollam]! [McCollam]! Example: Insert 45! Example: Insert 45! First, insert as for simple binary search tree! After insert to right of 40:! Newly inserted node is red! Problem: double right horizontal links starting at 35, need to split! 45 30 70 30 70 15 50 60 85 15 50 60 85 5 5 20 10 20 35 40 55 65 80 90 10 35 40 45 55 65 80 90 [McCollam]! [McCollam]! Split: Removing Double Reds! Example: Insert 45! Problem: With G X P G inserted, there After split at 35:! are two reds in a Problem: left horizontal link at 50 A B row! is introduced, need to skew! P Split is a simple X G left rotation 30 70 between X and P! A B P 15 40 50 60 85 P’s level increases X G 5 10 20 in the AA-tree ! 35 45 55 65 80 90 A B [McCollam]! [McCollam]! P X P G Skew: Removing Left Horizontal Link! Example: Insert 45! X G split! A B Problem: left horizontal A B P X After skew at 50:! link in AA-tree! Problem: double right horizontal links starting at , need to split! A 40 Skew is a simple right B C rotation between X and P! P X 30 70 P remains at the same A B C level as X! 15 40 50 60 85 P X 5 20 10 35 45 55 65 80 90 A B C [McCollam]! [McCollam]! P X P X P skew! X P G Example: Insert 45! Example: Insert 45! X G A B C A B C split! A B A After split at 40:! After skew at 70:! B Problem: left horizontal link at 70 introduced Problem: double right horizontal links (50 is now on same level as 70), need to skew! starting at 30, need to split! 30 50 70 30 50 70 15 40 60 85 15 40 60 85 5 20 5 20 10 35 45 55 65 80 90 10 35 45 55 65 80 90 [McCollam]! [McCollam]! Example: Insert 45! AATree::Insert()! After split at :! void AATree::! 30 insert(Link &root, Node &add) {! Insertion is complete (finally!)! if (root == NULL) // have found where to insert y! root = add;! else if (add!key < root!key) // <= if duplicate ok! 50 insert(root!left, add);! else if (add!key > root!key)! insert(root!right, add);! 30 70 // else handle duplicate if not ok! 15 40 60 85 skew(root); // do skew and split at each level! split(root);! }! 5 20 10 35 45 55 65 80 90 [McCollam]! [McCollam]! Skew: Remove Left Horizontal Link! Split: Remove Double Reds! P P X P X skew! X P G X G A B C A B C split! A B A B void AATree::skew(Link &root) { // root = X! if (root!left!level == root!level)! void AATree::split(Link &root) { // root = X! rotate_right(root);! if (root!right!right!level == root!level)! }! rotate_left(root);! }! [Lee,Andersson]! [Lee,Andersson]! AA-Tree Removal! 50 More on Skew and Split! 30 70 15 40 60 85 5 Skew may cause double reds! 10 20 35 45 55 65 80 90 Rules:! •!first we apply skew, then we do split if necessary! 1.!if node to be deleted is a red leaf, e.g., 10, remove leaf, done! After a split, the middle node increases a level, 2.!if it is parent to a single internal node, e.g., 5, it must be black; replace with its child (must be red) and recolor child black! which may create a problem for the original parent! •! parent may need to skew and split! 3.!if it has two internal-node children, swap node to be deleted with its in-order successor! •! if in-order successor is red (must be a leaf), remove leaf, done! •! if in-order successor is a single child parent, apply second rule! In both cases the resulting tree is a legit AA-tree" (we haven’t changed the number of black nodes in paths)! 3.!if in-order successor is a black leaf, or if the node to be deleted itself is a black leaf, things get complicated . .! [Lee]! Black Leaf Removal! Black Leaf Removal! p! Follow the path from the removed node to the root! 30 70 Level 3! At each node p with 2 internal-node children do:! •! if either of p’s children is two levels below p! Level 2! 15 50 60 85 •! decrease the level of p by one! 5 •! if p’s right child was a red node, decrease its level also! Level 1! 20 35 55 65 80 90 •! skew(p); skew(p!right); skew(p!right!right);! •! split(p); split(p!right);! Remove 5:" decrease 15’s level ! In the worst case, deleting one leaf node, e.g., 15, could p! 30 70 cause six nodes to all be at one level, connected by Level 3! horizontal right links! Level 2! 15 50 60 85 •! but the worst case can be" 30 70 resolved by 3 calls to" 15 20 50 85 Level 1! 35 skew(), followed by 2" 60 90 55 65 80 90 calls to split()!! [Andersson,McCollam]! [Andersson,McCollam]! Black Leaf Removal! Black Leaf Removal! skew(p!right!right)! p! decrease level ! p! 30 70 30 50 70 Level 3! Level 2! Level 2! 50 60 85 60 85 Level 1! 15 20 35 55 65 80 90 Level 1! 15 20 35 55 65 80 90 skew(p)! skew(p!right)! split(p)! p! p! 30 70 30 50 60 70 Level 2! Level 2! 50 60 85 85 Level 1! 15 20 35 55 65 80 90 Level 1! 15 20 35 55 65 80 90 [Andersson,McCollam]! [Andersson,McCollam]! Black Leaf Removal! AA-Tree p! 50 split(p!right)! Level 3! Implementation! 30 Level 2! 60 70 85 Level 1! 15 20 35 55 65 80 90 p! 50 70 Level 3! 30 Level 2! 60 85 Level 1! 15 20 35 55 65 80 90 [Andersson]! [Andersson,McCollam]! Balanced BST Summary! Randomized Search Trees! AVL Trees: maintain balance factor by rotations! Motivations:! 2-3 Trees: maintain perfect trees with variable node •!when items are inserted in order into a BST," sizes using rotations! worst-case performance becomes O(n)! •!balanced search trees either waste space or requires 2-3-4 Trees: simpler operations than 2-3 trees due complicated (empirically expensive) operations or both! to pre-splitting and pre-merging nodes, •!randomly permuting items to be inserted would ensure good wasteful in memory usage! performance of BST with high probability, but randomly permuting input is not always possible/practical, instead .