B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000...

74
B-trees B-trees

Transcript of B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000...

Page 1: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

B-trees

B-trees

Page 2: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Motivation

When the size of a data-structure is very big, it is kept onsecondary memory (hard-disk/SSD).

Secondary memory is slow compared to RAM.

The time for reading a block in secondary memoryconsists of seek time (the delay between the request andthe transfer of the data) and transfer time.

The seek time is ∼ 10 milliseconds in hard-disks, and∼ 0.1 milliseconds in SSDs.

We want a data-structure that minimizes the diskaccesses.

B-trees

Page 3: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Motivation

Suppose we need to store a dynamic set with 109

elements on secondary memory.

The height of a binary search tree is at leastblog 109c = 29. Thus, a search operation in an optimalbinary search tree requires 30 disk accesses in the worstcase.

1

1 1

1 1 1 1

1

2

22

23

1000

1000 1000 1000

1000 1000 1000

1000

1001·1000

10012·1000

B-trees

Page 4: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Motivation

A B-tree is a generalization of binary search tree, that canstore many elements in one node.

If for example we store 1000 elements in a node (andeach internal node has 1001 children), a tree of height 2suffices. Thus, a search operation in an optimal B-treerequires 3 disk accesses in the worst case.

In fact, we can store the top two levels in RAM, and thena search operation requires only 1 disk access.

1

1 1

1 1 1 1

1

2

22

23

1000

1000 1000 1000

1000 1000 1000

1000

1001·1000

10012·1000

B-trees

Page 5: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

B-tree

A B-tree is a rooted tree with the following properties.

1 A node x has the following fields.1 x .n, the number of keys stored at x .2 The keys of the elements, x .key1, x .key2, . . . , x .keyx .n in

increasing order (x .key1 ≤ x .key2 ≤ · · · ).3 Pointers to the satellite data of the keys.4 A boolean value x .leaf which is TRUE if and only if x is

a leaf.

t=2

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

B-trees

Page 6: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

B-tree

2 An internal nodes x has x .n + 1 children. The node xstores pointers to it children x .c1, x .c2, . . . , x .cx .n+1.

3 The keys x .key1, . . . , x .keyx .n separate the ranges of keysin the subtrees of x :If ki is some key in the subtree of x .ci thenk1 ≤ x .key1 ≤ k2 ≤ x .key2 ≤ · · · ≤ x .keyx .n ≤ kx .n+1.

t=2

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

B-trees

Page 7: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

B-tree

4 All leaves have the same depth.

5 The tree has a fixed parameter t, called minimum degree,that defines upper and lower bounds on the number ofkeys stored at each node:

1 Every node except the root must have at least t− 1 keys.2 If the tree is not empty, the root must have at least one

key.3 Every node can have at most 2t − 1 keys.

t=2

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

B-trees

Page 8: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

The height of a B-tree

Theorem

For n ≥ 1, the height of a B-tree with minimum degree tcontaining n keys is at most logt

n+12.

Consider a B-tree of height h with minimal number ofkeys: 1 key at the root and t − 1 keys at each other node.

Number of keys in nodes of depth i ≥ 1 is 2t i−1(t − 1).

t-1

0

1

2t-1

t-1

t-1

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

t-1

t-1

t-1

t-1 t-1 t-1

1

3

1

2

2t 2t(t-1)

2t2 2t2(t-1)

2(t-1)

1depth nodes keys

B-trees

Page 9: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

The height of a B-tree

The number of keys in a minimal B-tree is

1 +h∑

i=1

2t i−1(t − 1) = 1 + 2(t − 1)h∑

i=1

t i−1

= 1 + 2(t − 1)

(th − 1

t − 1

)= 2th − 1.

For any B-tree we have n ≥ 2th − 1.

Therefore, h ≤ logtn+12

.

B-trees

Page 10: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Searching

Search(x , k)

(1) i ← 1(2) while i ≤ x .n AND k > x .keyi(3) i ← i + 1(4) if i ≤ x .n AND k = x .keyi(5) return (x , i)(6) else if x .leaf(7) return NULL(8) else(9) Disk-Read(x .ci)(10) return Search(x .ci , k)

