Presentation is loading. Please wait.

Presentation is loading. Please wait.

B-Tree. p2. B-Tree : 性質: 典型之 B-Tree 運用情況: 在 B-Tree 中若 node x 有 x.n keys, 則 x 含有 x.n+1 個 children. M D H J K L F G B C Q T X Y Z V W R S N P Main Memory.

Similar presentations


Presentation on theme: "B-Tree. p2. B-Tree : 性質: 典型之 B-Tree 運用情況: 在 B-Tree 中若 node x 有 x.n keys, 則 x 含有 x.n+1 個 children. M D H J K L F G B C Q T X Y Z V W R S N P Main Memory."— Presentation transcript:

1 B-Tree

2 p2. B-Tree : 性質: 典型之 B-Tree 運用情況: 在 B-Tree 中若 node x 有 x.n keys, 則 x 含有 x.n+1 個 children. M D H J K L F G B C Q T X Y Z V W R S N P Main Memory Secondary Memory ( disks ) ( RAM ) 資料量龐大無法全部存在 main memory. 處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory.

3 p3. B-Tree T 之定義: T : rooted tree 並滿足下列: 1. 對每一 node x 含下列之 fields a. x.n : node x 所含之 key 數. b. 依遞增排列. c. x.leaf 為 true 若 x 為 leaf; false 若 x 不為 leaf. 2. 若 x 為一 internal node 則 x 有 x.n+1 個 pointers x.C 1, x.C 2,…, x.C x.n+1 指向某 children. 若 x 為一 leaf. 其 x.C i 沒有定義. 3. 若 x.key i 為 x.C i 所指之 subtree 中任一 key 則 key1 key2 K3K3 K2K2 K1K1 x.C 1 x.C 2 x.C 3 x

4 p4. 4. 每一 leaf 之高度相同. 5. t : minimum degree of the B-tree. a. node( 除 root ) 有 internal node ( 除 root ) 有 若 則 root 至少有一 key. b. node( 至多 ) 有 internal node ( 除 root ) 有 a node is FULL, if it contains exactly 2t-1 keys.

5 Thm : If, then for any n-key B-tree T of height h and minimum degree Proof : 1 t-1 t t t ttt 1 2 2t2t 2t22t2

6 p6. convention : 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 : 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.

7 p7. B-Tree-Search(x,k) : Algorithm : Total CPU time : B-Tree-Search(x,k) { }

8 p8. B-Tree-Create(T) : Algorithm : time : B-Tree-Create(T) { }

9 p9. B-Tree-Split-Child(x,i) : Splitting a node in a B-Tree : … N W W … x Key i-1 [x] y=C i [x] P Q R S T U V T1T1 T2T2 T4T4 T3T3 T5T5 T6T6 T8T8 T7T7 Key i [x] t=4 full … N S W … y=C i [x] T U V T5T5 T6T6 T8T8 T7T7 P Q R T1T1 T2T2 T4T4 T3T3 z=C i+1 [x] Splitting a full node y ( have 2t-1 keys ) around its median key y.key t into 2 nodes having (t-1) keys each.

10 p10. Algorithm : B-Tree-Split-Child(x, i) 1. z=Allocate-Node() 2. y=x.C i 3. z.leaf = y.leaf 4. z.n = t-1 5. for j=1 to t-1 z.key j = y.key j+t 6. if not y.leaf 7. for j= 1 to t z.C j =y.C j+t 8. y.n = t-1 9. for j = x.n +1 downto i+1 x.C j+1 =x.C j 10. x.C j+1 = z 11. for j = x.n down to i x.key j+1 = x.key j 12. x.key i =y.key t 13. x.n=x.n+1 14. DISK-WRITE(y); DISK-WRITE(z); DISK-WRITE(x);

11 p11. B-Tree-Insert(T,k) : Insert a key in a B-Tree : Root[T] r A D F H L N P T1T1 T2T2 T4T4 T3T3 T5T5 T6T6 T8T8 T7T7 t=4 H r L N P T5T5 T6T6 T8T8 T7T7 A D F T1T1 T2T2 T4T4 T3T3 Root[T] S

12 p12. B-Tree-Insert(T, k) 1. r=T.root 2. if r.n == 2t-1 3. s=Allocate-Node() 4. T.root = s 5. s.leaf = FLASE 6. s.n = 0 7. s.c 1 = r 8. B-Tree-Split-Child(s, 1) 9. B-Tree-Insert-Nonfull(s. k) 10. else B-Tree-Insert-Nonfull(r. k)

13 B-Tree-Insert-Nonfull(x, k) 1.i = x.n 2.if x.leaf 3. While i >=1 and k < x.key i 4. x.key i+1 =x.key i 5. i=i-1 6. x.key i+1 = k 7. x.n = x.n + 1 8. DISK-Write(x) 9.else while i >=1 and k < x.key i 10. i = i-1 11. i = i+1 12. DISKRead(x.C i ) 13. if x.c i.n == 2t-1 14. B-Tree-Split-Child(x, i) 15. if k > x.key i 16. i = i+1 17. B-Tree-Insert-Nonfull(x.C i, k)

14 Example : Inserting keys into a B-Tree. A C D E t=3 (a) Initial tree J K N O R S T U V Y Z G M P X A B C D E J K N O R S T U V Y Z G M P X (b) B inserted A B C D E J K N O Q R S Y Z G M P T X (c) Q inserted U V R S U V G M P T X

15 A B C D E J K L N O Q R S Y Z (d) L insert U V P G M T X A B J K L N O Q R S Y Z (e) F insert U V P C G M T X D E F

16 Deleting a key from a B-Tree : 1. K is in x and x is a leaf : 2. K is in x and x is an internal node : a. b. c. if both y,z has t-1 keys. Merge y,z and k into y. Recursively delete k from y. ( x has t keys ) K x delete k from x. K x k ’ y Recursively delete k’ and replace k by k’ in x. K x k ’ z K z x t-1 y K 2t-1

17 3. If K is not in internal node x : a. If C i [x] has only t-1 keys but has a sibling with t keys b. If C i [x] and all of C i [x] ’ s siblings have t-1 keys, merge c i with one sibling. x C i [x] k is in this subtree. t-1 x C i [x] t Move a key from x down to C i [x]. Move a key from C i [x]’s sibling to x. Move an appropriate child to C i [x] from its sibling. 0 x t-1 keys 0 2t-1 keys C i [x] t-1 keys

18 Example : Deleting a key from a B-Tree. t=3 (a) Initial tree (b) F delete : case 1 A B J K L N O Q R S Y Z U V P C G M T X D E F A B J K L N O Q R S Y Z U V P C G M T X D E (c) M delete : case 2a A B J K N O Q R S Y Z U V P C G L T X DEDE

19 (d) G deleted : case 2c A B N O Q R S Y Z U V P C L T X D E J K (e) D deleted : case 3b C L P T X A B N O Q R S Y Z U V E J K (e’) tree shrinks in height C L P T X A B N O Q R S Y Z U V E J K (f) B delete : case 3a E L P T X A C N O Q R S Y Z U V J K


Download ppt "B-Tree. p2. B-Tree : 性質: 典型之 B-Tree 運用情況: 在 B-Tree 中若 node x 有 x.n keys, 則 x 含有 x.n+1 個 children. M D H J K L F G B C Q T X Y Z V W R S N P Main Memory."

Similar presentations


Ads by Google