BTree Multiway search tree n n A tree
B-Tree
Multiway search tree n n A tree was defined as either an empty structure or a structure whose children are disjoint tree t 1, t 2, …tk. Each node of this kind of tree can have more than two children. This tree called a multiway tree of order m, or an m-way tree. A Multiway search tree of order m, or an m-way search tree, is a Multiway search tree in which n n n Each node has m children and m-1 keys The keys in each node are in ascending order The keys in the first i children are smaller the ith key The keys in the last m-i children are larger than the ith key M-way search tree m-way tree Binary search tree binary tree p 2.
Introduction of B-tree n n B-tree: proposed by Bayer and Mc. Creight 1972 A B-tree operates closely with secondary storage and can be tuned to reduce the impediments imposed by this storage One important property of B-trees is the size of each node which can be made as large as the size of the block. (the basic unit of I/O operations associated with a disk is a block) a B-tree of order t is a multiway search tree. p 3.
n B-Tree : n 性質: 在 B-Tree 中若 node x 有 n[x] keys, 則 x 含有 n[x]+1 個 children. M D B C Main Memory ( RAM ) n H F G QT X JKL N P R S V W Y Z Secondary Memory ( disks ) 典型之 B-Tree 運用情況: 資料量龐大無法全部存在 main memory. 處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory. p 4.
n B-Tree T 之定義: n T:rooted tree 並滿足下列: 1. 對每一 node x 含下列之 fields n a. n[x]:node x 所含之 key 數. n b. n c. leaf[x] 為 true 若 x 為 leaf 依遞增排列. false 若 x 不為 leaf. n 2. 若 x 為一 internal node 則 x 有 n[x]+1 個 pointers C 1[x] , C 2[x] , … , Cn[x]+1[x] 指向某 children. 若 x 為一 leaf. 其 Ci[x] 沒有定義. n 3. 若 ki 為 Ci[x] 所指之 subtree 中任一 key 則 x key 1 C 1[x] K 1 C 2[x] K 2 key 2 C 3[x] K 3 p 5.
n n 4. 每一 leaf 之高度相同. (all leaves are on the same level. ) 5. t:minimum degree of the B-tree. (a B-tree of order 2 t) n a. node( 除 root ) 有 internal node ( 除 root ) 有 若 n b. 則 root 至少有一 key. node( 至多 ) 有 internal node ( 除 root ) 有 a node is FULL, if it contains exactly 2 t-1 keys. p 6.
n Thm: If , then for any n-key B-tree T of height h and minimum degree Proof: 1 1 2 t-1 t t t-1 t-1 2 t t t 2 t 2
n n n A B-tree is not a binary tree because B-tree has many more than two children B-trees may be formulated to store a set of elements or a bag of elements. (a given elements can occur many times in a bag but only once in a set) A B-tree is balanced. Every leaf in a B-tree has the same depth 2 -3 -4 tree (discussed by Rudolf Bayer): a B-tree of order 4 (min degree=2) p 8.
The elements in a B-tree node n n n Rule 1: the root may have as few as one elements (or even no elements if it also has no children); every other node has at least minimum elements Rule 2: the maximum number of elements in a node is twice the value of minimum Rule 3: the elements of each B-tree node are stored in a partially filled array, sorted from the smallest elements (at index 0) to the largest elements (at the final used position of the array) Rule 4: the number of subtrees below a nonleaf node is always one more than the number of the elements in the node. Rule 5: for any leaf node: (1) an element at index i is greater than all the elements in subtree number i of the node, and (b) an element at index i is less than all the elements in subtree number i+1 of the node. p 9.
66 Subtree number 0 Each element in subtree number 0 is less than 66 88 Subtree number 1 Each element in subtree number 1 is between 66 and 88. Subtree number 2 Each element in subtree number 2 is greater than 88 p 10.
n convention: n n n Root of the B-tree is always in main memory. Any nodes that are passed as parameters must already have had a DISK_READ operation performed on them. Operations: n n n Searching a B-Tree. Creating an empty B-tree. Splitting a node in a B-tree. Inserting a key into a B-tree. Deleting a key from a B-tree. p 11.
n B-Tree-Search(x, k): n Algorithm: B-Tree-Search(x, k) { } n Total CPU time: p 12.
n B-Tree-Created(T): n Algorithm: B-Tree-Create(T) { } n time: p 13.
n B-Tree-Split-Child(x, i, y): n Splitting a node in a B-Tree: Keyi-1[x] t=4 Keyi [x] x …N W … y=Ci[x] P Q R S T U V full T 1 T 2 T 3 T 4 T 5 T 6 T 7 T 8 …N y=Ci[x] P Q R T 1 T 2 T 3 T 4 S W… z=Ci+1[x] T U V T 5 T 6 T 7 T 8 Splitting a full node y ( have 2 t-1 keys ) around its median keyt[y] into 2 nodes having (t-1) keys each. p 14.
n Algorithm: B-Tree-Split-Child(x, I, y) { } p 15.
n B-Tree-Insert(T, k): n Insert a key in a B-Tree: Root[T] t=4 S Root[T] H r A D F H L N P r A D F L N P T 1 T 2 T 3 T 4 T 5 T 6 T 7 T 8 p 16.
n Algorithm: B-Tree-Insert(T, k) { { } B-Tree-Split-Child(S, l, r) B-Tree-Insert-Nonfull(S, k) } p 17.
n B-Tree-Insert-Nonfull(x, k): n Algorithm: B-Tree-Insert-Nonfull(x, k) { { { } B-Tree-Insert-Nonfull(Ci[x], k) }
n Example:Inserting keys into a B-Tree. t=3 (a) Initial tree G M P X A C D E J K N O R S T U V Y Z (b) B inserted G M P X A B C D E J K N O R S T U V Y Z G M P R S (c) Q inserted G M P A B C D E J K N O T X Q R S U V Y Z T X U V
(d) L insert P G M A B C D E J K L T X N O Q R S U V Y Z (e) F insert P C G M A B D E F J K L T X N O Q R S U V Y Z
n Deleting a key from a B-Tree: ( x has t keys ) 1. K is in x and x is a leaf: x K delete k from x. 2. K is in x and x is an internal node: x a. K Recursively delete k’ and replace k by k’ in x. y k’ x b. K z k’ c. if both y, z has t-1 keys. Merge y, z and k into y. Recursively delete k from y. t+1 x K z y t+1 2 t-1 K
3. If K is not in internal node x: x K Ci[x] k is in this subtree. a. If Ci[x] has only t-1 keys but has a sibling with t keys x • Move a key from x down to Ci[x]. K Ci[x] t-1 t • Move a key from Ci[x]’s sibling to x. • Move an appropiate child to Ci[x] from its sibling. b. If Ci[x] and all of Ci[x]’s siblings have t-1 keys, merge ci with one sibling. x 0 t-1 keys Ci[x] t-1 keys 0 2 t-1 keys
n Example:Deleting a key from a B-Tree. t=3 (a) Initial tree P C G M A B J K L D E F T X N O (b) F delete:case 1 Q R S Y Z P C G M A B U V J K L D E T X N O Q R S U V Y Z (c) M delete:case 2 a P C G L A B D E J K T X N O Q R S U V Y Z
(d) G deleted:case 2 c P C L A B T X N O D E J H Q R S U V Y Z (e) D deleted:case 3 b C L P T A B E J K N O X Q R S U V Y Z (e’) tree shrinks in height C L P T A B E J K N O X Q R S U V Y Z (f) B delete:case 3 a E L P T A C J K N O X Q R S
- Slides: 24