1 / 19

B-Tree

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

aolani
Download Presentation

B-Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B-Tree

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

  3. B-Tree T 之定義: T:rooted tree 並滿足下列: 1. 對每一 node x 含下列之 fields • x.n:node x 所含之 key 數. • 依遞增排列. • x.leaf 為 true 若 x 為 leaf; false 若 x 不為 leaf. 2. 若 x 為一 internal node 則 x 有 x.n+1 個 pointers x.C1 , x.C2 ,… , x.Cx.n+1指向某 children. 若 x 為一 leaf. 其 x.Ci沒有定義. 3.若 x.keyi為 x.Ci所指之 subtree 中任一 key 則 x key1 key2 x.C3 x.C1 x.C2 K2 K3 K1

  4. 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. If , then for any n-key B-tree T of height h and minimum degree • Thm: Proof: 1 1 2 t-1 t-1 t t 2t t-1 t-1 t-1 t-1 t t t t 2t2

  6. 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. B-Tree-Search(x,k): • Algorithm: • Total CPU time: B-Tree-Search(x,k) { }

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

  9. P Q R T U V T3 T4 T1 T2 T7 T8 T5 T6 • B-Tree-Split-Child(x,i): • Splitting a node in a B-Tree: Keyi-1[x] Keyi [x] x t=4 … N S W … … N W W … y=Ci[x] z=Ci+1[x] y=Ci[x] P Q R S T U V full T3 T4 T7 T8 T1 T2 T5 T6 Splitting a full node y ( have 2t-1 keys ) around its median key y.keyt into 2 nodes having (t-1) keys each.

  10. Algorithm: B-Tree-Split-Child(x, i) • z=Allocate-Node() • y=x.Ci • z.leaf = y.leaf • z.n = t-1 • for j=1 to t-1 z.keyj= y.keyj+t • if not y.leaf • for j= 1 to t z.Cj=y.Cj+t • y.n = t-1 • for j = x.n +1 downto i+1 x.Cj+1=x.Cj • x.Cj+1 = z • for j = x.n down to i x.keyj+1= x.keyj • x.keyi=y.keyt • x.n=x.n+1 • DISK-WRITE(y); DISK-WRITE(z); DISK-WRITE(x);

  11. A D F L N P T3 T4 T1 T2 T7 T8 T5 T6 • B-Tree-Insert(T,k): • Insert a key in a B-Tree: Root[T] t=4 S Root[T] H r r A D F H L N P T3 T4 T7 T8 T1 T2 T5 T6

  12. B-Tree-Insert(T, k) • r=T.root • if r.n == 2t-1 • s=Allocate-Node() • T.root = s • s.leaf = FLASE • s.n = 0 • s.c1 = r • B-Tree-Split-Child(s, 1) • B-Tree-Insert-Nonfull(s. k) • else B-Tree-Insert-Nonfull(r. k)

  13. B-Tree-Insert-Nonfull(x, k) • i = x.n • if x.leaf • While i >=1 and k < x.keyi • x.keyi+1 =x.keyi • i=i-1 • x.keyi+1 = k • x.n = x.n + 1 • DISK-Write(x) • else while i >=1 and k < x.keyi • i = i-1 • i = i+1 • DISKRead(x.Ci) • if x.ci.n == 2t-1 • B-Tree-Split-Child(x, i) • if k > x.keyi • i = i+1 • B-Tree-Insert-Nonfull(x.Ci, k)

  14. t=3 (a) Initial tree • Example:Inserting keys into a B-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 G M P T X A B C D E J K N O R S T U V Y Z R S U V (c) Q inserted G M P T X A B C D E J K N O Q R S U V Y Z

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

  16. ( x has t keys ) x • 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. K delete k from x. x Recursively delete k’ and replace k by k’ in x. K y k’ x K z k’ x K z y t-1 t-1 2t-1 K

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

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

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

More Related