**Chapter 18 **

**B-Trees**

**Lee, Hsiu-Hui**

**Ack: This presentation is based on the **

**lecture slides from Prof. Tsai, Shi-Chun as**

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

## • **典型之 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**

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

**a. n[x]：# of keys currently stored in node x**

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

**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**

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

**C**

_{1}**[x] , C**

**[x] , C**

_{2}**[x] ,… , C**

**[x] ,… , C**

_{n[x]+1}**[x] to its children.**

**[x] to its children.**

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

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

_{i}**[x] undefined).**

**[x] undefined).**

**3. ** **If k**

**If k**

_{i}**is any key stored in the subtree with root ** **C**

**C**

_{i}**[x] **

**[x]**

### ] [ ]

### [ ]

### [ *x* *key* *x* *key*

_{[}

_{]}

*x* *key*

**1**

### ≤

**2**

### ≤ L ≤

_{n}

_{x}**key1 key2**

K_{3}
**K**_{2}

K_{1}

**C**_{1}**[x]** **C**_{2}**[x]** **C**_{3}**[x**
**x**

### ] [ ]

### [ ]

### [ ≤ ≤ ≤ ≤ ≤ ≤

_{+}

### ≤ **key** **x** **k** **key** **x** **k** **key** **x** **k**

**key**

**x**

**k**

**key**

**x**

**k**

**key**

**x**

**k**

**k** L

**k**

**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 least t children)**

**(have at most 2t children)**

**(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**

**Theorem 18.1**

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

**The larger the value of t, the smaller**

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

*n*

### ,

### ≥ 2 **t**

**t**

### . log ^{2}

### + 1

### ≤ _{t} ^{n}

_{t}

^{n}**h**

**h**

### Proof：

^{1}t-1 t-1

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

t t

t t t t

**1**

**2**

**2t**
**2t**^{2}

− ∑ +

≥

### =

*h* − *i*

*t* *i*

*t* *n*

### 1

2

### 1

) 1 ( 1

. 1 1 2

) 1 1 ( 2

1 ⎟⎟= −

⎠

⎞

⎜⎜

⎝

⎛

−

⋅ −

− +

=

^{h} *t* ^{h}

^{h}

^{h}

*t* *t* *t*

# of nodes

### . log

### . *h*

*n* *t*

^{n}*t*

*h*

### ≥

### + ≥

^{+}

**2**
**1**

**2**

**1**

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

**B-Tree-Search(x,k)**

**B-Tree-Search(x,k)**

**Total CPU time**

### ).

### log (

### )

### ( *th* *O* *t* *n*

*O* = _{t}

_{t}

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

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

**Creating an empty B-tree**

### ) 1 ( **O**

**O**

**Total CPU time**

**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**

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

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

**Insert a key in a B-Tree**

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

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

### (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

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

**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**

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

### (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