Ch 12_5 More Trees

advertisement
More
Trees
Outline
B Tree, B* Tree, B+ Tree
2-3 Tree, 2-3-4 Tree
Red-Black Tree (RBT)
Left-Leaning Red-Black Tree
Double Red & Double Black
B Tree
B-Tree
is in memory of R. Bayer
The B tree is a generalization of a binary
search tree in that a node can have more than
two children [wiki].
Degree 為 d 的 B tree:
1) 每個node 包含至多 d 個 child pointers
(或 d-1個 elements)
2) 每個 node 至少 1/2 滿
(即至少 [ (d-1)/2]個 elements)
B-Tree of Degree 3
20
10
28
25
30
B* Tree
B-tree 的 node 至少 2/3 滿
B* Tree of Degree 4
6
2
4
10
20
15
28
23
26
30
35
B+ Tree
• 包含 index pages 和 data pages
• root node 和 internal nodes
為 index pages (keys only).
• leaf nodes 為 data pages (排序的data )
data 即 element (含有 key)
• 每個 node 至少 1/2 滿 (Fill Factor 50%).
B+ Tree
index page
data page
This B+ tree:
Number of Keys
4
Number of Pointers
5
Fill Factor
50%
Minimum Keys in each page
2
2-3 Tree
2-3 Tree
為search tree 可為空或符合:
– 每個 internal node 為 2-node 或 3-node
2-node有2 child pointers, 3-node有3 child pointers
– 所有external nodes 都在相同 level.
2-node
3-node
40
A
B
10 20
C
internal node
80
external nodes
2-3 Tree Insertion
insert Case 1: 插入 70
• 先尋找 70. 發現不在其中.
• 須知尋找70時 遇到哪node. 是
含 80 的 node C
• node C 只有一個 element, 所以
70 可放 C
A
40
B
10 20
C
70 80
2-3 Tree
insert Case 2: 插入 30
•
•
•
•
•
•
會遇到 30 的是 node B
B 為 3-node, 須產生新 node D.
B 含 elements 10, 20, 30
B中最大element 30 放D
最小element 10 放 B.
中間element 20放B的parent A B
10
A
20 40
D
30
Figure 3
C
70 80
2-3 Tree Insertion (Cont.)
Insert case 3: 插入 60
•
•
•
•
•
•
•
•
•
尋找60會遇 node C
C 為 3-node,需產生新 node E
C 含 elements 60,70,80
中間值 70 放在C的parent A
最小值 60放C 最大值80放E
A 為 3-node,產生新 node F
A含 elements 20, 40, 70
中間值 40 放在A的parent G (需產生 G)
最小值 20放A 最大值70放F
2-3 Tree Insertion (Cont.)
G
40
A
F
20
B
10
D
30
70
C
60
E
80
Figure4 Insertion of 60 into the 2-3 tree of Figure 3
2-3 Tree Deletion
A
50 80
B
10 20
D
C
60 70
90 95
(a) Initial 2-3 tree
A
50 80
B
10 20
D
C
60
(b) 70 deleted
90 95
2-3 Tree Deletion (Cont.)
A
50 80
B
10 20
D
C
60
95
(c) 90 deleted
A
20 80
B
10
D
C
50
(d) 60 deleted
95
2-3 Tree Deletion (Cont.)
A
20
B
10
C
50 80
(e) 95 deleted
A
20
B
10
C
A
20 80
80
(g) 10 deleted
(f) 50 deleted
2-3-4 Tree
2-3-4 Tree
為 search tree 為空或是滿足:
– 每個 internal node 為 2, 3,或 4 node.
2-node 有2 child pointers, 3-node有3 child
pointers, 4-node 有4 child pointers
– 所有external nodes 都在相同 level.
2-3-4 tree 類似2-3 tree, 但它有 4-node (即
四個 pointers 和三個 elements) 如下圖
50 60 70
2-3-4 Tree Insertion
There are 3 cases for a 4-node:
Case 1: It is the root
Case 2: Its parent is a 2-node
Case 3: Its parent is a 3-node (fig. omitted)
2-3-4 Tree Insertion
• Case 1: It is the root.
t
t (root)
y
x
y
z
x
a
b
c
d
a
Figure1
z
b
when the root is a 4-node
c
d
2-3-4 Tree Insertion
• Case 2: Its parent is a 2-node
z
w
a
b
x
y
c
x
e
d
z
w
a
b
e
y
c
Figure 2 when the child of a 2-node is a 4-node
d
2-3-4 Tree Deletion
• Deleting p
– The following cases are to be considered:
•
•
•
•
•
p is a leaf.
q is not a 2-node. (q is a child of p)
q is a 2-node and its nearest sibling r is a 2-node.
q is a 2-node and its nearest sibling r is a 3-node.
q is a 2-node and its nearest sibling r is a 4-node.
2-3-4 Tree Deletion (Cont.)
q is the left child of a 3-node p
p
w
q
p
z
f
r
v
a
x
x
b
c
q
y
d
v
e
a
z
r
w
b c
Figure1 when the nearest sibling r is 3-node
f
y
d
e
2-3-4 Tree Deletion (Cont.)
q is the left child of a 4-node p
p
v
q
y
w
f g
r
u
a
z
w
b
c
q
u
x
d
e
a
y
z
f g
r
v
b c
Figure1 when the nearest sibling r is 3-node
x
d
e
2-3-4 Tree
2-3-4 tree 可轉成binary search tree
稱為 red-black tree
可節省儲存空間 因為2-3-4 node
會浪費不少未存資料的空的空間
Red-Black Tree
(RBT)
Red-Black Tree
red-black tree 為
binary search tree:
• 每個 node 不是red就是black
• 每個leaf (NULL) 都為black
• red node 的兩個children都為
black.
• 每個 path 含相同數目的 black
nodes.
• red node不可接著red node
(不可紅紅)
A basic red-black tree
Red-Black Tree
A red-black tree with n internal nodes
has height at most 2 log(n+1).
Red-Black tree can always be searched
in O (log n) time.
Red-Black Tree
S
L
S L
OR

