Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.

Similar presentations


Presentation on theme: "Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees."— Presentation transcript:

1 Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

2 Mudasser Naseer 2 10/20/2015 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

3 Mudasser Naseer 3 10/20/2015 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

4 Mudasser Naseer 4 10/20/2015

5 Mudasser Naseer 5 10/20/2015 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

6 Mudasser Naseer 6 10/20/2015

7 Mudasser Naseer 7 10/20/2015 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.}

8 Mudasser Naseer 8 10/20/2015 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 2 bh(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)?

9 Mudasser Naseer 9 10/20/2015 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 2 bh(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 2 bh(x) - 1 = 2 0 - 1 = 0 internal nodes (TRUE)

10 Mudasser Naseer 10 10/20/2015 RB Trees: Proving Height Bound ● Inductive proof that subtree at node x contains at least 2 bh(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 2 bh(x) - 1 - 1 internal nodes ○ Thus subtree at x contains (2 bh(x) - 1 - 1) + (2 bh(x) - 1 - 1) + 1 = 22 bh(x)-1 - 1 = 2 bh(x) - 1 nodes

11 Mudasser Naseer 11 10/20/2015 RB Trees: Proving Height Bound ● Thus at the root of the red-black tree: n  2 bh(root) - 1(Why?) n  2 h/2 - 1(Why?) lg(n+1)  h/2(Why?) h  2 lg(n + 1)(Why?) Thus h = O(lg n)

12 Mudasser Naseer 12 10/20/2015 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

13 Mudasser Naseer 13 10/20/2015 Red-Black Trees: An Example ● Color this tree: 7 59 12 59 7 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

14 Mudasser Naseer 14 10/20/2015 ● Insert 8 ■ Where does it go? Red-Black Trees: The Problem With Insertion 12 59 7 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

15 Mudasser Naseer 15 10/20/2015 ● Insert 8 ■ Where does it go? ■ What color should it be? Red-Black Trees: The Problem With Insertion 12 59 7 8 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

16 Mudasser Naseer 16 10/20/2015 ● Insert 8 ■ Where does it go? ■ What color should it be? Red-Black Trees: The Problem With Insertion 12 59 7 8 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

17 Mudasser Naseer 17 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ 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 12 59 7 8

18 Mudasser Naseer 18 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? 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 12 59 7 8 11

19 Mudasser Naseer 19 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Can’t be red! (#3) 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 12 59 7 8 11

20 Mudasser Naseer 20 10/20/2015 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) 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 12 59 7 8 11

21 Mudasser Naseer 21 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Solution: recolor the tree 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 12 59 7 8 11

22 Mudasser Naseer 22 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 10 ■ 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 12 59 7 8 11

23 Mudasser Naseer 23 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? ■ What color? 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 12 59 7 8 11 10

24 Mudasser Naseer 24 10/20/2015 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 structure to allow recoloring ■ Goal: restructure tree in O(lg n) time 12 59 7 8 11 10

25 Mudasser Naseer 25 10/20/2015 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 C AB x A y BC rightRotate(y) leftRotate(x)

26 Mudasser Naseer 26 10/20/2015 rightRotate(y) 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 C AB x A y BC

27 Mudasser Naseer 27 10/20/2015 Rotation Example ● Rotate left about 9: 12 59 7 8 11

28 Mudasser Naseer 28 10/20/2015 Rotation Example ● Rotate left about 9: 512 7 9 118

29 Mudasser Naseer 29 10/20/2015 Rotation Example ● After Colouring 9 118

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 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 Mudasser Naseer 30 10/20/2015

31 Rotation - Example Mudasser Naseer 31 10/20/2015

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 other path. Mudasser Naseer 32 10/20/2015

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 for insertion). Although insert and delete operations are complicated, their times remain O(lg n). Mudasser Naseer 33 10/20/2015

34 Advantages ● Red-black trees offer the best possible worst- case insertion time, deletion time, and search time. Mudasser Naseer 34 10/20/2015

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 35 10/20/2015


Download ppt "Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees."

Similar presentations


Ads by Google