2-3-4 Tree

advertisement
Old
Trees
1
Outline
•
•
•
•
•
Tree
B-Tree
2-3 Tree
2-3-4 Tree
Red-Black Tree
2
Tree
– 從 root 開始存取.
– node 為 leaf (external node)或 internal node .
• internal node (parent)有一或多個 child nodes,
• 相同 parent 的 nodes 互稱為siblings.
3
Tree
Root
Node
Parent Node
Child Node leaf
Internal node
Node
Node
Node leaf
4
B-Tree
in memory of R. Bayer
5
B-Tree
– B tree 中 degree 為 d 的node 含
至多 d 個 pointers 及 d-1 elements
– 每個 node 至少 1/2 滿
至少含floor ( (d-1)/2 ) elements.
6
B-Tree
This is a B-Tree of degree 3
20
10
28
25
30
7
8
B* Tree
B-tree 的node 至少 2/3 full
重分配 elements,填滿2 child
nodes, 然後分成 3個 nodes
9
B* Tree
This is a B*-Tree of degree 4
6
2
4
10
20
15
28
23
26
30
35
10
B+ Tree
Data 排序儲存在 leaves.
• B+Tree 包含
index pages 和 data pages
• root 和中間 nodes為index pages
(keys only).
• leaf nodes為data pages.
• 每個 node 至少 1/2 full.
11
B+ Tree
index page
data page
This examples conforms to:
Number of Keys
4
Number of Pointers
5
Fill Factor
50%
Minimum Keys in each page
2
12
B+-Tree
insert an element 時,須考慮 3 cases:
Leaf Page Full
Index Page Full
No
No
Yes
No
Yes
Yes
13
B+-Tree
• Case 1: 插入一 element到非滿的leaf page
把 element 放sorted leaf page 的適當位置
14
B+ Tree
Case 2: 加入 element 時 leaf page 為滿,
但 index page 沒滿
1. 分裂leaf page
2. 把middle 放已排好的index page
3. left leaf page :
比middle 小的 elements
4. right leaf page :
比middle 大的 elements
.
15
B+-Tree
• 插入70 到B+ tree.
• 70 將加入到 leaf page: (50, 55, 60, 65)
• 不幸此 page已滿 . 我們須分裂( split ) page 如下:
Left Leaf Page
Right Leaf Page
50 55
60 65 70
16
B+-Tree
• middle 60 放在50和75之間的 index page
• 下面表示加入70後的B+ tree
17
B+-Tree
Case 3: 加入element 當 leaf page 和 index page 都滿了
1.
2.
3.
4.
5.
6.
7.
分裂 leaf page.
< middle 放在 left leaf page.
>= middle 放在right leaf page.
分裂 index page.
elements < middle 放在 left index page.
elements > middle 放在 right index page.
middle 放在上一層 index.
如上一層 index也滿了, 持續分裂 index pages (steps 4 - 7).
18
B+-Tree
• 加入95 到 B+ tree.
• 95將加入 (75, 80, 85, 90). 既然此page 已滿 , 分裂:
Left Leaf Page
Right Leaf Page
75 80
85 90 95
• middle 85, 升到 index page. 不幸地, index page 也滿
了, 所以分裂 index page:
Left Index Page
Right Index Page
New Index Page
25 50
75 85
60
19
B+-Tree
• 下圖表示 加入95 的結果
20
B+-Tree
• Deletion 須考慮 3 Cases:
Leaf Page Below
Fill Factor
Index Page Below
Fill Factor
Case 1
No
No
Case 2
Yes
No
Yes
Yes
Case 3
21
B+-Tree
Case 1: delete 後, leaf page 和 index page
不符合 B+tree
– 從 leaf page delete此element
– 把element安排成升冪排序.
– 如刪除的element在 index page,
用下一element來取代它
22
B+-Tree
• Delete 70
– 70 is in a leaf page containing 60, 65 and 70. This page will
contain 2 elements after the deletion. Since our fill factor is 50%
(2 elements), simply delete 70 from the leaf node.
70
23
B+-Tree
Case 2: delete後, index page 不夠滿
–
–
結合 leaf page 和它的 sibling.
改變 index page 來反應 leaf page 的改變
24
B+-Tree
• Delete 25
– 25 is found in the leaf node containing 25, 28, and 30. The fill
factor will be 50% after the deletion; however, 25 appears in the
index page. Thus, when we delete 25, we must replace it with 28 in
the index page.
25
B+-Tree
Case 3: Delete後 ,
index page 和 data page 不夠滿
1. 結合(merge) leaf page 和它的 sibling.
2. 調整 index page 來反應其改變
3. 結合 index page 和它的 sibling.
持續結合 index pages 直到符合tree要求,
或到達 root page.
26
B+-Tree
•
Delete 60
1.
2.
The leaf page containing 60 (60 65) will be below the fill factor after the
deletion. Thus, we must merge leaf pages.
With merged pages, the index page (75,85) will be reduced by one key.
Hence, it (85)will fall below the fill factor. Thus, we must merge index
pages.
3.
60 is now the only key in root index page. Obviously, it will be removed.
27
2-3 Tree
28
2-3 Tree
• 2-3 tree為 search tree 可為空 或:
每個 internal node 為 2-node 或 3-node:
2-node 有兩個 children
3-node 有三個 children
– Fig.1:
element
40
A
B
10 20
C
80
29
2-3 Tree
Tree23Node data structure:
public class Tree23Node {
private elementType leftItem;
private elementType rightItem;
private Tree23Node leftChild;
private Tree23Node middleChild;
private Tree234Node rightChild;
// constructors omitted // }
30
2-3 Tree
• 插入2-3 tree
– Case 1: 插入 70 到 Figure 1.
• 先尋找 70. 發現不在其中.
• 須知道找尋70時會遇到哪些
node. 是 node C with 80.
• node C 只有一element, 所以70
可插入此位置
A
40
B
C
10 20
70 80
Figure 2
31
• 插入 2-3 tree
2-3 Tree
– Case 2: 插入 30 到 Figure 2.
• 會遭遇到30的leaf node
為node B
• 既然 B 為 3-node, 須產生新
node D.
• D 將含B 中最大element
B
• 最小的element 會在 B.
• 中間的element會在B的parent 10
A
A
20 40
D
30
Figure 3
C
70 80
32
2-3 Tree
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
90 95
(b) 70 deleted
33
2-3 Tree
A
50 80
B
10 20
D
C
60
95
(c) 90 deleted
A
20 80
B
10
D
C
50
95
(d) 60 deleted
34
2-3 Tree
A
20
B
10
C
50 80
(e) 95 deleted
A
20
B
10
C
A
20 80
80
35
(g) 10 deleted
(f) 50 deleted
2-3-4 Tree
36
2-3-4 Tree
• 2-3-4 tree 為search tree 為空或:
每個 internal node 為2, 3,或 4 node .
2-3-4 tree 類似一個 2-3 tree, 但它允許:
4-node:四個 children 和三個 elements
50 60 70
37
2-3-4 Tree
2-3-4 tree node data structure:
public class Tree234Node {
private elementType smallItem;
private elementType middleItem;
private elementType largeItem;
private Tree234Node leftchild;
private Tree234Node lmidchild;
private Tree234Node rmidchild;
private Tree234Node rightchild;
// constructors omitted // }
38
2-3-4 Tree Insertion
•
Insertion to a 2-3-4 tree
–
There are essentially 3 cases to consider 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
39
2-3-4 Tree Insertion
• Case 1:
t
t (root)
y
x
y
z
x
a
b
c
z
d
a
b
c
d
Figure1 when the root is a 4-node
40
2-3-4 Tree Insertion
• Case 2:
z
w
a
b
x
y
c
x
e
d
z
w
a
b
e
y
c
d
Figure 2 when the child of a 2-node is a 4-node
41
2-3-4 Tree Deletion
•
We are now at node p and will move to node q NEXT.
Consider the following 4 CASES:
(1) p is a leaf: delete the element in p OR it is NOT in the tree.
(2) q is not a 2-node : In this case, the search moves to q
(3)q is a 2-node and its nearest sibling r is also a 2-node.
(3.1) if p is a 2-node, merge p, q, r to a 4-node.
(3.2) if p is a 3 or 4-node, merge q,r
(4) q is a 2-node and its nearest sibling r is NOT a 2-node : see fig. in case 4.
42
2-3-4 Tree Deletion
• Case 1: v 在 leaf 直接刪除之
a
p
v
b
刪除v
f
r
w
a
x
y
p
w
b
r
f
x
y
43
2-3-4 Tree Deletion
• Case 2: q是3-node,p移到q時找到要刪除的v,則刪除之
p
w
q
z
w
r
s
v
p
f
x
z
s
y
f
r
x
y
刪除v
a
t
g
c
d
e
a
t
g
c
d
e
44
2-3-4 Tree Deletion
• Case 3.1:
merge 三個 2-node
p
p
w
q
v
a b
v
w
x
r
x
c
a
b
c
d
d
45
2-3-4 Tree Deletion
Case 3.2: w 下移到q merge q,r 便可刪除v
p
w
q
p
z
f
r
v
a
z
q
x
b
c
v
d
a
w
b
x
d
f
e
46
2-3-4 Tree Deletion
Case 4: w下移到q,x上移到p 若找到欲刪除的v,便可刪之
p
q
w
z
x
f
r
v
a
p
x
b
c
q
y
d
v
e
a
z
r
w
b c
f
y
d
e
47
2-3-4 Tree to Red-Black Tree
2-3-4 tree 可轉換成特殊的
binary search tree
稱為 red-black tree
可節省儲存空間
48
Red-Black Tree
49
Red-Black Tree
red-black tree 兩特性:
Path rule:
• 各path 從root 到leaf (或有一
子的node)有同數目的 black
nodes.
Red Rule:
• red node的孩子不可為red
node
A basic red-black tree
50
Red-Black Tree
S
L
S L
OR

c
a
L
S
a b
c
a
b
b
c
S for Small; L for Large.
Figure 1 Transforming a 3-node into two red_black nodes
51
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
52
範例:
將下圖的 Red-Black Tree 轉成 2-3-4 Tree 再依序
(1)刪除60
(2)加入8
畫出 2-3-4 Tree ,再轉成 Red-Black Tree 。
50
10
7
5
70
40
9
30
60
80
75
90
85
92
53
練習:
將下圖的 Red-Black Tree 轉成 2-3-4 Tree 再依序
(1)刪除60
(2)加入8
畫出 2-3-4 Tree ,再轉成 Red-Black Tree 。
50
10
7
5
70
40
9
30
60
80
75
90
85
Figure 3 Red-black
92
54
2-3-4 Tree
50
10
5
7
9
30 40
70 80
60
75
85 90 92
Figure 4 2-3-4 tree
55
刪除 60
70
56
加入 8
7
8
57
轉成 Red-Black Tree
50
7
80
5
10
70
30
8
9
90
75
85
92
40
58
Download