Download presentation
Presentation is loading. Please wait.
Published byWinifred Terry Modified over 8 years ago
1
More Trees
2
Outline Tree B-Tree 2-3 Tree 2-3-4 Tree Red-Black Tree
3
Tree 定義 : 從 root 開始存取的一種 data structure. 每個 node 為 leaf 或 internal node. internal node 有一個或多個 children nodes, 他為 child node 的 parent 相同 internal node 的 children nodes 互稱 siblings.
4
Tree Node Root Node leaf Internal node Parent Child
5
B-Tree in memory of R. Bayer Degree 為 d 的 B tree 每個 node 包含至多 d 個 pointers 或 d-1 elements 每個 node 至少 1/2 滿 ( 即至少 [ (d-1)/2] elements)
6
B-Tree of Degree 3 2028102530
7
B* Tree B-tree 的 node 至少 2/3 滿
8
B* Tree of Degree 4 2028152630610232435
9
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 full.
10
B+ Tree index page data page Number of Keys4 Number of Pointers5 Fill Factor50% Minimum Keys in each page2 This B+ tree:
11
B+-Tree insert element 時, 考慮下面 3 個 cases: Leaf Page FullIndex Page Full No YesNo Yes
12
B+-Tree Case 1: leaf 未滿 – 把 element 放到適當且 sorted 好的 leaf 位置
13
B+-Tree Case 2: leaf 滿, 但 index 沒滿 1. 把 leaf 分裂 (split) 2. 把 middle key 放在排序好的 index 3.left leaf 放比 middle key 小的 elements 4. right leaf 放比 middle key 大的 elements.
14
B+-Tree 插入 element 其 key 為 70 到 B+ tree. 此 element 將加到含有 50, 55, 60, and 65 的 leaf. 不幸地, 此 leaf 已滿. 這意味我們必須分裂 ( split ) leaf 如下 : Left Leaf PageRight Leaf Page 50 5560 65 70
15
B+-Tree middle key 60 放在 50 和 75 之間的 index 下面表示加入 70 後的 B+ tree
16
B+-Tree Case 3: leaf 和 index 都滿了 1. 分裂 leaf. 2.< middle key 的 element 放 left leaf. 3.>= middle key 的 record 放 right leaf. 4. 分裂 index. 5.keys < middle key 放 left index. 6.keys > middle key 放 right index. 7.middle key 放上一層 (higher level) index. 如果上一層 index 也滿了, 持續分裂 ( split) index (steps 4 - 7).
17
B+ Tree 加入 element 其 key 為 95. 此 element 將加入含 75, 80, 85, and 90 的 leaf. 此 leaf 已滿, 所以分裂 leaf: middle key 85, 提升到 index. 不幸地, index 也滿, 所以 分裂 index: Left Leaf PageRight Leaf Page 75 8085 90 95 Left Index PageRight Index PageHigher level Index 25 5075 8560
18
B+ Tree 下圖表示加入 key 為 95 的 element 的結果
19
B+ Tree Deleting element 考慮 3 個 cases: Leaf Page Below Fill Factor Index Page Below Fill Factor No YesNo Yes Case 1 Case 2 Case 3
20
B+ Tree Case 1: delete 後, leaf 和 index 符合 B+tree – 從 leaf delete 此 element – 把 keys 排成升冪排序. – 如被刪除的 key 在 index, 用下個 key 取代它
21
B+ Tree Delete element with key 70 –This element is in a leaf containing 60, 65 and 70. This leaf will contain 2 elements after the deletion. Since our fill factor is 50% or (2 elements), we simply delete 70 from the leaf.
22
B+ Tree Case 2: delete 後, leaf 不夠滿 – 結合 leaf 和它的 sibling leaf. – 改 index 來反應改變
23
B+ Tree Delete the element with key 25 –This element is found in the leaf containing 25, 28, and 30. The fill factor will be 50% after the deletion; however, 25 appears in the index. Thus, when we delete 25, we must replace it with 28 in the index.
24
B+ Tree Case 3: delete 後, index 和 leaf 都不夠滿 1. 合併 (merge) leaf 和它的 sibling leaf. 2. 調整 index page 來反應其改變 3. 合併 index page 和它的 sibling index. 4. 持續合併 index 直到符合 B+ tree, 或到達 root.
25
B+ Tree Delete the element with key 60 1.The leaf containing 60 (60 65) will be below the fill factor after the deletion. Thus, we must merge leaves. 2.With merged leaves, the index will be reduced by one key. Hence, it will fall below the fill factor. Thus, we must merge indexes. 3.60 is now the only key in root index page. Obviously, it will be removed.
26
2-3 Tree
27
為 search tree 可為空或符合 : – 每個 internal node 為 2-node 或 3-node 2-node 有一 element 而 3-node 有二 elements – 所有 external nodes 都在相同 level. 40 102080 external nodes internal node 2-node B 3-node C A
28
2-3 Tree insert Case 1: 插入 70 先尋找 70. 發現不在其中. 須知尋找 70 時 遇到哪 node. 是 含 80 的 node C node C 只有一個 element, 所以 70 可放 C 40 10207080 C A B
29
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 80 C A 2040 1070 B Figure 3 30 D
30
2-3 Tree 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
31
2-3 Tree 40 G 70 F 80 E 60 C 20 A 10 B 30 D Figure4 Insertion of 60 into the 2-3 tree of Figure 3
32
2-3 Tree 5080 A 1020 B 6070 C 9095 D 5080 A 1020 B 60 C 9095 D (a) Initial 2-3 tree (b) 70 deleted
33
2-3 Tree 5080 A 1020 B 60 C 95 D (c) 90 deleted 2080 A 10 B 50 C 95 D (d) 60 deleted
34
2-3 Tree 20 A 10 B 5080 C (e) 95 deleted 20 10 B 80 C (f) 50 deleted A 2080 (g) 10 deleted A
35
2-3-4 Tree
36
為 search tree 為空或是滿足 : – 每個 internal node 為 2, 3, 或 4 node. 2-node 有一 element, 3-node 有二 elements, 4-node 有三 elements – 所有 external nodes 都在相同 level. 2-3-4 tree 類似 2-3 tree, 但它有 4-nodes ( 即 四個 pointers 和三個 elements) 506070
37
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
38
2-3-4 Tree Case 1: It is the root. xyz abcd t (root) y xz ab cd t Figure1 when the root is a 4-node
39
2-3-4 Tree Case 2: Its parent is a 2-node wxy abcd z e w abcd xz e y Figure 2 when the child of a 2-node is a 4-node
40
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 also 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.
41
2-3-4 Tree vw b de xz f y v b c d wz e xy p q a f r Figure1 when the nearest sibling is 3-node q p r a q is the left child of a 3-node p c
42
2-3-4 Tree u b d vyz e wx p q a f r Figure1 when the nearest sibling is 3-node q is the left child of a 4-node p c g uv b e wyz x q f r d g a c
43
2-3-4 Tree 2-3-4 tree 可轉成 binary search tree 稱為 red-black tree 可節省儲存空間
44
Red-Black Tree
45
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
46
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.
47
Red-Black Tree c S L L S S L ab c a b OR a b c Figure 1 Transforming a 3-node into two red_black nodes S for Small; L for Large.
48
Red-Black Tree Figure 2 Transforming a 4-node into two red_black nodes S M L M S L a b c d bc d S for Small;M, Middle; L, Large. a
49
Red-black Tree 50 1070 80 5 7 930 4060 7590 8592 將下圖的 Red-Black Tree 轉成 2-3-4 Tree 依序 (1) 刪除 60 (2) 加入 8 再轉回 Red-Black Tree
50
2-3-4 Tree 50 107080 57930406075859092 2-3-4 tree
51
刪除 60 70
52
加入 8 8 7
53
轉回 Red-Black Tree 50 7 1090705 80 9 8 75 85 92 30 40
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.