Red-Black Trees Study Guide
Tree rotations. We rotateLeft
or rotateRight
on a node, creating a different but valid BST with the same elements. Notice when we rotateLeft(G)
, the node G
becomes the left child of the new root.
Left-Leaning Red Black Tree. Implements a 2-3 Tree as a BST with rotations and recoloring. Be able to convert between a 2-3 Tree and a LLRB tree. We use red links to indicate two nodes that would be in the same 2-3 Node. In a left-leaning red-black tree, we arbitrarily enforce that the tree is left-leaning.
- Perfect black balance. Every root-to-leaf path has the same number of black links.
- Left-leaning. Red links lean left.
- Color invariant. No node has two red links connected to it, either above/below or left/right.
1-1 correspondence between LLRBs and 2-3 trees. Understand how to map a 2-3 tree to an LLRB and vice versa.
LLRB Operations. Always insert with a red link at the correct location. Then use the following three operations to fix the LLRB tree.
- If there is a right leaning red link, rotate that node left.
- If there are two consecutive left leaning links, rotate right on the top node.
- If there is a node with two red links to children, flip all links with that node.
Recommended Problems
- Given an LLRB, is there exactly one corresponding 2-3 tree? Given a 2-3 tree, is the exactly one corresponding LLRB?
- Draw a 2-3 tree with all 3 nodes. Why is the height log3(N)?
- 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?
- For a 2-3 tree of height H, what is the best case number of compares to find a key? The worst case?
Show that in the worst case, we need to perform order log N LLRB operations (rotation and color flipping) after an insert. Give an example of a tree which requires log N LLRB operations after a single insert.
- Q4b from CS 61B 15sp MT2
- Q5 from CS 61BL 16su MT2 (LLRB Tree only)
- Q1b, Q1d from CS 61B 18sp MT2
- Q3a from CS 61BL 18su MT3
- Q4 from CS 61B 19sp MT2
- Q1 from CS 61BL 19su MT2