Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)

Similar presentations


Presentation on theme: "CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)"— Presentation transcript:

1 CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)

2 CS 473Lecture X2 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.

3 CS 473Lecture X3 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.

4 CS 473Lecture X4 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.

5 CS 473Lecture X5 Red-Black Properties 1.Every node is either red or black 2.Every leaf (NIL) is black 3.If a node is red, then both of its children are black. i.e. There can’t be 2 consecutive reds on a simple path. 4.Every simpe path from a node to a descendant leaf contains the same number of black nodes. 5.( Additional propery) The root is black. 6.Simplifies explanation of insertion which assumes and guarantees that the root is black.

6 CS 473Lecture X6 A Sample Red Black Tree bh = 2 h = 4 bh = 2 h = 3 bh = 1 h = 2 bh = 1 h = 1 bh = 0 h = 0 bh = 1 h = 1 bh = 2 h = 4

7 CS 473Lecture X7 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.

8 CS 473Lecture X8 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 T x rooted at any node x contains at least |T x | ≥ 2 bh(x) – 1 internal nodes.

9 CS 473Lecture X9 Properties of Red-Black Trees Basis: h(x)=0 x must be a leaf (NIL) bh(x)=0 T x contains 0 internal nodes = 2 bh(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] h(x) bh(x) r[x] l [x] T r[x] T l[x] x

10 CS 473Lecture X10 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. |T x | = |T l | + |T r | +1 ≥ (2 bh(x)-1 – 1) + (2 bh(x)-1 – 1) + 1 = 2 bh(x)-1 – 1 Q.E.D. Properties of Red-Black Trees

11 CS 473Lecture X11 Theorem : A red-black tree with n internal nodes has height at most 2lg(n+1) Due to lemma 2 |T root | = n ≥ 2 bh(root) – 1 Due to lemma 1 bh(root) ≥ h(root) / 2 = H / 2 n ≥ 2 H/2 -1 (n+1) ≥ 2 H/2 H ≤ 2 lg(n+1) Properties of Red-Black Trees

12 CS 473Lecture X12 Corollary: Dynamic set operations MIN, MAX, SEARCH, SUCCESSOR, PREDECESSOR take O(lgn) time. Fact: Let l min (x) & l max (x) denote the lengths of the shortest & longest paths from a node x to its leafs, respectively. Then, l max (x) ≤ l min (x) for any node x. Proof: l max (x) = h(x) ≤ 2bh(x) ≤ l min (x) Properties of Red-Black Trees Lemma 1

13 CS 473Lecture X13 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 12 9

14 CS 473Lecture X14 The Problem of Insertion into Red-Black Trees Insert 8 (Left child of 9) No problem, Just color it red 7 5 12 9 8 ? → R

15 CS 473Lecture X15 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 12 9 8 11 ?

16 CS 473Lecture X16 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 12 9 8 R → B B → R R → B 11 ?→R

17 CS 473Lecture X17 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 12 9 8 11 10 ?


Download ppt "CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)"

Similar presentations


Ads by Google