t=2Search(T.root,8)

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

xi=1

We assume that the root is stored in RAM.

CPU time: Θ(th) = Θ(t logt n).

Disk accesses: h ≤ logt n.

B-trees

Page 11: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Searching

Search(x , k)

(1) i ← 1(2) while i ≤ x .n AND k > x .keyi(3) i ← i + 1(4) if i ≤ x .n AND k = x .keyi(5) return (x , i)(6) else if x .leaf(7) return NULL(8) else(9) Disk-Read(x .ci)(10) return Search(x .ci , k)

t=2Search(T.root,8)

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

xi=1

We assume that the root is stored in RAM.

CPU time: Θ(th) = Θ(t logt n).

Disk accesses: h ≤ logt n.

B-trees

Page 12: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Searching

Search(x , k)

(1) i ← 1(2) while i ≤ x .n AND k > x .keyi(3) i ← i + 1(4) if i ≤ x .n AND k = x .keyi(5) return (x , i)(6) else if x .leaf(7) return NULL(8) else(9) Disk-Read(x .ci)(10) return Search(x .ci , k)

t=2Search(T.root,8)

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

xi=2

We assume that the root is stored in RAM.

CPU time: Θ(th) = Θ(t logt n).

Disk accesses: h ≤ logt n.

B-trees

Page 13: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Searching

Search(x , k)

(1) i ← 1(2) while i ≤ x .n AND k > x .keyi(3) i ← i + 1(4) if i ≤ x .n AND k = x .keyi(5) return (x , i)(6) else if x .leaf(7) return NULL(8) else(9) Disk-Read(x .ci)(10) return Search(x .ci , k)

t=2Search(T.root,8)

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

xi=3

We assume that the root is stored in RAM.

CPU time: Θ(th) = Θ(t logt n).

Disk accesses: h ≤ logt n.

B-trees

Page 14: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Searching

Search(x , k)

(1) i ← 1(2) while i ≤ x .n AND k > x .keyi(3) i ← i + 1(4) if i ≤ x .n AND k = x .keyi(5) return (x , i)(6) else if x .leaf(7) return NULL(8) else(9) Disk-Read(x .ci)(10) return Search(x .ci , k)

t=2Search(T.root,8)

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

xi=1

We assume that the root is stored in RAM.

CPU time: Θ(th) = Θ(t logt n).

Disk accesses: h ≤ logt n.

B-trees

Page 15: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Creating an empty B-tree

Create(T )

(1) x ← Allocate-Node()(2) x .leaf ← TRUE(3) x .n← 0(4) diskWrite(x)(5) T .root← x

CPU time: Θ(1).

Disk accesses: 1.

B-trees

Page 16: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Insertion

The goal is to insert the new element into an existing leaf.

This is easy if the leaf has less than 2t − 1 elements.

10 30 50

2 4 8

t=2

12 20 30 60 65

Insert(T,15)

15 can be inserted to this leaf

B-trees

Page 17: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Insertion

The hard case is when the leaf has 2t − 1 elements.

The solution is to perform the following step during thedescend in the tree: Before entering a node, if the nodehas 2t − 1 elements, perform node splitting on the nodein order to reduce the number of elements.

10 30 50

2 4 8

t=2

12 20 30 60 65

Insert(T,9)

B-trees

Page 18: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Node splitting

Let v be a node with 2t − 1 elements. The node splittingoperation on v replaces v with two nodes:one containing the t − 1 smallest elements of v , andanother containing the t − 1 largest elements.

The median element of v is moved to the parent of v .If v is the root, a new root is created.

10 40 60

15 20 25 30 35

10 40 60

15 20 30 35

25

B C D E A CA F B D FE

t=3

B-trees

Page 19: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Node splitting

If v is not the root, the parent of v has less than 2t − 1elements, so it is legal to move the median to the parent.

10 40 60

15 20 25 30 35

10 40 60

15 20 30 35

25

B C D E A CA F B D FE

t=3

B-trees

Page 20: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

10 30 50

