170 likes | 290 Views
CS473-Algorithms. Lecture RED-BLACK TREES (RBT). Overview. Previous lecture showed that a BST of height h can implement any of the dynamic set operations in O(h) time. Thus, set operations are fast if the height of BST is small.
E N D
CS473-Algorithms Lecture RED-BLACK TREES (RBT) Lecture X
Overview • Previous lecture showed that a BST of height h can implement any of the dynamic set operations in O(h) time. • Thus, set operations are fast if the height of BST is small. • But, if h is large, its performance is no better than linked list. Lecture X
Overview • Red-Black trees are one of many search tree schemes that are balanced to guarantee h= O(lgn) • First, we will show that h= O(lgn) in RBTs • Then, show how to manipulate & maintain RBTs during dynamic set operations. Lecture X
Properties of Red-Black Trees • Add color field (1 bit) to each node of a BST. • Consider “NIL” child fields as pointer to external nodes (leafs) i.e. Leaf nodes do not have keys • Consider normal key-bearing nodes as internal nodes. Hence, each internal node has exactly 2 children. • Thus, RBTs are full binart trees (FBTs) • FBT: Each node is either a leaf or has degree exactly 2. i.e. There are no degree-1 nodes. Lecture X
Red-Black Properties • Every node is either red or black • Every leaf (NIL) is black • If a node is red, then both of its children are black. i.e. There can’t be 2 consecutive reds on a simplepath. • Every simpe path from a node to a descendant leaf contains the same number of black nodes. • ( Additional propery) The root is black. • Simplifies explanation of insertion which assumes and guarantees that the root is black. Lecture X
A Sample Red Black Tree bh = 2 h = 4 bh = 1 bh = 2 h = 1 h = 3 bh = 2 bh = 1 h = 4 h = 2 bh = 1 h = 1 bh = 0 h = 0 Lecture X
Properties of Red-Black Trees Black-Height : bh (x) • # of blacks on the simple paths to all descendant leaves not counting x. • By the property 4, the black-height is well defined. Red-Black properties ensure that No simpe path from a node to a descendant leaf is more that twice as long as any other. So, red-black trees are apptoximately balanced. Lecture X
Properties of Red-Black Trees • Lemma 1 : A height h node has black height ≥ h/2 • Height is length of the longest path to a leaf. • By property 3, # of reds on this path ≤ h/2 • Hence, # of blacks ≥ h/2 • Lemma 2 : The subtree Tx rooted at any node x contains at least |Tx| ≥ 2bh(x) – 1 internal nodes. Lecture X
Properties of Red-Black Trees Basis:h(x)=0 x must be a leaf (NIL) bh(x)=0 Tx contains 0 internal nodes = 2bh(x) -1=2-1= 0 Inductive Step: h(x) > 0 x is internal node with 2 children l[x] : left[x] r[x] : right[x] x h(x) bh(x) l[x] r[x] Tl[x] Tr[x] Lecture X
Properties of Red-Black Trees Depending on l[x] and r[x] being red or black • bh(l[x]) = bh(x) or bh(x) -1, respectively, • bh(r[x]) = bh(x) or bh(x) -1, respectively. Since h(l[x]), h(r[x]) < h(x) we can apply inductive hypothesis. |Tx| = |Tl| + |Tr| +1 ≥ (2bh(x)-1 – 1) + (2bh(x)-1 – 1) + 1 = 2bh(x)-1 – 1 Q.E.D. Lecture X
Properties of Red-Black Trees Theorem : A red-black tree with n internal nodes has height at most 2lg(n+1) Due to lemma 2 |Troot| = n ≥ 2bh(root) – 1 Due to lemma 1 bh(root) ≥ h(root) / 2 = H / 2 n ≥ 2H/2 -1 (n+1) ≥ 2H/2 H ≤ 2 lg(n+1) Lecture X
Properties of Red-Black Trees Corollary: Dynamic set operations MIN, MAX, SEARCH, SUCCESSOR, PREDECESSOR take O(lgn) time. Fact: Let lmin(x) & lmax(x) denote the lengths of the shortest & longest paths from a node x to its leafs, respectively. Then, lmax(x) ≤ lmin(x) for any node x. Proof:lmax(x) = h(x) ≤ 2bh(x) ≤ lmin(x) Lemma 1 Lecture X
The Problem of Insertion into Red-Black Trees Draw a R-B tree Color choices starting at 12 Forced choices : 12 → R ; 5 and 9 → B 7 5 9 12 Lecture X
The Problem of Insertion into Red-Black Trees Insert 8 (Left child of 9) No problem, Just color it red 7 5 9 8 ? → R 12 Lecture X
The Problem of Insertion into Red-Black Trees Insert 11 (Left child of 12) 11 can’t be red (Violate property 3) 11 can’t be black (Violate property 4) 7 5 9 8 12 ? 11 Lecture X
The Problem of Insertion into Red-Black Trees Can fix up by recoloring the tree New 11 to Red Change 9 to Red Change 8 and 12 to Black 7 5 9 B → R 8 R → B R → B 12 ?→R 11 Lecture X
The Problem of Insertion into Red-Black Trees Insert 10 (Left child of 11) Recoloring is not enough, why? -Because of tree imbalance -Can’t satisfy property 4 (Equal # of blacks on paths) without violating property 3 (No consecutive reds on paths) Must change tree structure -Goal : Restructure in O(lgn) time. 7 5 9 8 12 11 ? 10 Lecture X