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" ! (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- Ordering Properties! An AA-Tree Example!

An AA-Tree is a binary 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 ! 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 . . .! Red-black Trees: binary representation of 2-3-4 trees, no wasted node space but Randomized search trees balance the trees complicated rules and lots of cases! probabilistically instead of maintaining balance deterministically! AA-Trees: simpler operations than red-black trees, binary representation of 2-3 trees! Treaps! Example of a !

A treap is a that:! assuming min- •!has a key associated with each of its internal node:! ordering of the priorities:! •! the key in any node is > the keys in all nodes in its left subtree T: and < the keys in all nodes in its right subtree! •! i.e., internal nodes are arranged in in-order" k/3! with respect to their keys! •!and simultaneously has a priority associated" d/6! m/7! with each of its internal node:! •! the priority of a parent is higher than those of its descendants! •! i.e., internal nodes are arranged in heap-order" a/12! f/9! l/15! n/8! with respect to their priorities!

A treap is a BST with heap-ordered priorities (but it is not z/11! a heap as it is not required to be a complete binary tree)!

Treaps: Insert! m/7! Example Treap with p/5 Inserted! 1.! a new item to be inserted into a treap" l/15! n/8! is given a random, unique priority (no assuming min-heap z/11! duplicates)! ordering of the priorities:! T: 2.! the new item is then inserted into a m/7! p/5! treap as a leaf node, just like it would" k/3! be under a standard BST! l/15! n/8!

3.! if its priority violates the heap-order p/5! d/6! p/5! property of the treap, the new node is rotated up until it is in the correct z/11! heap-order priority, using one or" a/12! f/9! m/7! z/11!

more single left- or right-rotation! m/7! p/5!

Example: insert (p/5) into the l/15! p/5! m/7! z/11! l/15! n/8!

example treap! n/8! z/11! l/15! n/8! Treaps: Delete! m/7! Treaps: Search!

Exact reverse of insert:! l/15! n/8! Standard BST search! 1.! Rotate the node to be deleted such z/11! that its child with larger priority If it is desirable to keep frequently accessed items becomes the new parent! m/7! p/5! near the root, e.g., when the treap is used to 2.! continue rotating until the node to l/15! n/8! maintain a cache, whenever an item is accessed, be deleted is a leaf node! p/5! assign the item a new random number that gives it 3.! delete the leaf node! a higher priority and, if necessary, rotate its node z/11! up to maintain heap-order! Example: delete (p/5) from the example treap! m/7! p/5! If it is desirable for the treap of a of keys to be l/15! p/5! m/7! z/11! unique, use one-way hash function on keys to generate priorities! n/8! z/11! l/15! n/8!

Runtime Complexity! Treaps Running Time! The expected depth of any node is O(log n) ! the Various metrics to measure the expected running time of search, insert, delete (and complexity of an algorithm:! tree split and join) are all O(log n)! •!asymptotic worst-case bound! •!average-case bound! The expected number of rotations per insertion or •!amortized bound! deletion is less than 2 ⇒ fast implementation! •!probabilistic expected-case bound! Proof: relies on probabilistic analysis that is beyond the scope of this course . . .! Calls to random number generator usually incur non-trivial cost! Treap Exercise!

Insert F, E, D, C, B, A with random priorities! •!assuming min-heap ordering of the priorities!