680 likes | 806 Views
red -black tree. Lai Ah Fur. Background: AVL trees may require many restructure operations (rotations) to be performed after an element removal, (2,4) trees may require many fusing or split operations to be performed after either an insertion or removal.
E N D
red-black tree Lai Ah Fur
Background: • AVL trees may require many restructure operations (rotations) to be performed after an element removal, • (2,4) trees may require many fusing or split operations to be performed after either an insertion or removal. • The red-black tree, does not have these drawbacks. It requires that only O(1) structural changes be made after an update in order to stay balanced.
Property of A red-black tree • A red-black tree is a binary search tree with nodes colored red and black in a way that satisfies the following properties: • Root Property: The root is black. • External Property: Every external node is black. • Internal Property: The children of a red node are black. • Depth property: All the external nodes have the same black depth, which is defined as the number of black ancestors minus one. • The height of a red-black tree storing n items is O(log n).
Example of A red-black tree 12 5 15 3 10 13 17 14 7 4 11 6 8
x x y α y y x β β β α α γ LEFT-ROTATE(T,x) γ RIGHT-ROTATE(T,y) 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[x] ≠ nil[T] 4 thenp[left[y] ] ←x // β’s father 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 thenleft[p[x]] ←y 10 elseright[p[x]] ←y 11 left[y] ←x //Put x on y’s left 12 p[x ] ←y
RB-INSERT(T,z) //insert z into T 1 y ← nil[T] 2 x ← root[T] 3 while x≠ nil[T] 4 doy←x 5 ifkey[z]< key[x] 6 then x ← left[x] 7 elsex ← right [x] 8 p[z] ←y 9 if y=nil[T] 10 thenroot[T] ← z 11 elseif key[z]<key[y] 12 thenleft[y] ← z 13 else right[y] ← z 14 left[z] ←nil[T] 15 right[z] ← nil[T] 16 color[z] ← RED 17 RB-INSERT-FIXUT(T,z)
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[z]] //case 1 9 else if z= right [p[z]] 10 then z← 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
11 (a) 2 14 1 7 15 5 8 y 4 z Case 1 : z’s uncle y is red (b) 11 y 2 14 1 7 z 15 5 8 4 Case 2
11 (c) 7 y z 14 2 8 15 1 5 4 Case 3 7 (d) z 2 11 1 5 8 14 15 4 Case 2
C C new z A y A D D α z z B B δ ε δ ε β γ β γ C C new z B D y B D y z A γ z A γ ε ε δ δ β α β Case 1-1: z’s uncle y is red α α β γ δε:Black height不變 Problem:連續雙red α 做法:改變parent, uncle, grandparent的color
C C y y B B D D α α β β γ γ z z A A δ δ ε ε Case 1-2: z’s uncle y is red new z C y C A D y A D β α z B β z ε ε B δ γ δ γ α β γ δε:Black height不變 If c’s parent is red? Continue… Problem:連續雙red new z 做法:改變parent, uncle, grandparent的color
Case 2-1: z’s uncle y is black and z is a right child Case 3-1: z’s uncle y is black and z is a left child Change color: parent & grandpa right rotation Left rotation α C C B A B z A C δ δ y y z A γ B z γ δ α β α β γ β Right rotate C C case 2-1 case 3-1 y B y α α B A z C A δ β B z A z z γ δ α β γ β γ δ Case 2-2 Case 3-2
insertion 4 4 4 7 7 (a) 12 (b) (c) Case 3-2 7 7 4 4 12 12 15 (d) (e) Case 1-2 +root must be black
7 7 4 4 12 12 15 3 15 (f) (g)
4 12 3 5 7 15 (h) Insert 5
4 12 3 5 14 7 15 (i)Insert 14 Case 2-2
4 14 3 12 5 7 15 (j)
4 14 3 12 5 18 7 15 (k)
7 4 14 3 5 12 15 18 (l)
16 7 4 14 3 5 12 15 18 (m) Case 2-2
7 4 14 3 5 12 16 15 18 (n)
7 4 14 3 5 12 16 15 18 17 (o) Insert 17 Case 1-2
7 4 14 3 5 12 16 15 18 17 (p) Case 3-2
14 7 16 4 12 15 18 3 5 17 (q)
Insertion complexity • The insertion of a key-element item in a red-black tree storing n items can be done in O(log n) time and at most O(log n) recolorings and one trinode restructuring (a restructure operation).
RB-DELETE(T,z) 1 ifleft[z]=nil[z] or right[z]=nil[T] 2 then y ←z 3 else z ←TREE-SUCCESSOR(z) 4 ifleft[y] ≠ nil[T] 5 then x← left[y] 6 else x ← right[y] 7 p[x] ← p [y] 8 if p[y]= nil[T] 9 then root [T] ← x 10 else if y=left [p[z]] 11 then left [p[z]] ← x 12 else right [p[z]] ← x 13 if y ≠z 14 then key [z] ← key [y] 15 copy y’s satellite data into z 16 ifcolor [y] = BLACK 17 then RB-DELETE-FIXUP(T,x) 18 return y
RB-DELETE FIXUP(T,x) //y為真正被deleted之node, x是y的right or left child • 1 While x ≠ root[T] and color[x] =BLACK • 2 do if x =left [p[x]] • 3 then w ← right [p[x]] • 4 ifcolor[w] = RED • 5 then color[w] ← BLACK //Case 1 • 6 color [p[x]] ← RED //Case 1 • 7 LEFT-ROTATE(T,p[x]) //Case 1 • 8 w ← right [p[x]] //Case 1 • ifcolor [left[w]] = BLACK and color [right[w]]= BLACK • thencolor[w] ← RED //Case 2 • x ← p[x] //Case 2 • 12 else ifcolor [left[w]] = BLACK • 13 then color [left[w]] ← BLACK //Case 3 • 14 color[w] ← RED//Case 3 • 15 RIGHT-ROTATE(T,w) //Case 3 • w ←right [p[x]] //Case 3 • color[w] ← color [p[x]] //Case 4 • color [p[x]] ← BLACK //Case 4 • color [right[w]] ← BLACK //Case 4 • LEFT-ROTATE(T,p[x]) //Case 4 • x ← root[T] //Case 4 • else(same as then clause with”right”and”left”exchanged) • 23 color[x] ← BLACK //若x is red, 改為black, black height 即能維持
Case 1 (a) B D restructure x A D w B E w α β C E x A C ε ζ new w γ δ ε ζ α β γ δ (d) (C) Case 2 (b) c new x c B B recolor x A D w A D α β C E α β C E γ δ ε ζ γ δ ε ζ Reduce 1 black height :Red or black y為真正被deleted之node, x是y的right or left child
Case 3 c B (c) c B new w A C x x A D w α β γ D α β C E δ E γ δ ε ζ ε ζ Case 4 (d) c c B D x A D w B E α β C c’ E A C c’ ε ζ γ δ ε ζ α β γ δ new x=root[T]
Case 1: x’s sibling w is red Case 2: x’s sibling w is black, and both of w’s children are black Case 3: x’s sibling w is black, w’s left child is red, and w’s right child is black Case 4: x’s sibling w is black, and w’s right child is red
If v is a 2-node, then keep the (black) children of v as is. • If v is a 3-node, then create a new red node w, give v’s first two (black) children to w, and make w and v;s third child be the two children of v. • If v is a 4-node, then create two new red nodes w and z, give v’s first two (black) children to w, give v’s last two (black) children to z, and make w and z be the two children of v.
deletion 14 7 16 4 12 15 18 3 5 17 (a) initial
14 7 16 4 12 15 18 5 17 (b) Delete 3
14 7 16 4 15 18 restructure 5 17 (c) Delete 12
14 5 16 Delete 17 4 7 15 18 17 (d)
14 5 16 Delete 18 4 7 15 18 (e)
14 5 16 4 7 15 (f)
14 5 16 Delete 15 4 7 15 (g) After recoloring
14 5 16 Delete 16 4 7 (h)
5 14 4 4 7 (k) 14 5 14 5 7 4 7 adjustment (j) (i) recoloring
15 15 (a) 13 14 13 14 or 14 13 (b) 7 6 7 8 6 8 (c)
Insertion: Case 1: The Sibling w of v is Black. u v w z u u 30 30 v v 20 10 w w z z black 20 10 Double red u 10 10 v 20 30 w z 20 30 (a)
10 30 b 20 a c (b) After a trinode restructuring • Take node z, its parent v, and grandparent u, and temporarily relabel them as a,b,and c, in left-to-right order, so that a, b, c will be visited in this order by an inorder tree traversal. • Replace the grandparent u with the node labeled b, and nodes a and c the children of b, keeping inorder relationships unchanged.
20 40 Case 2: The Sibling w of v is Red. u 30 v 10 20 30 40 w z 10 recoloring (a) u … 30 … 30 … … v w 20 40 10 20 40 z 10 (b)
Deletion: case 1: the sibling y of r is black and Has a red child z. x … 30 … 30 … … r y 40 20 10 20 z 10 40 (a)
… 30 … x … … 30 y 10 r 10 20 z 40 20 40 (b)
40 b … 20 … 20 … … a c 10 30 10 30 r 40 (a)(b) (c)After restructure
Case 2: the sibling y of r is black and both children of y are black. 10 10 30 … x … 30 y 20 r 20 40 40 (a)
10 10 … x … 30 y 20 30 r 20 40 40 After recoloring (b)
30 x 30 y 20 r 20 40 40 (a)