b-tree

19
B-Tree

Upload: kameko-mack

Post on 31-Dec-2015

20 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: B-Tree

B-Tree

Page 2: B-Tree

p2.

B-Tree :

性質:

典型之 B-Tree 運用情況:

在 B-Tree 中若 node x 有 x.n keys, 則 x 含有 x.n+1 個 children.M

D H

J K LF GB C

Q T X

Y ZV WR SN P

MainMemory

Secondary Memory

( disks ) ( RAM )

資料量龐大無法全部存在 main memory.

處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory.object some pointer to a x

READ(x)-DISK

WRITE(x)-DISK

x of fields modify theand/or access that operations

x of fields modify thenot dobut access that operations others

Page 3: B-Tree

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.C1 ,

x.C2 ,… , x.Cx.n+1 指向某 children.

若 x 為一 leaf. 其 x.Ci 沒有定義 .

3. 若 x.keyi 為 x.Ci 所指之 subtree 中任一 key 則

1 2 .. . . x nx key x key x key

1 1 2 2 . . 1. . . x n x nk x key k x key x key k key1 key2

K3K2K1

x.C1 x.C2x.C3

x

Page 4: B-Tree

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.

keys. 1 t

children. t

T

keys. 12t

children.2t

Page 5: B-Tree

Thm :If , then for any n-key B-tree T of height h and minimum degree

Proof:

1n ,2t

.log 2

1

n

th

1

t-1

t-1

t-1

t-1

t-1

t-1

t t

t t t t

1

2

2t

2t2

h

i

ittn1

12)1(1

.121

1)1(21

hh

tt

tt

.log . htn n

th

2

1

2

1

Page 6: B-Tree

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.

Page 7: B-Tree

p7.

B-Tree-Search(x,k) : Algorithm :

Total CPU time :

B-Tree-Search(x,k)

{1. 12. while . and .3. 14. if . and .5. return( , )6. elseif . return NIL7. else DISK-READ( . )8. return B-Tree-Search( . , )

i

i

i

i

ii x n k x keyi i

i x n k x keyx i

x leafxC

xC k

}

( ) ( log ).tO th O t n

Page 8: B-Tree

p8.

B-Tree-Create(T) : Algorithm :

time :

B-Tree-Create(T){ Allocate Node()

x.leaf TRUEx.n=0DISK-WRITE(x)T.root=x

x

}

)(1O

Page 9: B-Tree

p9.

B-Tree-Split-Child(x,i): Splitting a node in a B-Tree :

… N W W …

xKeyi-1[x]

y=Ci[x]

P Q R S T U V

T1 T2 T4T3 T5 T6 T8T7

Keyi [x]t=4

full

… N S W …

y=Ci[x]

T U V

T5 T6 T8T7

11 iii keykeykeyx

P Q R

T1 T2 T4T3

z=Ci+1[x]

Splitting a full node y ( have 2t-1 keys ) around its median key y.keyt into 2 nodes having (t-1) keys each.

Page 10: B-Tree

p10.

Algorithm:B-Tree-Split-Child(x, i)1. z=Allocate-Node()2. y=x.Ci

3. z.leaf = y.leaf4. z.n = t-15. for j=1 to t-1 z.keyj= y.keyj+t

6. if not y.leaf 7. for j= 1 to t z.Cj=y.Cj+t

8. y.n = t-19. for j = x.n +1 downto i+1 x.Cj+1=x.Cj

10. x.Cj+1 = z

11. for j = x.n down to i x.keyj+1= x.keyj

12. x.keyi=y.keyt

13. x.n=x.n+114. DISK-WRITE(y); DISK-WRITE(z); DISK-WRITE(x);

Page 11: B-Tree

p11.

B-Tree-Insert(T,k): Insert a key in a B-Tree :

Root[T]

r

A D F H L N P

T1 T2 T4T3 T5 T6 T8T7

t=4

H

r

L N P

T5 T6 T8T7

A D F

T1 T2 T4T3

Root[T]

S

Page 12: B-Tree

p12.

B-Tree-Insert(T, k)1. r=T.root2. if r.n == 2t-13. s=Allocate-Node()4. T.root = s5. s.leaf = FLASE6. s.n = 07. s.c1 = r8. B-Tree-Split-Child(s, 1)9. B-Tree-Insert-Nonfull(s. k)10. else B-Tree-Insert-Nonfull(r. k)

Page 13: B-Tree

B-Tree-Insert-Nonfull(x, k)1. i = x.n2. if x.leaf3. While i >=1 and k < x.keyi

4. x.keyi+1 =x.keyi

5. i=i-16. x.keyi+1 = k7. x.n = x.n + 18. DISK-Write(x)9. else while i >=1 and k < x.keyi

10. i = i-111. i = i+112. DISKRead(x.Ci)13. if x.ci.n == 2t-114. B-Tree-Split-Child(x, i)15. if k > x.keyi

16. i = i+117. B-Tree-Insert-Nonfull(x.Ci, k)

Page 14: B-Tree

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

Page 15: B-Tree

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

Page 16: B-Tree

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’ keys ty

Recursively delete k’ and replace k by k’ in x.

K

x

k’keys t

z

K

z

x

t-1t-1

y

K2t-1

Page 17: B-Tree

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.

x

Ci[x]

k is in this subtree.

t-1

x

Ci[x] t

• 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.

0

x

t-1keys

02t-1keys

Ci[x] t-1keys

Page 18: B-Tree

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

D E

Page 19: B-Tree

(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