Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms Jiafen Liu Sept. 2013.

Similar presentations


Presentation on theme: "Introduction to Algorithms Jiafen Liu Sept. 2013."— Presentation transcript:

1 Introduction to Algorithms Jiafen Liu Sept. 2013

2 Today’s Tasks Balanced Search Trees Red-black trees –Height of a red-black tree –Rotations –Insertion

3 Balanced Search Trees Balanced search tree: A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items. Examples: –A–AVL Tree –2–2-3-4 Tree –B–B Tree –R–Red-black Tree

4 Red-black trees This data structure requires an extra 1-bit color field in each node. Red-black properties: –Every node is either red or black. –The root and leaves (NIL’s) are black. –If a node is red, then its parent is black. –All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x).

5 Example of a red-black tree Convention: black-height of x does not include x itself.

6 Example of a red-black tree It could have a bunch of blacks, but it will never repeat two reds in a row.

7 Goals of red-black trees there are a couple of goals that we are trying to achieve. –These properties should force the tree to have logarithmic height, O(lgn) height. –The other desire we have from these properties is that they are easy to maintain. We can create a tree in the beginning that has this property. A perfectly balanced binary tree with all nodes black will satisfy those properties. The tricky part is to maintain them when we make changes to the tree.

8 Height of a red black tree Let's look at the height of a red black tree. And we will start to see where these properties come from. Theorem. A red-black tree with n keys has height h ≤ 2 lg(n + 1). –Can be proved by induction, as in the book P164. –Another informal way: intuition.

9 Intuition Merge red nodes into their black parents. This process produces another type of balanced search tree: 2-3-4 tree. –Any guesses why it's called a 2-3-4 tree? –Another nice property: All of the leaves have the same depth. Why? By Property 4

10 height of merged tree h' Now we will prove the height of merged tree h'. The first question is how many leaves are there in a red-black tree? –#(internal nodes) +1 –It can also be proved by induction. Try it.

11 Height of Red-Black Tree The number of leaves in each tree is n + 1 2 h' ≤ n + 1 ≤ 4 h' h' ≤ lg(n + 1) How to connect h' and h? We also have h' ≥ 1/2 h h ≤ 2 lg(n + 1).

12 Query operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(lgn) time on a red-black tree with n nodes.

13 Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree. How to INSERT and DELETE a node in Red Black Tree? –The first thing we do is just use the BST operation. –They will preserve the binary search tree property, but don't necessarily preserve balance.

14 Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree. How to INSERT and DELETE a node in Red Black Tree? –The second thing is to set color of new internal node, to preserve property 1. –We can color it red, and property 3 does not hold. –The good news is that property 4 is still true.

15 How to fix property 3? We are going to move the violation of property 3 up the tree. IDEA: Only red-black property 3 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. we have to restructure the links of the tree via “rotation”.

16 Rotations Rotations maintain the inorder ordering of keys. a ∈ α, b ∈ β, c ∈ γ ⇒ a ≤ A ≤ b ≤ B ≤ c. A rotation can be performed in O(1) time. –Because we only change a constant number of pointers.

17 Implement of Rotation P166

18 Example Insert 15

19 Example Insert 15 Color it red

20 Example Insert 15 Color it red Handle Property 3 by recoloring

21 Example Insert 15 Color it red Handle Property 3 by recoloring

22 Example Insert 15 Color it red Handle Property 3 by recoloring Failed !

23 RIGHT-ROTATE RIGHT-ROTATE(18) It turns out to be?

24 RIGHT-ROTATE We are still in trouble between 10 and 18. But made this straighter. It doesn't look more balanced than before. We can not resolve by recoloring. Then?

25 LEFT-ROTATE LEFT-ROTATE(7) It turns out to be?

26 LEFT-ROTATE LEFT-ROTATE(7) and recoloring at the same time It satisfy all the 4 properties

27 Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt or uncle of x if color[y] = RED then 〈 Case 1 〉

28 Case 1 Let denote a subtree with a black root. All have the same black-height. Push C’s black onto A and D, and recurse. Caution: We don't know whether B was the right child or the left child. In fact, it doesn't matter. How?

29 Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt/uncle of x if color[y] = RED then 〈 Case 1 〉 else if x = right[p[x]] //y is red, x,y have a zigzag then 〈 Case 2 〉 // This falls into case 3

30 Case 2 Now we have a zigzig. we may want a straight path between x. How?

31 Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt/uncle of x if color[y] = RED then 〈 Case 1 〉 else if x = right[p[x]] then 〈 Case 2 〉 // This falls into case 3 〈 Case 3 〉 //straight path

32 Case 3 Done! No more violations of RB property 3 are possible. Property 4 is also preserved. How?

33 Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt/uncle of x if color[y] = RED then 〈 Case 1 〉 else if x = right[p[x]] then 〈 Case 2 〉 // This falls into case 3 〈 Case 3 〉 else 〈 “then” clause with “left” and “right” swapped 〉 color[root[T]] ← BLACK

34 Analysis Go up the tree performing Case 1, which only recolors nodes. If Case 2 or Case 3 occurs, perform 1 or 2 rotations, and terminate. Running time of insertion? – O(lgn) with O(1) rotations. RB-DELETE — same asymptotic running time and number of rotations as RB- INSERT (see textbook).

35


Download ppt "Introduction to Algorithms Jiafen Liu Sept. 2013."

Similar presentations


Ads by Google