2 4 8

t=2

12 20 30 60 65

Insert(T,9)

B-trees

Page 21: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

10 30 50

2 4 8

t=2

12 20 30 60 65

Insert(T,9)

Split node

B-trees

Page 22: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

10

2 4 8

t=2

12 20 30 60 65

Insert(T,9)

30

50

B-trees

Page 23: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

10

2 4 8

t=2

12 20 30 60 65

Insert(T,9)

30

50Split node

B-trees

Page 24: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

4,10

t=2

12 20 30 60 65

Insert(T,9)

30

50

2 8

B-trees

Page 25: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

4,10

t=2

12 20 40 60 65

Insert(T,9)

30

50

2 8 9

9 is addedto the leaf

B-trees

Page 26: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525

4060n: 3 leaf: FALSE

B-trees

Page 27: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525 keyn: 2 leaf: FALSE

c

z

4060n: 3 leaf: FALSE

B-trees

Page 28: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525 keyn: 2 leaf: FALSE

c

z3035

4060n: 3 leaf: FALSE

B-trees

Page 29: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525 keyn: 2 leaf: FALSE

c

z3035

D E F

4060n: 3 leaf: FALSE

B-trees

Page 30: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525 keyn: 2 leaf: FALSE

c

z3035

D E F

4060n: 3 leaf: FALSE

B-trees

Page 31: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525 keyn: 2 leaf: FALSE

c

z3035

D E F

4060n: 3 leaf: FALSE

B-trees

Page 32: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 5 leaf: FALSE

D E F

303525 keyn: 2 leaf: FALSE

c

z3035

D E F

4060n: 3 leaf: FALSE

B-trees

Page 33: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

SplitChild(x , i)

(1) y ← x .ci(2) z ← AllocateNode()(3) z .leaf ← y .leaf(4) z .n← t − 1(5) for j = 1 to t − 1(6) z .keyj ← y .keyj+t

(7) if not y .leaf(8) for j = 1 to t(9) z .cj ← y .cj+t

(10) for j = x .n + 1 downtoi + 1

(11) x .cj+1 ← x .cj(12) x .ci+1 ← z(13) for j = x .n downto i(14) x .keyj+1 ← x .keyj(15) x .keyi ← y .keyt(16) x .n← x .n + 1(17) y .n← t − 1(18) DiskWrite(y)(19) DiskWrite(z)(20) DiskWrite(x)

B CA

t=310key

c

1520key

c

xi=2

yn: 2 leaf: FALSE

keyn: 2 leaf: FALSE

c

z3035

D E F

406025n: 4 leaf: FALSE

B-trees

Page 34: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Insert — Pseudo-code

Insert(T , k)

(1) r ← T .Root(2) if r .n = 2t − 1(3) s ← AllocateNode()(4) T .root← s(5) s.leaf ←FALSE(6) s.n← 0(7) s.c1 ← r(8) SplitChild(s, 1)(9) InsertNonfull(s, k)(10) else(11) InsertNonfull(r , k)

B-trees

Page 35: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Insert — Pseudo-code

InsertNonfull(x , k)

(1) i ← x .n(2) if x .leaf(3) while i ≥ 1 AND

k < x .keyi(4) x .keyi+1 ← x .keyi(5) i ← i − 1(6) x .keyi+1 ← k(7) x .n← x .n + 1(8) DiskWrite(x)(9) else

(10) while i ≥ 1 ANDk < x .keyi

(11) i ← i − 1(12) i ← i + 1(13) DiskRead(x .ci)(14) if x .ci .n = 2t − 1(15) SplitChild(x , i)

if k > x .keyi(16) i ← i + 1(17) InsertNonfull(x .ci , k)

B-trees

Page 36: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deletion

The easy case is deleting an element from a leaf that hasmore than t − 1 elements.

The hard cases are deleting an item from a leaf that hast − 1 elements, and deleting an item from an internalnode.

The following step is performed during the descend in thetree: Before entering a node v , if v has t − 1 elements,perform shifting or merging in order to increase thenumber of elements.

B-trees

Page 37: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Shifting

