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
x y β α γ x y β α y x β α γ L EFT -R OTATE (T,x) R IGHT -R OTATE (T,y) L EFT -R OTATE (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 then p[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 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
RB-I NSERT (T,z) //insert z into T 1 y ← nil[T] 2 x ← root[T] 3 while x≠ nil[T] 4 do y←x 5 if key[z]< key[x] 6 then x ← left[x] 7 else x ← right [x] 8 p[z] ←y 9 if y=nil[T] 10 then root[T] ← z 11 else if key[z]<key[y] 12 then left[y] ← z 13 else right[y] ← z 14 left[z] ←nil[T] 15 right[z] ← nil[T] 16 color[z] ← RED 17 RB-I NSERT -F IXUT (T,z)
RB-INSERT-FIXUP(T,z) 1 while color [p[z]] = RED 2 do if p[z]=left [p[p[z]]] 3 then y ← right[p[p[z]]] 4 if color [y] = RED 5 then color 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 L EFT -R OTATE (T,z) //case 2 12 color [p[z]] ← BLACK //case 3 13 color [p[p[z]]] ← RED // //case 3 14 R IGHT -R OTATE (T, p[p[z]]) //case 3 15 else (same as then clause with “ right”and “left” exchanged 16 color [root [T]] ←BLACK
z z y Case 1 : z’s uncle y is red y Case 2 (a) (b) 7
Case z y Case z (c) (d)
Case 1-1: z’s uncle y is red α α C D B z y A βγ δε C D B z A βγ δε new z α C DB z y A β γ δ ε C DB z y A βα γ δ ε 做法 : 改變 parent, uncle, grandparent 的 color Problem: 連續雙 red α β γ δ ε :Black height 不變
Case 1-2: z’s uncle y is red α C D B z y A β γ δ εε α C DB z y A β γ δε new z 做法 : 改變 parent, uncle, grandparent 的 color Problem: 連續雙 red α β γ δ ε :Black height 不變 If c’s parent is red? Continue… C D B z y A β γ δ new z α C DB z y A β γ δε
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 α C B z y A β γ δ case 3-1case 2-1 C B z y A β α γ δ B A z βα γ δ C Left rotation Change color: parent & grandpa right rotation α C B z y A βγ δ Case 3-2 Case 2-2 B C z βα γ δ A α C y B z A z β γ δ Right rotate
4 (a) (b) (c) Case 3-2 (d) (e) Case 1-2 +root must be black insertion
(g) (f)
5 (h) Insert
5 (i)Insert 14 Case
5 (j)
5 (k)
5 (l)
5 (m) Case
5 (n)
5 (o) Insert 17 Case
5 (p) Case
12 (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-D ELETE (T,z) 1 if left[z]=nil[z] or right[z]=nil[T] 2 then y ←z 3 else z ←T REE -S UCCESSOR (z) 4 if left[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 if color [y] = BLACK 17 then RB-D ELETE -F IXUP (T,x) 18 return y
RB-D ELETE F IXUP (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 if color[w] = RED 5 then color[w] ← BLACK //Case 1 6 color [p[x]] ← RED //Case 1 7 L EFT -R OTATE (T,p[x]) //Case 1 8 w ← right [p[x]] //Case 1 9 if color [left[w]] = BLACK and color [right[w]]= BLACK 10 then color[w] ← RED//Case 2 11 x ← p[x] //Case 2 12 else if color [left[w]] = BLACK 13 then color [left[w]] ← BLACK //Case 3 14 color[w] ← RED //Case 3 15 R IGHT -R OTATE (T,w) //Case 3 16 w ←right [p[x]] //Case 3 17 color[w] ← color [p[x]] //Case 4 18 color [p[x]] ← BLACK //Case 4 19 color [right[w]] ← BLACK //Case 4 20 L EFT -R OTATE (T,p[x]) //Case 4 21 x ← root[T] //Case 4 22 else (same as then clause with”right”and”left”exchanged) 23 color[x] ← BLACK // 若 x is red, 改為 black, black height 即能維持
B A x βα Case 1 δγζε D CE w D x E w B AC βαδγ ζε new w (a) B A x βα Case 2 δγζε D CE w new x (b) c B A βα δγζε D CE c :Red or black (C) (d) y 為真正被 deleted 之 node, x 是 y 的 right or left child restructure Reduce 1 black height recolor
B A x βα Case 3 δγζε D CE w (c) B A x βα Case 4 δγζε D CE w (d) c c new w B A βα δ γ ζε C D E c x c’ D EB AC βαδγ ζε 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.
12 (a) initial deletion
12 (b) Delete
(c) Delete restructure
7 (d) Delete 17
7 (e) Delete 18
7 (f)
7 (g) After recoloring Delete 15
7 (h) Delete 16
7 (i) (j) (k) adjustment recoloring
(a) (b) (c) or 7 86
Insertion: Case 1: The Sibling w of v is Black u v z w u v z w u v z w u v zw (a) black Double red
20 b a c (b) After a trinode restructuring 3010 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.
Case 2: The Sibling w of v is Red … 30 … u v w (a) z 30 u v w (b) z … recoloring
… 30 … x y r z 40 … (a) Deletion: case 1: the sibling y of r is black and Has a red child z.
… 30 … … (b) x y z r 40
… 20 … … (c)After restructure 20 b a c r (a)(b)
10 30 … x y r (a) 10 … Case 2: the sibling y of r is black and both children of y are black.
x y r (b) 10 … … 10 After recoloring
x y r (a)
x y r (b) After recoloring
20 30 (a) … 10 … 40 … 30 x y r z Case 3: the sibling y of r is red.
20 30 (b) After adjustment … 10 … 40 … 20 y zx r
Deletion complexity The algorithm for removing an item from a red-black tree with n items takes O(log n) time and performs O(log n) recolorings and at most one adjustment plus one additional trinode restructuring. Thus it perform at most two restructure operations.
2 5 (1)Insert 2(2)Insert (3)Insert Change Color 2 13 (4)Insert (5)Insert RL (6)Insert
(7)Insert RR (8)Insert (9)Insert RL
(1)Delete Rotate Del (2)Delete Del
最主要的三個判斷 判斷一:爸爸是爺爺的左 or 右兒子 判斷二: uncle 是不是紅的 判斷三:我是爸爸的左 or 右兒子 所以會有八種可能 請往下看 ( 注意:例圖不是由樹根畫起 而是最低的三層節點。 )
爸爸是爺爺的左兒子系列 Case 1 Uncle 是紅的 Case 2 在爸爸右邊 Case 3 在爸爸左邊
爸爸是爺爺的右兒子系列 Case 1 Uncle 是紅的 Case 2 在爸爸右邊 Case 3 在爸爸左邊
Case 1 (uncle 是紅的 ) 解決方法 爺爺變紅色 爸爸變黑色 原來的樣子 回左兒子系列
Case 2 ( 爸在爺右邊 ) 解決方法 原來的樣子 做向左旋轉 這樣就變成可以用 Case 3 來解決了 回左兒子系列
Case 3 ( 爸在爺左邊 ) 解決方法 原來的樣子 爺爺變紅色 爸爸變黑色 做向右旋轉 回左兒子系列
8 8 加入 加入 變色 Case 3 2 右轉 Case 2 左轉 加入 8
8 7 5 Case 變色 2 根黑色 Case 變色 加入 4 加入 2 加入 6
加入 3
Case 變色 Case 變色 2 右轉 加入 1
謝謝您的流覽 希望有所幫助 有問題請洽
exercise Complete the following algorithm: RB- DELETE FIXUP(T,x) & RB-INSERT- FIXUP(T,z) 實作習題四 : 以 java 實作 red-black tree (similar to 習題 1)