c
S
Left-leaning
a b
c
a
b
a
Rightleaning
L
b
S for Small; L for Large.
Figure 1 Transforming a 3-node into two red_black nodes
c
Red-Black Tree
M
SML

S
a
b
c
L
d
a
b
c
d
S for Small;M, Middle; L, Large.
Figure 2 Transforming a 4-node into two red_black nodes
將下圖的 Red-Black Tree 轉成 2-3-4 Tree
依序 (1)刪除60
(2)加入8
再轉回 Red-Black Tree
50
10
7
5
70
40
9
30
60
80
75
90
85
92
上圖轉成的 2-3-4 Tree
50
10
5
7
9
30 40
70 80
60
75
85 90 92
刪除 60
70
加入 8
7
8
轉回 Red-Black Tree
50
7
80
5
10
70
30
8
9
90
75
40
85
92
Left-Leaning
Red-Black Tree
LLRBT is easier to implement
than RBT, especially the
deletion.
It requires 3-nodes are leftleaning, thus maintains 1-1
correspondence with 2-3-4 trees
(see next page).
LL Red-Black Tree
L
S L

c
Left-leaning
S
a b
c
a
b
S for Small; L for Large.
Transforming a 3-node into LL red black nodes
Double Red &
Double Black
During Red-Black Tree
Insertion, Double Red may
occur as shown next.
Red-Black Tree Insertion
我們要對左圖 insert 4
1)依 binary search tree
把 4 當 3 的 right child
2)依 red black tree 新加入者為red
故 3,4 形成右圖 Double Red 違反 Red Rule
2
2
1
3
1
3
4
43
Double Red in 2-3-4 Tree
1
2
3
1
2
3
已滿, 此時 insert 4
這 node 爆掉了,故要調整之
4
對應的 Red-Black Tree:
2
1
此時 3
4 叫 Double Red 雙紅,
表示原來 node 爆掉了
3
4
During Red-Black Tree
Deletion, Double Black
may occur as shown next.
Double Black in 2-3-4 Tree
7
5
8
7
8
此時 Delete 5
對應的 Red-Black Tree:
3
3
7
8
3
Double BLACK 雙黑線,表示
其中有個空 2-3-4 node.故要調整之
Download