Lecture 5 - Red-Black Trees Flashcards
What are red-black trees?
balanced binary search trees with tree height O(logn) where each node has a bit indicating red or black.
Whats the runtime of most operations in RBT?
O(logn)
What are the 5 properties of a RBT?
- Every node is either red or black
- The root is black
- All leaves (nil) are black.
- If a node is red, then its children are black (no 2 consecutive red nodes)
- For each node, all paths from node to descendant leaves contain the same number of black nodes (same black height)
What is the Black-height of a node x?
The number of black nodes on the path from root to leaf, without counting x.
What’s the relation between Black height and height.
bh <= h <= bh
Prove that any node x with height h(x) has a black-height bh(x) ≥ h(x)/2.
Proof: By RB property 4, ≤ h / 2 nodes on the path from
the node to a leaf are red. Hence ≥ h/2 are black.
Prove: The subtree rooted at any node x contains ≥ 2^(bh(x)) – 1 internal nodes.
Proof: By induction on height of x.
• Base Case: Height h(x) = 0 -> x is a leaf -> bh(x) = 0.
Subtree has ≥ 2^0–1 = 0 nodes.
• Induction Step:
– Each child of x has height h(x) - 1 and black-height either bh(x) (child is red) or bh(x) - 1 (child is black).
– By ind. hyp., each child has ≥ 2^(bh(x)– 1) – 1 internal nodes.
– Subtree rooted at x has ≥ 2 * (2^(bh(x)– 1)– 1) + 1
= 2bh(x) – 1 internal nodes.
Prove: A red-black tree with n internal nodes has
height at most 2 lg(n+1).
- By lemma 2, n ≥ 2^bh – 1,
- By lemma 1, bh ≥ h/2, thus n ≥ 2^h/2 – 1.
- -> h ≤ 2 lg(n + 1).
State the three lemmas of Red-Black Trees.
- Any node x with height h(x) has a black-height bh(x) ≥ h(x)/2.
- The subtree rooted at any node x contains ≥ 2^(bh(x)) – 1 internal nodes.
- A red-black tree with n internal nodes has
height at most 2 lg(n+1).
What’s the run-time of insert in a RBT?
O(logn)
What’s the maximum amount of rotation needed for each insert in a RBT?
2
What are the differences between AVL and RBT?
- AVL trees are more strictly balanced ⇒ faster search
- Red Black Trees have less constraints and insert/remove operations require less rotations ⇒ faster insertion and removal
- AVL trees store balance factors or heights with each node
- Red Black Tree requires only 1 bit of information per node