Download presentation
Presentation is loading. Please wait.
Published byChristian Adam Gray Modified over 9 years ago
1
red-black tree Lai Ah Fur
2
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.
3
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).
4
3 4 6 8 7 1 1717 1515 1313 1414 1010 5 1212 Example of A red-black tree
5
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
6
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)
7
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
8
z 1 11 14 157 5 4 2 8 z y Case 1 : z’s uncle y is red 1 11 14 15 5 4 2 8 y Case 2 (a) (b) 7
9
Case 2 1 11 14 15 5 4 2 8 z y Case 3 1 7 14 15 5 4 2 z 7 11 8 (c) (d)
10
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 不變
11
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 β γ δε
12
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
13
4 (a) (b) (c) Case 3-2 (d) 4 7 4 7 12 4 7 4 7 15 (e) Case 1-2 +root must be black insertion
14
(g) 4 7 12 153 (f) 4 7 12 15
15
5 (h) Insert 5 3 15 412 7
16
5 (i)Insert 14 Case 2-2 3 15 412 7 14
17
5 (j) 3 15 12414 7
18
5 (k) 3 15 12414 7 18
19
5 (l) 31512 414 7 18
20
5 (m) Case 2-2 31512 414 7 18 16
21
5 (n) 31612 414 7 1815
22
5 (o) Insert 17 Case 1-2 31612 414 7 1815 17
23
5 (p) Case 3-2 31612 414 7 1815 17
24
12 (q) 1815 716 14 173 4 5
25
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).
26
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
27
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 即能維持
28
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
29
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]
30
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
31
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.
32
12 (a) initial 1815 716 14 173 4 5 deletion
33
12 (b) Delete 3 1815 716 14 17 4 5
34
(c) Delete 12 1815 716 14 17 4 5 restructure
35
7 (d) 1815 516 14 17 4 Delete 17
36
7 (e) 1815 516 14 4 Delete 18
37
7 (f) 15 516 14 4
38
7 (g) After recoloring 15 516 14 4 Delete 15
39
7 (h) 516 14 4 Delete 16
40
7 (i) 5 14 4 7 (j) 5 4 14 7 (k) 5 414 adjustment recoloring
41
15 13 14 6 7 8 15 13 14 (a) (b) (c) 14 13 or 7 86
42
Insertion: Case 1: The Sibling w of v is Black. 30 20 10 20 30 u v z w u v z w 20 10 u v z w 20 30 u v zw (a) black Double red
43
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.
44
Case 2: The Sibling w of v is Red. 10 20 30 40 … 30 … 10 2040 30 u v w (a) 4020 10 z 30 u v w (b) 4020 10 z … recoloring
45
… 30 … 10 20 30 x y r 40 20 10 z 40 … (a) Deletion: case 1: the sibling y of r is black and Has a red child z.
46
… 30 … 10 20 40 … (b) 30 20 10 x y z r 40
47
… 20 … 1030 40 … (c)After restructure 20 b a c 3010 40 r (a)(b)
48
10 30 … 20 30 x y r 40 20 40 (a) 10 … Case 2: the sibling y of r is black and both children of y are black.
49
20 30 30 x y r 40 20 40 (b) 10 … … 10 After recoloring
50
30 20 40 30 x y r 40 20 (a)
51
20 30 30 x y r 40 20 40 (b) After recoloring
52
20 30 (a) … 10 … 40 … 30 x y r 40 20 10 z Case 3: the sibling y of r is red.
53
20 30 (b) After adjustment … 10 … 40 … 20 y zx 3010 40 r
54
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.
55
2 5 (1)Insert 2(2)Insert 1 2 1 (3)Insert 3 2 13 Change Color 2 13 (4)Insert 7 7 2 13 (5)Insert 5 7 2 13 RL 7 2 15 3 (6)Insert 8 8 7 2 15 3
56
(7)Insert 9 8 7 2 15 3 9 RR 5 2 8 9 7 3 1 (8)Insert 11 5 2 8 9 7 3 1 11 (9)Insert 10 5 2 8 9 7 3 1 11 10 RL 5 2 8 10 7 3 1 11 9
57
(1)Delete 7 5 2 8 10 7 3 1 11 9 Rotate 5 2 10 11 8 3 1 9 7 Del 7 5 2 10 11 8 3 1 9 (2)Delete 9 5 2 10 11 8 3 1 9 Del 9 5 2 10 11 8 3 1
58
最主要的三個判斷 判斷一:爸爸是爺爺的左 or 右兒子 判斷二: uncle 是不是紅的 判斷三:我是爸爸的左 or 右兒子 所以會有八種可能 請往下看 ( 注意:例圖不是由樹根畫起 而是最低的三層節點。 )
59
爸爸是爺爺的左兒子系列 Case 1 Uncle 是紅的 Case 2 在爸爸右邊 Case 3 在爸爸左邊
60
爸爸是爺爺的右兒子系列 Case 1 Uncle 是紅的 Case 2 在爸爸右邊 Case 3 在爸爸左邊
61
Case 1 (uncle 是紅的 ) 解決方法 爺爺變紅色 爸爸變黑色 原來的樣子 回左兒子系列
62
Case 2 ( 爸在爺右邊 ) 解決方法 原來的樣子 做向左旋轉 這樣就變成可以用 Case 3 來解決了 回左兒子系列
63
Case 3 ( 爸在爺左邊 ) 解決方法 原來的樣子 爺爺變紅色 爸爸變黑色 做向右旋轉 回左兒子系列
64
8 8 加入 5 5 8 加入 7 7 8 5 8 7 5 1 變色 Case 3 2 右轉 7 5 5 8 7 Case 2 左轉 加入 8
65
8 7 5 Case 1 8 7 5 44 5 8 1 變色 2 根黑色 7 4 5 8 7 6 4 58 7 6 2 Case 1 8 7 2 46 5 變色 加入 4 加入 2 加入 6
66
8 7 2 46 5 8 7 2 4 6 5 3 加入 3
67
1 8 7 2 4 6 5 3 Case 1 1 8 7 2 4 6 5 3 變色 Case 3 1 8 7 2 4 6 5 3 1 變色 2 右轉 1 8 2 4 6 5 3 7 加入 1
68
謝謝您的流覽 希望有所幫助 有問題請洽 drumrick1600@hotmail.com
69
exercise Complete the following algorithm: RB- DELETE FIXUP(T,x) & RB-INSERT- FIXUP(T,z) 實作習題四 : 以 java 實作 red-black tree (similar to 習題 1)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.