Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the.

Similar presentations


Presentation on theme: "Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the."— Presentation transcript:

1 Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.

2 20071130 chap13Hsiu-Hui Lee2 Many search-tree schemes that are “ balanced ” in order to guarantee that basic dynamic-set operations take O(lg n ) time in the worse case. e.g. AVL trees Red-black trees Balanced search Trees

3 20071130 chap13Hsiu-Hui Lee3 Red-Black Tree a variation of binary search trees with one extra bit of storage per node: its color, which can either RED or BLACK. Each node of the tree now contains the fields color, key, left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer field of the node contains the value NIL. We shall regard these NIL ’ s as being pointers to external nodes(leaves) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree.

4 20071130 chap13Hsiu-Hui Lee4 Red-Black Properties 1.Every node is either red or black. 2.The root is black. 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black. 5.For each node, all paths from the node x to descendant leaves contain the same number of black nodes (i.e. black-height(x)).

5 20071130 chap13Hsiu-Hui Lee5 Example of a red-black tree

6 20071130 chap13Hsiu-Hui Lee6 Example of a red-black tree 1. Every node is either red or black.

7 20071130 chap13Hsiu-Hui Lee7 Example of a red-black tree 2. 3. The root and leaves (NIL’s) are black.

8 20071130 chap13Hsiu-Hui Lee8 4. If a node is red, then its children are black. ?? Means ( If a node is red, then its parent is black. ) Example of a red-black tree

9 20071130 chap13Hsiu-Hui Lee9 5. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). Example of a red-black tree

10 20071130 chap13Hsiu-Hui Lee10 black-height of the node: bh(x) # of black nodes on any path from, but not including, a node x down to a leaf black-height of a RB tree = black-height of its root

11 20071130 chap13Hsiu-Hui Lee11

12 20071130 chap13Hsiu-Hui Lee12 Leaves and the root’s parent omitted entirely

13 20071130 chap13Hsiu-Hui Lee13 Height and black-height Height of a node x: h(x) is the number of edges in a longest path to a leaf. Black-height of a node x: bh(x) is the number of black nodes (including nil[T ]) on the path from x to leaf, not counting x.

14 20071130 chap13Hsiu-Hui Lee14 Lemma 13.0a The subtree rooted at any node x contains ≥ 2 bh(x) − 1 internal nodes. Proof By induction on height of x. Basis: Height of x = 0 ⇒ x is a leaf ⇒ bh(x) = 0. The subtree rooted at x has 0 internal nodes. 2 0 − 1 = 0. Inductive step: Let the height of x be h and bh(x) = b. Any child of x has height h − 1 and black-height either b (if the child is red) or b − 1 (if the child is black). By the inductive hypothesis, each child has ≥ 2 bh(x)-1 − 1 internal nodes. Thus, the subtree rooted at x contains ≥ 2 · (2 bh(x)-1 − 1 )+1 = 2 bh(x) −1 internal nodes. (The +1 is for x itself.)

15 20071130 chap13Hsiu-Hui Lee15 Lemma 13.0b Any node with height h has black – height at least h/2. Proof By property 4 (If a node is red, then both its children are black), ≤ h/2 nodes on the path from the node to a leaf are red. Hence ≥ h/2 are black.

16 20071130 chap13Hsiu-Hui Lee16 Lemma 13.1 A red-black tree with n internal nodes has height at most 2lg(n+1). Proof Let h and b be the height and black-height of the root, respectively. By the above two claims, n ≥ 2 b − 1 ≥ 2 h/2 − 1. Adding 1 to both sides and then taking logs gives lg(n + 1) ≥ h/2, which implies that h ≤ 2 lg(n + 1).

17 20071130 chap13Hsiu-Hui Lee17 13.2 Rotations Left-Rotation makes y ’ s left subtree into x ’ s right subtree. Right-Rotation makes x ’ s right subtree into y ’ s left subtree.

18 20071130 chap13Hsiu-Hui Lee18 L EFT -R OTATE (T, x) 1.y ← right[x] ✄ Set y. 2.right[x] ← left[y] ✄ Turn y ’ s left subtree into x ’ s right subtree. 3.if left[y] ≠ nil[T ] 4. then p[left[y]] ← x 5.p[y] ← p[x] ✄ Link x ’ s parent to y. 6.if p[x] = nil[T ] 7. then root[T ]← y 8. else if x = left[p[x]] 9. then left[p[x]] ← y 10. else right[p[x]] ← y 11.left[y] ← x ✄ Put x on y ’ s left. 12.p[x] ← y The code for R IGHT -R OTATE is symmetric. Both L EFT -R OTATE and R IGHT -R OTATE run in O(1) time.

19 20071130 chap13Hsiu-Hui Lee19 Example of L EFT -R OTATE (T,x) Rotation makes y ’ s left subtree into x ’ s right subtree.