If v has an immediate left sibling u and u has at least telements:

1 Move the element in the parent of v that “separates” vand u to v .

2 Move the maximum element of u to the parent of v .

10 40

E FA

5020 25 30

A C

10

B

20 25 5040

30t=2

B C D D FE

vu

B-trees

Page 38: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Shifting

Otherwise, if v has an immediate right sibling w , and whas at least t elements then

1 Move the element of the parent of v that “separates” vand w to v .

2 Move the minimum element of w to the parent of v .

10 40

A B C E

30 50 60

D D EA C

10

60

B

30 40

50t=2

v w

5 5

B-trees

Page 39: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Merging

If v has no immediate sibling with at least t elements:1 Merge v with an immediate sibling

(merge with the immediate left sibling if it exists, andotherwise merge with the right immediate right sibling).

2 Move the “separating element” from the parent of v tothe new node.

10 40

A B

30

A CB

30 40 50

t=2

50

C D

10

D

v

B-trees

Page 40: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 1

When reaching the node x that contains the key k we want todelete. there are several cases.

Case 1: If x is a leaf, delete the key.

20 25 30 20 30

t=2

x

B-trees

Page 41: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 2

Suppose that x is an internal node, and k = x .keyi .Let y = x .ci and z = x .ci+1.

Case 2: If y has at least t elements, recursively delete thepredecessor of k (which is in the subtree of y) and put it in xinstead of k .

t=2

zy

x Delete(x,40)10 40

20 30

23 26 35

predecessor

B-trees

Page 42: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 2

Suppose that x is an internal node, and k = x .keyi .Let y = x .ci and z = x .ci+1.

Case 2: If y has at least t elements, recursively delete thepredecessor of k (which is in the subtree of y) and put it in xinstead of k .

t=2

zy

x Delete(x,40)10 40

20 26

23 30 35

B-trees

Page 43: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 2

Suppose that x is an internal node, and k = x .keyi .Let y = x .ci and z = x .ci+1.

Case 2: If y has at least t elements, recursively delete thepredecessor of k (which is in the subtree of y) and put it in xinstead of k .

t=2

zy

x Delete(x,40)

20 26

23 30

10 35

B-trees

Page 44: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 3

Case 3: If y has t − 1 elements and z has at least t elements,recursively delete the successor of k (which is in the subtree ofz) and put it in x instead of k .

t=2

zy

x Delete(x,40)

30 50 60

43 46

successor

10 40

B-trees

Page 45: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 3

Case 3: If y has t − 1 elements and z has at least t elements,recursively delete the successor of k (which is in the subtree ofz) and put it in x instead of k .

t=2

zy

x Delete(x,40)

30 50 60

10 43

46

B-trees

Page 46: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 4

Case 4: If both y and z have t − 1 elements, merge y and z ,and recursively delete k from the merged node.

t=2

zy

x Delete(x,40)

30

10 40

50

25 35 45 55

B-trees

Page 47: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 4

Case 4: If both y and z have t − 1 elements, merge y and z ,and recursively delete k from the merged node.

t=2

zy

x

Delete(x,40)

30 40 50

25 35 45 55

10

B-trees

Page 48: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 4

Case 4: If both y and z have t − 1 elements, merge y and z ,and recursively delete k from the merged node.

t=2

x

Delete(x,40)

30 50

25 55

10

35 40 45

B-trees

Page 49: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Deleting a key — Case 4

Case 4: If both y and z have t − 1 elements, merge y and z ,and recursively delete k from the merged node.

t=2

x

Delete(x,40)

30 50

25 55

10

35 45

B-trees

Page 50: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

delete(T,6)

B-trees

Page 51: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

delete(T,6)

6 < 14 so the next node is the 1st child of the current node.This child has 3 > t−1 elements, so no modification is needed.

B-trees

Page 52: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

delete(T,6)

3 < 6 < 7 so the next node is the 2nd child of the current node.This child has 3 > t−1 elements, so no modification is needed.

B-trees

Page 53: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 234 5 61 2

3 7 11

14

12 13

delete(T,6)

B-trees

