• 沒有找到結果。

Chapter 18

N/A
N/A
Protected

Academic year: 2022

Share "Chapter 18 "

Copied!
22
0
0

加載中.... (立即查看全文)

全文

(1)

Chapter 18

B-Trees

Lee, Hsiu-Hui

Ack: This presentation is based on the

lecture slides from Prof. Tsai, Shi-Chun as

(2)

B-Tree 性質:

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

M

D H

J K L B C F G

Q T X V W Y Z N P R S

(3)

典型之 B-Tree 運用情況:

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

處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory.

Memory Main Secondary Memory

(

disks )

(

RAM )

object some

pointer to

← a x

READ(x) -

DISK

of x fields the

modify and/or

access that

operations

(4)

Definition of B-trees

B-Tree T : is a rooted tree with the properties:

1

. Every node x has the following fields:

a. n[x]:# of keys currently stored in node x b.

c. leaf[x] is true if x is a leaf; false if x is an internal node.

2. Each internal node x contains n[x]+1 pointers C

1

[x] , C

2

[x] ,… , C

n[x]+1

[x] to its children.

Leaf nodes have no children ( i.e. its C

i

[x] undefined).

3. If k

i

is any key stored in the subtree with root C

i

[x]

] [ ]

[ ]

[ x key x key

[ ]

x key

1

2

≤ L ≤

n x

key1 key2

K3 K2

K1

C1[x] C2[x] C3[x x

] [ ]

[ ]

[ ≤ ≤ ≤ ≤ ≤ ≤

+

key x k key x k key x k

k L

(5)

4. All leaves have the same depth, which is the tree’s height h.

5. t: minimum degree of the B-tree.

Every node (other than root) must have at least t-1 keys.

(have at least t children) Every node can contain at most 2t-1 keys.

(have at most 2t children)

A node is FULL, if it contains exactly 2t-1 keys

.

2-3-4 tree:

When t=2, every internal node has either 2, 3, or 4 children

(6)

Theorem 18.1

The larger the value of t, the smaller the height of the B-tree.

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

n

,

≥ 2 t

. log 2

+ 1

t n

h

(7)

Proof:

1

t-1 t-1

t-1 t-1 t-1 t-1

t t

t t t t

1

2

2t 2t2

+

=

hi

t i

t n

1

2

1

) 1 ( 1

. 1 1 2

) 1 1 ( 2

1 =

+

=

h t h

t t t

# of nodes

. log

. h

n t

n

t

h

+ ≥

+

2 1

2

1

(8)

Basic operations on B-trees

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.

(9)

B-Tree-Search(x,k)

Total CPU time

).

log (

)

( th O t n

O = t

(10)

Ex. B-Tree-Search(x,R)

(11)

Creating an empty B-tree

) 1 ( O

Total CPU time

(12)

Splitting a node

Splitting a full node y ( have 2t-1 keys ) around its median key

into 2 nodes having (t-1) keys each.

full

(13)

B-Tree-Split-Child(x, i, y)

(14)

Insert a key in a B-Tree

(15)

Splitting the root is the only way to increase the height of a B-tree.

(16)
(17)

• Example:Inserting keys into a B-Tree.

t=3

A C D E

(a) Initial tree

J K N O R S T U V Y Z G M P X

G M P T X

(c) Q inserted

A B C D E J K N O R S T U V Y Z G M P X

(b) B inserted

R S U V G M P T X

(18)

(d) L insert

A B C D E J K L N O Q R S U V Y Z P

G M T X

(e) F insert

P

C G M T X

(19)

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.

( x has t keys ) ≥

K x

delete k from x.

K x

k’

t keys

y

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

K x

k’

t keys

z

(20)

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 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 Ci[x] t-1

keys

(21)

• 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 U V Y Z

P

C G M T X

D E F

A B J K L N O Q R S U V Y Z

P

C G M T X

D E

(c) M delete:case 2a

P

(22)

(d) G deleted:case 2c

A B N O Q R S U V Y Z

P

C L T X

D E J H

(e) D deleted:case 3b

C L P T X

A B E J K N O Q R S U V Y Z

(e’) tree shrinks in height

C L P T X

A B E J K N O Q R S U V Y Z

(f) B delete:case 3a

E L P T X

參考文獻

相關文件

our class: four to six times harder than a normal one in NTU around seven homework sets (and a hard final project) homework due within two weeks.. even have homework 0 and 1 NOW

• If a graph contains a triangle, any independent set can contain at most one node of the triangle.. • We consider graphs whose nodes can be partitioned into m

When we know that a relation R is a partial order on a set A, we can eliminate the loops at the vertices of its digraph .Since R is also transitive , having the edges (1, 2) and (2,

1 Generalized Extreme Value Distribution Let Y be a random variable having a generalized extreme- value (GEV) distribution with shape parameter ξ, loca- tion parameter µ and

[r]

• Definition: A max tree is a tree in which the key v alue in each node is no smaller (larger) than the k ey values in its children (if any). • Definition: A max heap is a

Classifying sensitive data (personal data, mailbox, exam papers etc.) Managing file storage, backup and cloud services, IT Assets (keys) Security in IT Procurement and

 understand and use the English terms for describing the animal types, external features, body parts, feeding habits, movement and habitats of a panda, a crocodile and a crab