CS200: Algorithms Analysis
RED BLACK TREES Red black trees maintain a balanced tree structure so that height of tree = θ(log n). Idea is to add a color(r, b) to a BST. RED-BLACK PROPERTIES 1. every node is r or b. 2. every leaf (nil) is black. 3. if a node is red then both its children are black => there can’t be two successive red nodes in a tree. 4. every simple path from a node to a leaf contains the same # of b nodes. 5. root is black (simplifying assumption for inserts).
Black-Height of a node : bh(x) = # of b nodes on path to leaf not counting x. Show an example tree, label with r or b, and h
Example RB Tree Label with bh (4. every simple path from a node to a leaf contains the same # of b nodes and bh(x) = # of b nodes on path to leaf not counting x.) A height-h node has bh >= h/2. Why?
Nodes
Inductive Proof of Lemma 13.1 (Go over on own) Prove: a red-black tree with n internal nodes has h <=2log(n+1). Proof is based on claim that a sub-tree rooted at node x contains >= 2bh(x)-1 internal nodes. Proof is by induction on height h of node x. basis: h=0 => x is nil; black leaf => 2bh(x)-1 = 20-1 = 0 inductive step: x is an internal node with 2 children.
A sub-tree rooted at x has at least 2bh(x)-1 internal nodes. bh of children of x have at least bh(x) -1 (if child is black). subtree has at least (2bh(x)-1 -1) + (2bh(x)-1 -1) +1 internal nodes. (2bh(x)-1 -1) + (2bh(x)-1 -1) +1 = 2(2bh(x)-1 -1) +1= 2bh(x)-1 internal nodes. Let h be the height of tree then n >=2bh(root) -1 >= 2h/2 -1 (using property 3 of red-black trees). n >= 2h/2-1 n+1>= 2h/2 log(n+1)>=h/2 2log(n+1)>=h
Query Operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(lg n) time on a red-black tree with n nodes.
Modifying Operations The operations INSERT and DELETE cause modifications to the red-black tree: • the operation itself, • color changes, • restructuring the links of the tree via “rotations”.
Rotations
Insertions
Insertions
Insertions
Insertions
Insertions
Pseudo Code
Graphical Notation
Case 1
Case 2
Case 3
Analysis : Given line 1., what property of RB trees can be violated by above code?
Summary RB Tree properties Balancing algorithms (rotations) analysis