more trees
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/1.jpg)
2-3-4
Tree
and how it relates to Red Black Tree
![Page 2: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/2.jpg)
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
RBT insert Example Same EX. with 2-3-4 tree
![Page 3: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/3.jpg)
B Tree
![Page 4: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/4.jpg)
B-Tree is in memory of R. BayerIt is a generalization of binary search tree in that a node (or, an entry) 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)
![Page 5: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/5.jpg)
B-Tree of Degree 3
20 28
10 25 30
![Page 6: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/6.jpg)
B* Tree
B-tree 的 node 至少 2/3 滿
![Page 7: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/7.jpg)
B* Tree of Degree 4
20 28
15 26 30
6
10 232 4 35
![Page 8: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/8.jpg)
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%).
![Page 9: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/9.jpg)
B+ Tree index page
data page
Number of Keys 4
Number of Pointers 5
Fill Factor 50%
Minimum Keys in each page 2
This B+ tree:
![Page 10: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/10.jpg)
2-3 Tree
![Page 11: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/11.jpg)
2-3 Tree
為 search tree 可為空或 :
每個 internal node 為 2-node ( 有 2 child pointers)
或 3-node ( 有 3 child pointers)
40
10 20 80
2-node
B 3-node C
A
![Page 12: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/12.jpg)
2-3 Tree Insertion insert Case 1: 插入 70
• 先尋找 70. 發現不在其中 .• 須知尋找 70 時 遇到哪 node?
是 含 80 的 node C• node C 只有一個 element,
所以 70 可放 C
40
10 20 70 80
C
A
B
![Page 13: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/13.jpg)
2-3 Tree
insert Case 2: 插入 30
• 會遇到 30 的是 node B• B 為 3-node, 須產生新 node D.• B 含 elements 10, 20, 30• 其中最大 element 30 放 D • 最小 element 10 放 B.• 中間 20 放 B 的 parent A
這叫 Split ( 分裂 ):1. 產生新 node D. 2. 中間 20 推升上層
80
C
A
20 40
10 70
B
Figure 3
30
D
![Page 14: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/14.jpg)
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
![Page 15: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/15.jpg)
2-3 Tree Insertion (Cont.)
40
G
70
F
80
E
60
C
20
A
10
B
30
D
Figure 4 Insertion of 60 into the 2-3 tree of Figure 3
![Page 16: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/16.jpg)
2-3 Tree Deletion
50 80
A
10 20
B60 70
C90 95
D
50 80
A
10 20
B60
C90 95
D
(a) Initial 2-3 tree
(b) 70 deleted
![Page 17: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/17.jpg)
2-3 Tree Deletion (Cont.)
50 80
A
10 20
B60
C95
D
(c) 90 deleted
20 80
A
10
B50
C95
D
(d) 60 deleted Next, delete 95
![Page 18: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/18.jpg)
2-3 Tree Deletion (Cont.)
20
A
10
B50 80
C
(e) 95 deleted
20
10
B80
C
(f) 50 deleted
A
20 80
(g) 10 deleted
A
這叫 Merge (融合 ):1.消去 node D2.上層 80 併入下層
![Page 19: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/19.jpg)
2-3-4 Tree
![Page 20: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/20.jpg)
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 如下圖
50 60 70
![Page 21: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/21.jpg)
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)
![Page 22: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/22.jpg)
2-3-4 Tree Insertion
• Case 1: It is the root.
x y z
a b c d
t (root)
y
x z
a b c d
t
Figure1 when the root is a 4-node
![Page 23: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/23.jpg)
2-3-4 Tree Insertion
• Case 2: Its parent is a 2-node
w x y
a b c d
z
e w
a b c d
x z
ey
Figure 2 when the child of a 2-node is a 4-node
![Page 24: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/24.jpg)
2-3-4 Tree
2-3-4 tree 轉成 binary search tree
則稱為 red-black tree
red-black tree 比 2-3-4 tree 節省空間 因為 2-3-4 node 會浪費不少未存資料的空的空間
![Page 25: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/25.jpg)
Red-Black Tree(RBT)
![Page 26: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/26.jpg)
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
![Page 27: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/27.jpg)
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.
![Page 28: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/28.jpg)
Red-Black Tree
c
S L
L
S
S
L
a b
ca
b
OR
a b c
Figure 1 Transforming a 3-node into two red-black nodes
S for Small L for Large
Left-leaning
Right-leaning
![Page 29: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/29.jpg)
Red-Black Tree
Figure 2 Transforming a 4-node into two red-black nodes
S M LM
S La b c d
b c d
S for Small M, Middle L, Large
a
![Page 30: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/30.jpg)
50
10 70
80
5
7
9 30
40 60
75 90
85 92
1. 將下圖的 Red-Black Tree 轉成 2-3-4 Tree
2. 依序 (1)刪除 60 (2)加入 8
3. 再轉回 Red-Black Tree
![Page 31: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/31.jpg)
上圖轉成的 2-3-4 Tree
50
10 70 80
5 7 9 30 40 60 75 85 90 92
![Page 32: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/32.jpg)
刪除 60
70
wasted space
![Page 33: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/33.jpg)
加入 8
8
7
![Page 34: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/34.jpg)
轉回 Red-Black Tree 50
7
10 90705
80
9
8 75 85 9230
40
![Page 35: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/35.jpg)
Red Black Tree Saves Space
In the example above, the 2-3-4 tree wastes 8 unused space of elements.
The corresponding red-black tree
cuts this waste!
![Page 36: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/36.jpg)
Left-Leaning Red-Black Tree
![Page 37: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/37.jpg)
LLRBT is easier to implement than RBT, especially the deletionIt requires
3-nodes are left-leaning, thus maintains 1-1 correspondence with 2-3-4 trees (see next page).
![Page 38: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/38.jpg)
LL Red-Black Tree
S L
L
S
a b
c
a b c
Transforming a 3-node into LL red-black nodes
S for Small L for Large
Left-Leaning (LL)
![Page 39: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/39.jpg)
Double Red
&
Double Black
![Page 40: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/40.jpg)
During Red-Black Tree insertion, abnormal Double Red may occur as shown next.
![Page 41: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/41.jpg)
41
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
31
4
2
31
![Page 42: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/42.jpg)
Double Red in 2-3-4 Tree
1 2 3已滿 , 此時 insert
4
1 2 3 4這 node 爆掉了 , 故要調整
之對應的 Red-Black Tree:
2
1 3
4
此時 叫 Double Red 雙紅 , 表示原來 node 爆掉了
3 4
![Page 43: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/43.jpg)
During Red-Black Tree deletion, again, abnormal Double Black may occur as shown next.
![Page 44: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/44.jpg)
Double Black in 2-3-4 Tree7 8
5
3
此時 Delete 5
7 8
3
對應的 Red-Black Tree:
7
3
Double BLACK 雙黑線 , 表示其中有個空 2-3-4 node.故要調整之
8
![Page 45: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/45.jpg)
HW12.7 RBT insert 30
30 30
![Page 46: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/46.jpg)
RBT insert 40
40
30
![Page 47: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/47.jpg)
RBT insert 20
40
30
20
![Page 48: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/48.jpg)
RBT insert 90
40
30
20
90
40
30
20
90
40
30
20
90
![Page 49: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/49.jpg)
RBT insert 10
40
30
20
9010
![Page 50: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/50.jpg)
RBT insert 50
40
30
20
10 90
50
40
30
20
10 50
90
50
30
20
10 9040
![Page 51: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/51.jpg)
RBT insert 70
50
30
20
10 9040
70
50
30
20
10 9040
70
![Page 52: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/52.jpg)
RBT insert 60
50
30
20
10 7040
60
50
30
20
10 9040
70
60
90
![Page 53: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/53.jpg)
RBT insert 80
50
30
20
10 7040
60 90
80
50
30
20
10 7040
60 90
80
70
50
9060
80
30
4020
10
![Page 54: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/54.jpg)
2-3-4 tree insert 30
30
![Page 55: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/55.jpg)
2-3-4 tree insert 40
30 40
![Page 56: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/56.jpg)
2-3-4 tree insert 20
20 30 40
![Page 57: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/57.jpg)
2-3-4 tree insert 90
30
20 40 90
![Page 58: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/58.jpg)
2-3-4 tree insert 10
30
10 20 40 90
![Page 59: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/59.jpg)
2-3-4 tree insert 50
30
10 20 40 50 90
![Page 60: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/60.jpg)
2-3-4 tree insert 70
30 50
10 20 70 9040
![Page 61: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/61.jpg)
2-3-4 tree insert 60
30 50
10 20 60 70 9040
![Page 62: More Trees](https://reader035.vdocuments.us/reader035/viewer/2022070404/56813aee550346895da35f10/html5/thumbnails/62.jpg)
2-3-4 tree insert 80
30 50 70
10 20 6040 80 90
浪費空間 = 6 elements / 15 elements = 40 %