Page 54: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 2312 13 4 51 2

3 7 11

14 delete(T,6)

The element 6 is deleted from the current node.

B-trees

Page 55: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 2312 13 4 51 2

3 7 11

14 delete(T,11)

B-trees

Page 56: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 2312 13 4 51 2

3 7 11

14 delete(T,11)

11 < 14 so the next node is the 1st child of the current node.This child has 3 > t−1 elements, so no modification is needed.

B-trees

Page 57: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 2312 13 4 51 2

3 7 11

14 delete(T,11)

11 is the 3rd item of the node.The 4th child has 2 = t − 1 elements, so we don’t replace 11with its successor.The 3rd child has 3 > t − 1 so we replace 11 by its predecessorand delete the predecessor.

B-trees

Page 58: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 10 15 16 17 19 20 22 2312 13 4 51 2

3 7 11

14 delete(T,11)

B-trees

Page 59: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 15 16 17 19 20 22 2312 13 4 51 2

3 7 11

14 delete(T,11)

The element 10 is deleted from the current node.

B-trees

Page 60: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 15 16 17 19 20 22 2312 13 4 51 2

3 7 10

14 delete(T,11)

The element 11 is replaced by 10.

B-trees

Page 61: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 15 16 17 19 20 22 2312 13 4 51 2

3 7 10

14 delete(T,7)

B-trees

Page 62: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 15 16 17 19 20 22 2312 13 4 51 2

3 7 10

14 delete(T,7)

7 < 14 so the next node is the 1st child of the current node.This child has 3 > t−1 elements, so no modification is needed.

B-trees

Page 63: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

8 9 15 16 17 19 20 22 2312 13 4 51 2

3 7 10

14 delete(T,7)

7 is the 2nd item of the node.Both the 2nd and 3rd children of the node have 2 = t − 1elements. Therefore we merge these children and move 7 tothe new node.

B-trees

Page 64: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

15 16 17 19 20 22 2312 13 4 5 7 8 91 2

3 10

14 delete(T,7)

B-trees

Page 65: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

15 16 17 19 20 22 2312 13 4 5 8 91 2

3 10

14 delete(T,7)

The element 7 is deleted from the current node.

B-trees

Page 66: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

15 16 17 19 20 22 2312 13 4 5 8 91 2

3 10

14 delete(T,4)

B-trees

Page 67: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

18 21

15 16 17 19 20 22 2312 13 4 5 8 91 2

3 10

14 delete(T,4)

4 < 14 so the next node is the 1st child of the current node.This child has 2 > t − 1 elements, so a modification is needed.This child has no immediate sibling with more than 2 = t − 1elements, so perform merging on this child and its sibling.The height of the tree decreases by 1.

B-trees

Page 68: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 4 5 8 91 2

3 10 14 18 21

delete(T,4)

3 < 4 < 10 so the next node is the 2nd child of the currentnode.This child has 4 > t−1 elements, so no modification is needed.

B-trees

Page 69: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 4 5 8 91 2

3 10 14 18 21

delete(T,4)

B-trees

Page 70: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 5 8 91 2

3 10 14 18 21

delete(T,4)

The element 4 is deleted from the current node.

B-trees

Page 71: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 5 8 91 2

3 10 14 18 21

delete(T,2)

B-trees

Page 72: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 5 8 91 2

3 10 14 18 21

delete(T,2)

2 < 3 so the next node is the 1st child of the current node.This child has 2 = t − 1 elements, so a modification is needed.The immediate sibling of this child has 3 > t − 1 elements, sowe perform shifting.

B-trees

Page 73: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 8 91 2 3

5 10 14 18 21

delete(T,2)

B-trees

Page 74: B-treesds162/wiki.files/06-B-tree.pdf1000 1000 1000 1000 1000 1000 1000 1000 1001·1000 1001 2·1000 B-trees Motivation AB-treeis a generalization of binary search tree, that can store

Example

t=3

15 16 17 19 20 22 2312 13 8 91 3

5 10 14 18 21

delete(T,2)

The element 2 is deleted from the current node.

B-trees