More Trees Outline • • • • • Tree B-Tree 2-3 Tree 2-3-4 Tree Red-Black Tree Tree • 定義: 從 root 開始存取的一種 data structure. 每個 node 為leaf 或 internal node . • internal node 有一個或多個 children nodes, 他為child node的 parent • 相同internal node 的 children nodes 互稱 siblings. Tree Root Node Parent Node Child Node leaf Internal node Node Node leaf Node B-Tree in memory of R. Bayer Degree 為 d 的B tree 每個node 包含至多 d 個 pointers 或 d-1 elements 每個 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 full. 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 B+-Tree insert element 時, 考慮下面 3個 cases: Leaf Page Full Index Page Full No No Yes No Yes Yes B+-Tree • Case 1: leaf 未滿 – 把element 放到適當且 sorted 好的leaf 位置 B+-Tree Case 2: leaf 滿, 但 index 沒滿 1. 2. 3. 4. . 把 leaf 分裂(split) 把middle key 放在排序好的 index left leaf 放比middle key小的 elements right leaf 放比middle key大的 elements B+-Tree • 插入element 其key 為70 到 B+ tree. • 此 element 將加到含有50, 55, 60, and 65 的leaf. • 不幸地, 此 leaf已滿. 這意味我們必須分裂( split ) leaf 如下: Left Leaf Page Right Leaf Page 50 55 60 65 70 B+-Tree • middle key 60 放在50和75之間的 index • 下面表示加入70後的B+ tree B+-Tree Case 3: leaf 和 index 都滿了 1. 2. 3. 4. 5. 6. 7. 分裂 leaf. < middle key的 element 放left leaf. >= middle key的record 放 right leaf. 分裂 index. keys < middle key 放 left index. keys > middle key 放 right index. middle key 放上一層(higher level) index. 如果上一層 index 也滿了, 持續分裂( split) index (steps 4 - 7). B+ Tree • 加入element 其 key 為 95. • 此 element 將加入含75, 80, 85, and 90 的leaf. 此leaf 已滿 ,所以分裂 leaf: Left Leaf Page Right Leaf Page 75 80 85 90 95 • middle key 85, 提升到 index. 不幸地, index 也滿, 所以 分裂 index: Left Index Page Right Index Page Higher level Index 25 50 75 85 60 B+ Tree • 下圖表示加入key 為 95 的 element 的結果 B+ Tree • Deleting element 考慮3個 cases: Leaf Page Below Fill Factor Index Page Below Fill Factor Case 1 No No Case 2 Yes No Yes Yes Case 3 B+ Tree Case 1: delete 後, leaf 和 index 符合B+tree – 從 leaf delete 此 element – 把keys 排成升冪排序. – 如被刪除的key 在 index, 用下個 key 取代它 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. B+ Tree Case 2: delete 後, leaf 不夠滿 – – 結合 leaf 和它的 sibling leaf. 改 index 來反應改變 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. 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. B+ Tree • Delete the element with key 60 1. 2. 3. The leaf containing 60 (60 65) will be below the fill factor after the deletion. Thus, we must merge leaves. With merged leaves, the index will be reduced by one key. Hence, it will fall below the fill factor. Thus, we must merge indexes. 60 is now the only key in root index page. Obviously, it will be removed. 2-3 Tree 2-3 Tree 為search tree 可為空或符合: – 每個 internal node 為 2-node 或 3-node 2-node 有一element 而 3-node 有二 elements – 所有external nodes 都在相同 level. 40 2-node 3-node A B 10 20 C internal node 80 external nodes 2-3 Tree 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 • 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 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 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 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 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 有一element, 3-node 有二elements, 4-node 有三elements – 所有external nodes 都在相同 level. 2-3-4 tree 類似2-3 tree, 但它有 4-nodes (即 四個 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 2-3-4 Tree • 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 • 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 also a 2node. • 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 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 is 3-node f y d e 2-3-4 Tree 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 is 3-node x d e 2-3-4 Tree 2-3-4 tree 可轉成binary search tree 稱為 red-black tree 可節省儲存空間 Red-Black Tree 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 a L S a b c a b 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 Red-black Tree 92 2-3-4 Tree 50 10 5 7 9 30 40 70 80 60 2-3-4 tree 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