1 / 35

CSC 201: Design and Analysis of Algorithms Lecture # 11

CSC 201: Design and Analysis of Algorithms Lecture # 11. Red-Black Trees. Red-Black Trees. Red-black trees : Binary search trees augmented with node color Balanced: height is O(lg n ), where n is the number of nodes. Operations will take O(lg n ) time in the worst case.

roman
Télécharger la présentation

CSC 201: Design and Analysis of Algorithms Lecture # 11

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. CSC 201: Design and Analysis of AlgorithmsLecture # 11 Red-Black Trees Mudasser Naseer 110/4/2014

  2. Red-Black Trees • Red-black trees: • Binary search trees augmented with node color • Balanced: height is O(lg n), where n is the number of nodes. • Operations will take O(lg n) time in the worst case. • First: describe the properties of red-black trees • Then: prove that these guarantee h = O(lg n) • Finally: describe operations on red-black trees Mudasser Naseer 210/4/2014

  3. Red-Black Properties • The red-black properties: 1. Every node is either red or black 2. The root is always black. 3. Every leaf (NULL pointer (nil[T])) is black • Note: this means every “real” node has 2 children 4. If a node is red, both children are black • Note: can’t have 2 consecutive reds on a path 5. Every path from node to descendent leaf contains the same number of black nodes Mudasser Naseer 310/4/2014

  4. Mudasser Naseer 410/4/2014

  5. Red-Black Trees • Put example on board and verify properties: 1. Every node is either red or black 2. The root is always black. 3. Every leaf (NULL pointer) is black 4. If a node is red, both children are black 5. Every path from node to descendent leaf contains the same number of black nodes • black-height bh(x): # black nodes on path from x to leaf (including nil[T] but not counting x) • Label example with h and bh values Mudasser Naseer 510/4/2014

  6. Mudasser Naseer 610/4/2014

  7. Height of Red-Black Trees • What is the minimum black-height of a node with height h? • A: a height-h node has black-height  h/2 • Proof: By property 4, ≤ h/2 nodes on the path from the node to a leaf are red.Hence ≥ h/2 are black. • Theorem: A red-black tree with n internal nodes has height h  2 lg(n + 1) • How do you suppose we’ll prove this? • {An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf node.} Mudasser Naseer 710/4/2014

  8. RB Trees: Proving Height Bound • Prove: n-node RB tree has height h  2 lg(n+1) • Claim: A subtree rooted at a node x contains at least 2bh(x) - 1 internal nodes • Proof by induction on height h • Base step: x has height 0 (i.e., NULL leaf node) • What is bh(x)? Mudasser Naseer 810/4/2014

  9. RB Trees: Proving Height Bound • Prove: n-node RB tree has height h  2 lg(n+1) • Claim: A subtree rooted at a node x contains at least 2bh(x) - 1 internal nodes • Proof by induction on height h • Base step: x has height 0 (i.e., NULL leaf node) • What is bh(x)? • A: 0 • So…subtree contains 2bh(x) - 1 = 20 - 1 = 0 internal nodes (TRUE) Mudasser Naseer 910/4/2014

  10. RB Trees: Proving Height Bound • Inductive proof that subtree at node x contains at least 2bh(x) - 1 internal nodes • Inductive step: x has positive height and 2 children • Each child has black-height of bh(x) or bh(x)-1 (Why?) • The height of a child = (height of x)- 1 • So the subtrees rooted at each child contain at least 2bh(x) - 1 - 1 internal nodes • Thus subtree at x contains (2bh(x) - 1 - 1) + (2bh(x) - 1 - 1) + 1= 2•2bh(x)-1 - 1 = 2bh(x) - 1 nodes Mudasser Naseer 1010/4/2014

  11. RB Trees: Proving Height Bound • Thus at the root of the red-black tree: n 2bh(root) - 1 (Why?) n  2h/2 - 1 (Why?) lg(n+1)  h/2 (Why?) h  2 lg(n + 1) (Why?) Thus h = O(lg n) Mudasser Naseer 1110/4/2014

  12. RB Trees: Worst-Case Time • So we’ve proved that a red-black tree has O(lg n) height • Corollary: These operations take O(lg n) time: • Minimum(), Maximum() • Successor(), Predecessor() • Search() • Insert() and Delete(): • Will also take O(lg n) time • But will need special care since they modify tree Mudasser Naseer 1210/4/2014

  13. 7 7 5 9 5 9 12 12 Red-Black Trees: An Example • Color this tree: Red-black properties: 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1310/4/2014

  14. 7 5 9 12 Red-Black Trees: The Problem With Insertion • Insert 8 • Where does it go? 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1410/4/2014

  15. Red-Black Trees: The Problem With Insertion • Insert 8 • Where does it go? • What color should it be? 7 5 9 8 12 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1510/4/2014

  16. Red-Black Trees: The Problem With Insertion • Insert 8 • Where does it go? • What color should it be? 7 5 9 8 12 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1610/4/2014

  17. Red-Black Trees:The Problem With Insertion • Insert 11 • Where does it go? 7 5 9 8 12 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1710/4/2014

  18. Red-Black Trees:The Problem With Insertion • Insert 11 • Where does it go? • What color? 7 5 9 8 12 11 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1810/4/2014

  19. Red-Black Trees:The Problem With Insertion • Insert 11 • Where does it go? • What color? • Can’t be red! (#3) 7 5 9 8 12 11 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 1910/4/2014

  20. Red-Black Trees:The Problem With Insertion • Insert 11 • Where does it go? • What color? • Can’t be red! (#3) • Can’t be black! (#4) 7 5 9 8 12 11 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 2010/4/2014

  21. Red-Black Trees:The Problem With Insertion • Insert 11 • Where does it go? • What color? • Solution: recolor the tree 7 5 9 8 12 11 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 2110/4/2014

  22. Red-Black Trees:The Problem With Insertion • Insert 10 • Where does it go? 7 5 9 8 12 11 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black Mudasser Naseer 2210/4/2014

  23. Red-Black Trees:The Problem With Insertion • Insert 10 • Where does it go? • What color? 7 5 9 8 12 11 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black 10 Mudasser Naseer 2310/4/2014

  24. Red-Black Trees:The Problem With Insertion • Insert 10 • Where does it go? • What color? • A: no color! Tree is too imbalanced • Must change tree structureto allow recoloring • Goal: restructure tree in O(lg n) time 7 5 9 8 12 11 10 Mudasser Naseer 2410/4/2014

  25. RB Trees: Rotation • Our basic operation for changing tree structure is called rotation: • Does rotation preserve inorder key ordering? • What would the code for rightRotate() actually do? y x rightRotate(y) x C A y leftRotate(x) A B B C Mudasser Naseer 2510/4/2014

  26. RB Trees: Rotation • Answer: A lot of pointer manipulation • x keeps its left child • y keeps its right child • x’s right child becomes y’s left child • x’s and y’s parents change • What is the running time? y x rightRotate(y) x C A y A B B C Mudasser Naseer 2610/4/2014

  27. Rotation Example • Rotate left about 9: 7 5 9 8 12 11 Mudasser Naseer 2710/4/2014

  28. Rotation Example • Rotate left about 9: 7 5 12 9 8 11 Mudasser Naseer 2810/4/2014

  29. Rotation Example • After Colouring 7 5 12 9 8 11 Mudasser Naseer 2910/4/2014

  30. Rotation LEFT-ROTATE(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 thenroot[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 Mudasser Naseer 3010/4/2014

  31. Rotation - Example Mudasser Naseer 3110/4/2014

  32. The longest possible path from the root to a leaf is no more than twice as long as the shortest possible path. The result is that the tree is roughly balanced. The shortest possible path has all black nodes, and the longest possible path alternates between red and black nodes. All maximal paths have the same number of black nodes, by property 5, this shows that no path is more than twice as long as any otherpath. Mudasser Naseer 3210/4/2014

  33. Time Taken Read-only operations: binary search trees. Insertion or removal may violate the properties of a red-black tree. Restoring the red-black properties requires a small number (O(lg n)) of color changes and no more than three tree rotations (two forinsertion). Although insert and deleteoperations are complicated, their times remainO(lg n). Mudasser Naseer 3310/4/2014

  34. Advantages Red-black trees offer the best possible worst-case insertion time, deletion time, and search time. Mudasser Naseer 3410/4/2014

  35. Applications Time-sensitive applications such as real-time applications. Data structures used in computational geometry. Valuable in functional programming where they are one of the most common persistent data structures, used to construct associative arrays. Mudasser Naseer 3510/4/2014

More Related