Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 24 Red Black Trees.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 24 Red Black Trees."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 24 Red Black Trees

2 2 Properties of Red-black trees 1.Every node is either red or black 2.The root is black 3.Every leaf (nil(T)) is black 4.If a node is red, both children are black 5.For each node, all paths from that node to the descendent leaves contain the same number of black nodes. 26 17 1421 41 30 NIL Usually omitted when drawing trees NIL

3 3 Red-Black Insertion Insertion in a Red-Black Tree starts the same way as insertion into a regular binary-search-tree. Insert node z at the bottom of the tree by following the branches from root to leaf in the correct order. Then: 1) Color node z red. 2) Call RB-Insert-Fixup(T, z) to regain Red-Black Tree properties.

4 4 RB-Insert(T, z) pseudocode RB-Insert(T, z)... {Insert z into T, just like Binary-Search-Tree}... left[z]  nil[T] right[z]  nil[T] color[z]  RED RB-Insert-Fixup(T, z)

5 5 Fixing up the tree What might need fixing? PropertyDescriptionTrue? 1Every node in the tree is red or blackyes 2The root is black? 3Every leaf (nil[T]) is blackyes 4If a node is red, both children are black? 5All paths from any given node to the yes descendant leaves contain the same number of black nodes. Properties, 1, 3 and 5 still hold. Therefore, we only need to check properties 2 and 4 and fix them if they are violated.

6 6 Things we know The following are true at the start of RB-Insert-Fixup: a) Node z is red. b) if p[z] is the root, then p[z] is black. Therefore, if p[z] is red, then p[p[z]] exists. c) If property 4) is violated, then it is because p[z] is red. d) If property 2) is violated, it is because z is the root of the tree. As we fix up the tree, we keep these facts the same after each iteration of the fixup (i.e. they are loop invariant).

7 7 Cases to Consider If property 2 is violated, then z is the root. Color z black and we are done. Cases to consider for Property 4: Case 1: z's parent's sibling (i.e. z's uncle) is also red. Case 2: z's uncle is black and z is a right child. Case 3: z's uncle is black and z is a left child.

8 8 Case 1 Case 1: z's uncle is also red. C A B D   z uncle y C B   z a) Color both p[z] and uncle of z black. b) Color p[p[z]] red (to maintain equal numbers of black nodes on all paths. c) Move z pointer up two levels (to p[p[z]]). d) Repeat check for violation of properties 2 or 4. C A D

9 9 Pseudocode for case 1 while color[p[z]] = RED do{violation of property 4} if p[z] = left[p[p[z]]] then y  right[p[p[z]]]{y is uncle} if color[y] = RED then{case 1 applies} color[p[z]]  BLACK color[y]  BLACK color[p[p[z]]]  RED z  p[p[z]]...{consider cases 2 and 3} else {else clause is the same except left and right exchanged}

10 10 Case 2 Case 2: z's uncle is black and z is a right child. Use left rotation to convert to case 3. Case 3: z's uncle is black and z is a left child. A B   z uncle y B A    z uncle y Case 2 Case 3 if z = right[p[z]] then z <- p[z] Left-Rotate(T, z) {Proceed with case 3} C D C D

11 11 Case 3 B A    z uncle y Case 3 Case 3: z's uncle is black and z is a left child. AC    z 1) Color p[z] black. 2) Color p[p[z]] red. 3) Perform right rotation. After fixing case 3, we are done. There are no more violations (Why?) C D B D

12 12 Pseudocode for case 3 color[p[z]]  BLACK color[p[p[z]]]  RED Right-Rotate(T, p[p[z]]) {End of While loop} {After the end of the while loop, set the root color to black:} color[root[T]]  BLACK {fix any violations of property 2} Running time of RB-Insert-Fixup = ?


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 24 Red Black Trees."

Similar presentations


Ads by Google