红黑树算法详解
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
B
D
α
β
α
z A
γ
β
γ
z A
δ
ε
δ
ε
做法:改變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
//Set y.
//Turn y’s left subtree into x’s right subtree.
//Link x’s parent to y.
11 left[y] ←x 12 p[x ] ←y
//Put x on y’s left
RB-INSERT(T,z) //insert z into T
then y ← right[p[p[z]]] if color [y] =RED then color p[z] ←BLACK color [y ] ←BLACK
//case 1 //case 1
color [p[p[z]]] ← RED //case 1 z ←[p[z]] else if z = right [p[z]] then z← p[z] LEFT-ROTATE(T,z) color [p[z]] ←BLACK //case 2 //case 2 //case 3
Left rotation
C A C
Change color: parent & grandpa right rotation
B B z A C
α β
δy
B z z A
δy γ α
γ
case 2-1
α
β
γ
δ
β
case 3-1
Right rotate
C y
C B B C Az z A z
α
B z
y
A
α
RB-DELETE(T,z)
1 if left[z]=nil[z] or right[z]=nil[T]
2
3
then y ←z
else z ←TREE-SUCCESSOR(z)
4 if left[y] ≠ nil[T] 5 6 then x← left[y] else x ← right[y]
7 p[x] ← p [y] 8 if p[y]= nil[T] 9 10 11 12 13 if y ≠z 14 15 then key [z] ← key [y] copy y’s satellite data into z then root [T] ← x else if y=left [p[z]] then left [p[z]] ← x else right [p[z]] ← x
(a)
x A
B D w
Case 1 restructure
E x A
D B new w C E w
α
β
γ
C
ε
δ
δ
(d) (C)
δ
ε
δ
α
β
γ
(b)
x A
B c D w
Case 2
recolor
E
new x B c A D
α
β γ
C
α δ
β γ
C
E
δ
ε
δ
ε
δ
Reduce 1 black height
// //case 3
//case 1
13
14 15
color [p[p[z]]] ← RED
RIGHT-ROTATE(T, p[p[z]]) //case 3 else (same as then clause with “ right”and “left” exchanged
16 color [root [T]] ←BLACK
7
4 3 5
12 15 14
(i)Insert 14 Case 2-2
7
4 3 5 12
14 15
(j)
7
4 3 5 12
14 15 18
(k)
7
4 3 5 12
14 15 18
(l)
7
4 3 5 12
14 15 18 16
(m) Case 2-2
7
4 3 5 12
14 16 15 18
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:
16 if color [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 if color[w] = RED 5 then color[w] ← BLACK 6 color [p[x]] ← RED 7 8 9 10 11 12 13 14 15 LEFT-ROTATE(T,p[x]) w ← right [p[x]] if color [left[w]] = BLACK and color [right[w]]= BLACK then color[w] ← RED x ← p[x] else if color [left[w]] = BLACK then color [left[w]] ← BLACK color[w] ← RED RIGHT-ROTATE(T,w)
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.
Example of A red-black tree
1 2
5
1 5 1 0 1 3 1 7 wenku.baidu.com 4
3 4 7
1 1
8
6
x α β y
LEFT-ROTATE(T,x) x y RIGHT-ROTATE(T,y) α
y x γ β
γ LEFT-ROTATE(T,x)
1 y←right[x]
2 right[x] ← left[y] 3 if left[x] ≠ nil[T] 4 then p[left[y] ] ←x 5 p[y ] ← p[x ] 6 if p[x ] = nil[T] 7 then root [T] ←y 8 else if x=left[p[x]] 9 10 then left[p[x]] ←y else right[p[x]] ←y // β’s father
δ
γ
β γ
Case 3-2
β
Case 2-2
δ
α
β
γ
δ
insertion
4 4
7
4
7 12
(a) (b)
7
4 12 4
7
12
(c) Case 3-2
15
(d)
(e) Case 1-2 +root must be black
7
7 12 15 3
4
4
12 15
(f)
(g)
7
4 3 5
12 15
(h) Insert 5
//Case 1 //Case 1 //Case 1 //Case 1
//Case 2 //Case 2
//Case 3 //Case 3 //Case 3 //Case 3 //Case 4 //Case 4 //Case 4 //Case 4 //Case 4
16
17 18
19
w ←right [p[x]]
(n)
7
4 3 5 12
14 16 15 17 18
(o) Insert 17
Case 1-2
7
4 3 5 12
14 16 15 17 18
(p) Case 3-2
14
7 4 3 5 12 15
16 18 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).
(a)
2 1 5 z 4 7
11 14 15 8 y Case 1 : z’s uncle y is red
(b)
2 1 5 4 7 z 8
11 14 y 15
Case 2
(c)
z 2 1 4
11 7 8 5 Case 3 14 y 15
7
(d)
1
z 2 5 8
11
14 15
4
Case 2
Case 1-1: z’s uncle y is red
– 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).
1 y ← nil[T] 2 x ← root[T] 3 while x≠ nil[T]
4
5 6 7
do y←x
if key[z]< key[x] then x ← left[x] 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 13 then left[y] ← z else right[y] ← z
color[w] ← color [p[x]] color [p[x]] ← BLACK color [right[w]] ← BLACK
LEFT-ROTATE(T,p[x])
20
21 x ← root[T] 22 else (same as then clause with”right”and”left”exchanged) 23 color[x] ← BLACK //若x is red, 改為black, black height 即能維持
C A D B z y A new z C D B z
α β
α δ γ ε β
δ γ
ε
Problem:連續雙red
α β γ δ ε :Black height不變
C
B z A D y
new z B z A
C
D y
γ
β
α
δ
ε
γ
β
α
δ
ε
做法:改變parent, uncle, grandparent的color
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 while color [p[z]] =RED
2
3 4 5 6 7 8 9 10 11 12
do if p[z]=left [p[p[z]]]
Case 1-2: z’s uncle y is red
new z
C y A D C D
y
A
α
β δ
β
ε δ
B z
B z
γ
γ
ε
Problem:連續雙red
α β γ δ ε :Black height不變 If c’s parent is red? Continue…
C y B D
new z C y