20 20071130 chap13Hsiu-Hui Lee20 Some Good Java Applets to simulate Red-Black Tree http://webpages.ull.es/users/jriera/Docencia/AVL /AVL tree applet.htmhttp://webpages.ull.es/users/jriera/Docencia/AVL /AVL tree applet.htm http://gauss.ececs.uc.edu/RedBlack/redblack.html

21 Insertion and Deletion

22 20071130 chap13Hsiu-Hui Lee22 RB-I NSERT ( T, x ) 1. y ← nil[T] 2.x ← root[T] 3.while x ≠ nil[T] 4. do y ← x 5. if key[z] < key[x] 6. then x ← left[x] 7. else x ← right[x] 8.p[z] ← y 9.if y = nil[T] 10. then root[T] ← z 11. else if key[z] < key[y] 12. then left[y] ← z 13. else right[y] ← z 14.left[z] ← nil[T] 15.right[z] ← nil[T] 16.color[z] ← RED 17.RB-I NSERT -F IXUP (T, z)

23 20071130 chap13Hsiu-Hui Lee23 RB-I NSERT- F IXUP ( T, z ) 1 while color[p[z]] = RED 2 do if p[z] = left[p[p[z]]] 3 then y ← right[p[p[z]]] 4if color[y] = RED 5 then color[p[z]] ← BLACK Case 1 6 color[y] ← BLACK Case 1 7 color[p[p[z]]] ← RED Case 1 8 z ← p[p[z]] Case 1

24 20071130 chap13Hsiu-Hui Lee24 9 else if z = right[p[z]] 10then z ← p[p[z]] Case 2 11 L EFT -R OTATE (T,z) Case 2 12 color[p[z]] ← BLACK Case 3 13color[p[p[z]]] ← RED Case 3 14R IGHT -R OTATE (T,p[p[z]]) Case 3 15else (same as then clause with “ right ” and “ left ” exchanged) 16 color[root[T]] ← BLACK

25 20071130 chap13Hsiu-Hui Lee25 The operation of RB-I NSERT -F IXUP 紅叔 黑叔右子

26 20071130 chap13Hsiu-Hui Lee26 黑叔左子

27 20071130 chap13Hsiu-Hui Lee27 case 1 of the RB-I NSERT (z’s uncle y is red) 紅叔右子 紅叔左子

28 20071130 chap13Hsiu-Hui Lee28 case 2/3 of the RB-INSERT (z’s uncle y is black and z is a right/left child) 黑叔右子黑叔左子

29 20071130 chap13Hsiu-Hui Lee29 Analysis RB-INSERT take a total of O(lg n) time. It never performs more than two rotations, since the while loop terminates if case 2 or case 3 executed.

30 20071130 chap13Hsiu-Hui Lee30 13.4 Deletion RB-D ELETE (T, z) 1 if left[z] = nil[T] or right[z] = nil[T] 2 then y ← z 3 else y ← T REE -S UCCESSOR (z) 4 if left[y] ≠ nil[T] 5 then x ← left[y] 6 else x ← right[y] 7 p[x] ← p[y] 8 if p[y] = nil[T]

31 20071130 chap13Hsiu-Hui Lee31 9 then root[T] ← x 10 else if y = left[p[y]] 11then left[p[y]] ← x 12else right[p[y]] ← x 13 if y ≠ z 14 then key[z] ← key[y] 15 copy y ’ s satellite data into z 16 if color[y] = BLACK 17then RB-D ELETE -F IXUP (T, x) 18 return y

32 20071130 chap13Hsiu-Hui Lee32 RB-D ELETE -F IXUP (T,x) 1while x ≠ root[T] and color[x] = BLACK 2 do if x = left[p[x]] 3then w ← right[p[x]] 4 if color[w] = RED 5then color[w] ← BLACK Case1 6 color[p[x]] = RED Case1 7 LEFT-ROTATE(T,p[x]) Case1 8 w ← right[p[x]] Case1

33 20071130 chap13Hsiu-Hui Lee33 9if color[right[w]] = BLACK and color[right[w]= BLACK 10 then color[w] ← RED Case2 11 x ← p[x] Case2 12 else if color[right[w]] = BLACK 13 then color[left[w]] ← BLACK Case3 14 color[w] ← RED Case3 15 RIGHT-ROTATE(T,w) Case3 16w ← right[p[x]] Case3 17 color[w] ← color[p[x]] Case4 18 color[p[x]] ← BLACK Case4 19 color[right[w]] ← BLACK Case4 20 LEFT-ROTATE(T,p[x]) Case4 21 x ← root[T] Case4 22 else (same as then clause with “ right ” and “ left ” exchanged) 23 color[x] ← BLACK

34 20071130 chap13Hsiu-Hui Lee34 the case in the while loop of RB-D ELETE -F IXUP

35 20071130 chap13Hsiu-Hui Lee35

36 20071130 chap13Hsiu-Hui Lee36 Analysis The RB-D ELETE -F IXUP takes O(lg n) time and performs at most three rotations. The overall time for RB-DELETE is therefore also O(lg n)


Download ppt "Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the."

Similar presentations


Ads by Google