530 likes | 548 Views
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.
E N D
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 Internal node Node Parent Node Node Node Node leaf leaf Child
B-Treein memory of R. BayerDegree 為 d的B tree 每個node 包含至多 d 個 pointers 或 d-1 elements每個 node 至少 1/2 滿 (即至少[ (d-1)/2] elements)
20 28 10 25 30 B-Tree of Degree 3
B* Tree B-tree 的 node 至少 2/3 滿
20 28 6 15 26 30 10 23 2 4 35 B* Treeof Degree 4
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.
index page data page B+ Tree This B+ tree:
B+-Tree insertelement 時, 考慮下面 3個 cases:
B+-Tree • Case 1:leaf 未滿 • 把element 放到適當且 sorted 好的leaf 位置
B+-Tree Case 2: leaf 滿, 但 index 沒滿 • 把 leaf 分裂(split) • 把middle key 放在排序好的 index • left leaf 放比middle key小的 elements 4. right leaf 放比middle key大的 elements .
B+-Tree • 插入element 其key 為70 到 B+ tree. • 此 element 將加到含有50, 55, 60, and 65 的leaf. • 不幸地, 此 leaf已滿. 這意味我們必須分裂( split ) leaf 如下:
B+-Tree • middle key 60 放在50和75之間的 index • 下面表示加入70後的B+ tree
B+-Tree Case 3: leaf 和 index 都滿了 • 分裂 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: • middle key85, 提升到 index. 不幸地, index 也滿, 所以分裂 index:
B+ Tree • 下圖表示加入key 為 95 的 element 的結果
B+ Tree • Deleting element 考慮3個 cases: Case 1 Case 2 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 都不夠滿 • 合併(merge)leaf 和它的 sibling leaf. • 調整 index page 來反應其改變 • 合併 index page 和它的 sibling index. • 持續合併 index 直到符合B+ tree, 或到達 root.
B+ Tree • Delete the element with key 60 • 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 為search tree 可為空或符合: • 每個 internal node 為 2-node 或 3-node 2-node 有一element而 3-node 有二 elements • 所有external nodes 都在相同 level. A 2-node 40 B C 3-node internal node 10 20 80 external nodes
40 10 20 70 80 2-3 Tree insertCase 1:插入 70 • 先尋找 70. 發現不在其中. • 須知尋找70時 遇到哪node. 是 含 80 的 node C • node C 只有一個 element, 所以70 可放 C A B C
A 20 40 B D C 10 30 70 80 Figure 3 2-3 Tree insertCase 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
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
G 40 A F 20 70 B D C E 10 30 80 60 2-3 Tree Figure4 Insertion of 60 into the 2-3 tree of Figure 3
2-3 Tree A 50 80 D C B 60 70 10 20 90 95 (a) Initial 2-3 tree A 50 80 D C B 60 10 20 90 95 (b) 70 deleted
2-3 Tree A 50 80 D C B 60 10 20 95 (c) 90 deleted A 20 80 D C B 50 10 95 (d) 60 deleted
2-3 Tree A 20 C B 50 80 10 (e) 95 deleted A 20 A C B 20 80 80 10 (g) 10 deleted (f) 50 deleted
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 z a b c d a b c d Figure1 when the root is a 4-node
2-3-4 Tree • Case 2: Its parent is a 2-node z x z e e w x y w y a b c d a b c d Figure 2 when the child of a 2-node is a 4-node
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.
2-3-4 Tree q is the left child of a 3-node p p p w z x z q f q r r f v x y v w y d e c c d e a b a b Figure1 when the nearest sibling is 3-node
2-3-4 Tree q is the left child of a 4-node p p v y z w y z g g q q f f r r u u v w x x d e e c c d a b a b Figure1 when the nearest sibling is 3-node
2-3-4 Tree 2-3-4 tree 可轉成binary search tree 稱為 red-black tree 可節省儲存空間
Red-Black Tree red-black tree 為 binarysearch tree: • 每個 node 不是red就是black • 每個leaf (NULL) 都為black • rednode 的兩個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 2log(n+1). Red-Black tree can always be searched in O(log n) time.
S L S L OR c a L S a c b a b b Red-Black Tree c S for Small; L for Large. Figure 1 Transforming a 3-node into two red_black nodes
Red-Black Tree M S M L S L c d a b a d b c S for Small;M, Middle; L, Large. Figure 2 Transforming a 4-node into two red_black nodes
50 10 70 7 40 60 80 75 90 5 9 30 85 92 將下圖的 Red-Black Tree 轉成 2-3-4 Tree 依序 (1)刪除60 (2)加入8再轉回 Red-Black Tree Red-blackTree
50 10 70 5 30 60 75 85 80 7 40 90 9 92 2-3-4 Tree 2-3-4 tree