1 / 36

Red-Black Trees: Balanced Search Trees for Efficient Dynamic Set Operations

Learn about red-black trees, a variation of binary search trees, that guarantee O(log n) time complexity for basic dynamic set operations. Explore their properties and operations, including insertion and deletion. Examples and Java applets provided.

fneumann
Download Presentation

Red-Black Trees: Balanced Search Trees for Efficient Dynamic Set Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Balanced search Trees • 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 Hsiu-Hui Lee

  3. 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. Hsiu-Hui Lee

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

  5. Example of a red-black tree Hsiu-Hui Lee

  6. Example of a red-black tree 1. Every node is either red or black. Hsiu-Hui Lee

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

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

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

  10. 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 Hsiu-Hui Lee

  11. Hsiu-Hui Lee

  12. Leaves and the root’s parent omitted entirely Hsiu-Hui Lee

  13. 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. Hsiu-Hui Lee

  14. Lemma 13.0a The subtree rooted at any node x contains ≥ 2bh(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. 20 − 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 ≥ 2bh(x)-1 − 1 internal nodes. Thus, the subtree rooted at x contains ≥ 2 ·(2bh(x)-1 − 1 )+1 = 2bh(x)−1 internal nodes. (The +1 is for x itself.) Hsiu-Hui Lee

  15. Lemma 13.0b Any node with height h has black –height at leasth/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. Hsiu-Hui Lee

  16. 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 ≥ 2b− 1 ≥ 2h/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). Hsiu-Hui Lee

  17. 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. Hsiu-Hui Lee

  18. LEFT-ROTATE(T, x) y ← right[x] ✄ Set y. right[x] ← left[y] ✄ Turn y’s left subtree into x’s right subtree. if left[y] ≠nil[T ] then p[left[y]] ← x p[y] ← p[x] ✄ Link x’s parent to y. if p[x] = nil[T ] then root[T ]← y else if x = left[p[x]] then left[p[x]] ← y else right[p[x]] ← y left[y] ← x ✄ Put x on y’s left. p[x] ← y The code for RIGHT-ROTATE is symmetric. Both LEFT-ROTATE and RIGHT-ROTATE run in O(1) time. Hsiu-Hui Lee

  19. Example of LEFT-ROTATE(T,x) Rotation makes y’s left subtree into x’s right subtree. Hsiu-Hui Lee

  20. Some Good Java Appletsto simulate Red-Black Tree • http://webpages.ull.es/users/jriera/Docencia/AVL/AVL tree applet.htm • http://gauss.ececs.uc.edu/RedBlack/redblack.html Hsiu-Hui Lee

  21. Insertion and Deletion

  22. RB-INSERT(T, x) • y← nil[T] • x← root[T] • while x≠ nil[T] • do y← x • if key[z] < key[x] • then x← left[x] • else x← right[x] • p[z] ← y • if y = nil[T] • then root[T] ← z • else if key[z] < key[y] • then left[y] ← z • else right[y] ← z • left[z] ← nil[T] • right[z] ← nil[T] • color[z] ← RED • RB-INSERT-FIXUP(T, z) Hsiu-Hui Lee

  23. RB-INSERT-FIXUP(T, z) 1 whilecolor[p[z]] = RED 2 do ifp[z] = left[p[p[z]]] 3 theny← right[p[p[z]]] 4 ifcolor[y] = RED 5 thencolor[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 Hsiu-Hui Lee

  24. 9 elseifz = right[p[z]] 10 thenz← p[p[z]] Case 2 11 LEFT-ROTATE(T,z) Case 2 12 color[p[z]] ← BLACK Case 3 13 color[p[p[z]]] ← RED Case 3 14 RIGHT-ROTATE(T,p[p[z]]) Case 3 15 else (same as then clause with “right” and “left” exchanged) 16 color[root[T]] ← BLACK Hsiu-Hui Lee

  25. The operation of RB-INSERT-FIXUP 紅叔 黑叔右子 Hsiu-Hui Lee

  26. 黑叔左子 Hsiu-Hui Lee

  27. case 1 of the RB-INSERT(z’s uncle y is red) 紅叔右子 紅叔左子 Hsiu-Hui Lee

  28. case 2/3 of the RB-INSERT(z’s uncle y is black andz is a right/left child) 黑叔右子 黑叔左子 Hsiu-Hui Lee

  29. 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. Hsiu-Hui Lee

  30. 13.4 Deletion RB-DELETE(T, z) 1 ifleft[z] = nil[T] or right[z] = nil[T] 2 theny← z 3 elsey← TREE-SUCCESSOR(z) 4 ifleft[y] ≠ nil[T] 5 thenx← left[y] 6 elsex← right[y] 7 p[x] ← p[y] 8 ifp[y] = nil[T] Hsiu-Hui Lee

  31. 9 thenroot[T] ← x 10 else ify = left[p[y]] 11 thenleft[p[y]] ← x 12 elseright[p[y]] ← x 13 ify≠ z 14 thenkey[z] ← key[y] 15 copy y’s satellite data into z 16 ifcolor[y] = BLACK 17 then RB-DELETE-FIXUP(T, x) 18 returny Hsiu-Hui Lee

  32. RB-DELETE-FIXUP(T,x) RB-DELETE-FIXUP (T, x) 1 whilex ≠ root[T] and color[x] = BLACK 2 do ifx = left[p[x]] 3 thenw← right[p[x]] 4 ifcolor[w] = RED 5 thencolor[w] ← BLACKCase1 6 color[p[x]] = REDCase1 7 LEFT-ROTATE(T,p[x]) Case1 8 w← right[p[x]] Case1 Hsiu-Hui Lee

  33. 9 ifcolor[right[w]] = BLACK and color[right[w]= BLACK 10 thencolor[w] ← RED Case2 11 x← p[x] Case2 12 elseif color[right[w]] = BLACK 13 thencolor[left[w]] ← BLACK Case3 14 color[w] ← RED Case3 15 RIGHT-ROTATE(T,w) Case3 16 w← right[p[x]] Case3 • 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 Hsiu-Hui Lee

  34. the case in the while loop of RB-DELETE-FIXUP Hsiu-Hui Lee

  35. Hsiu-Hui Lee

  36. Analysis • The RB-DELETE-FIXUP takes O(lg n) time and performs at most three rotations. • The overall time for RB-DELETE is therefore also O(lg n) Hsiu-Hui Lee

More Related