### BALANCED SEARCH TREES

2-3 trees. Understand how to search and insert. Understand why insertion technique leads to perfect balance and symmetric order.

Terminology. Symmetric order. Perfect balance.

Performance of 2-3 trees. Tree height is between log_3 N and log_2 N. All paths are of the same height.

1-1 correspondence between LLRBs and 2-3 trees. Understand how to map a 2-3 tree to an LLRB and vice versa.

LLRBs. BST such that no node has two red links touching it; perfect black balance; red links lean left.

LLRB get. Exactly the same as regular BST get.

Storing color. A node's color is the same as the edge connecting that node to its parent.

LLRB insert. Insert as usual, then apply rotations recursively as follows, working upwards from the new node.

1. Case 3: A parent node has a black left child and a red right child, so rotate the parent left. The former right child is now the boss. Reminder: null links are considered black for the purposes of deciding cases.
2. Case 2: A grandparent node has a red left child whose left child is also red. Rotate this grandparent right (so that one in the middle is now the boss).
3. Case 1: A parent node has two red children. Flip colors.
Conveniently, we can always apply case 3, case 2, then case 1 to every node in a tree, and we can guarantee that the entire tree will be an LLRB.

LLRB performance. Perfect black balance ensures worst case performance for get and insert is ~ 2 log_2 N.

### Recommended Problems

#### C level

1. Given an LLRB, is there exactly one corresponding 2-3 tree? Given a 2-3 tree, is the exactly one corresponding LLRB?
2. Draw a 2-3 tree with all 3 nodes. Why is the height log3(N)?
3. How many compares does it take in the worst case to decide whether to take the left, middle, or right link from a 3 node?
4. Spring 2012 Midterm, #5.

5. (a) Circle the keys whose insertion will cause either rotations and/or color flips. A B C E F H K L N P T V X Y Z
(b) Draw and give the level order traversal of the tree that results after inserting the key Z. Write the level order traversal. Clearly indicate red nodes.
(c) In plain English, describe the function of mystery().
```public boolean mystery() {
return mystery(root, null, null);
}
private boolean mystery(Node x, Key a, Key b) {
if (x == null) return true;
if (a != null && x.key.compareTo(a) <= 0) return false;
if (b != null && x.key.compareTo(b) >= 0) return false;
return mystery(x.left, a, x.key) && mystery(x.right, x.key, b);
}```
6. (a) Label each node in the following binary tree with numbers from the set {2,26,10,27,20,15,42} so that it is a legal Binary Search Tree. (Hint: use the back of the page as scratch space, and only write down the answer once you have it.)

(b) Now label each edge in the figure with r or b, denoting RED and BLACK, so that the tree is a legal Left-Leaning Red-Black Tree

#### B level

1. Continuing the previous question.

(c) Considering your labeling in (b), is it possible to assign different red/black labels and still satisfy the LLRB-tree conditions? (Answer yes if a different labeling is possible, or no if your labeling in (b) is unique.)

(d) If the answer to (c) is yes, draw and label the second tree. If the answer is no, how do you know that the red-black labeled tree must be unique?