B-Trees ( Rizwan Rehman)

27
B-Trees (Rizwan Rehman) Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries to overcome cache-miss penalties.

description

B-Trees ( Rizwan Rehman). Large degree B-trees used to represent very large dictionaries that reside on disk. Smaller degree B-trees used for internal-memory dictionaries to overcome cache-miss penalties. - PowerPoint PPT Presentation

Transcript of B-Trees ( Rizwan Rehman)

Page 1: B-Trees ( Rizwan Rehman)

B-Trees

(Rizwan Rehman)

• Large degree B-trees used to represent very large dictionaries that reside on disk.

• Smaller degree B-trees used for internal-memory dictionaries to overcome cache-miss penalties.

Page 2: B-Trees ( Rizwan Rehman)

• A B-tree is a keyed index structure, comparable to a number of memory resident keyed lookup structures such as balanced binary tree, the AVL tree, and the 2-3 tree.

• The difference is that a B-tree is meant to reside on disk, being made partially memory-resident only when entries int the structure are accessed.

• The B-tree structure is the most common used index type in databases today.

• It is provided by ORACLE, DB2, and INGRES.

Page 3: B-Trees ( Rizwan Rehman)

• For a binary search tree, the search time is O(h), where h is the height of the tree.

• The height cannot be less than roughly log2n for a tree with n nodes.

• If the tree is too large for internal memory, each access of a node is an I/O.

• For a tree with 10,000 nodes: • log210000 = 100 disk accesses

• We need a structure that would require only 3 or 4 disk accesses.

Page 4: B-Trees ( Rizwan Rehman)

AVL Trees

• n = 230 = 109 (approx).• 30 <= height <= 43.• When the AVL tree resides on a disk, up to

43 disk access are made for a search.• This takes up to (approx) 4 seconds.• Not acceptable.

Page 5: B-Trees ( Rizwan Rehman)

m-way Search Trees

• Each node has up to m – 1 pairs and m children. • m = 2 => binary search tree.

Page 6: B-Trees ( Rizwan Rehman)

4-Way Search Tree

10 30 35

k < 10 10 < k < 30 30 < k < 35 k > 35

Page 7: B-Trees ( Rizwan Rehman)

Definition Of B-Tree• Definition assumes external nodes

(extended m-way search tree).• B-tree of order m.

m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least

ceil(m/2) children. External (or failure) nodes on same level.

Page 8: B-Trees ( Rizwan Rehman)

2-3 And 2-3-4 Trees• B-tree of order m.

m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least

ceil(m/2) children. External (or failure) nodes on same level.

• 2-3 tree is B-tree of order 3. • 2-3-4 tree is B-tree of order 4.

Page 9: B-Trees ( Rizwan Rehman)

B-Trees Of Order 5 And 2• B-tree of order m.

m-way search tree. Not empty => root has at least 2 children. Remaining internal nodes (if any) have at least

ceil(m/2) children. External (or failure) nodes on same level.

• B-tree of order 5 is 3-4-5 tree (root may be 2-node though).

• B-tree of order 2 is full binary tree.

Page 10: B-Trees ( Rizwan Rehman)

Minimum # Of Pairs• n = # of pairs.• # of external nodes = n + 1.• Height = h => external nodes on level h + 1.

1 12 >= 23 >= 2*ceil(m/2)

h + 1 >= 2*ceil(m/2)h-1

n + 1 >= 2*ceil(m/2)h-1, h >= 1

level # of nodes

Page 11: B-Trees ( Rizwan Rehman)

Minimum # Of Pairs

• m = 200.

n + 1 >= 2*ceil(m/2)h-1, h >= 1

height # of pairs

2 >= 1993 >= 19,9994 >= 2 * 106 – 1

5 >= 2 * 108 – 1

h <= log ceil(m/2) [(n+1)/2] + 1

Page 12: B-Trees ( Rizwan Rehman)

Insert

15 20

8

4

1 3 5 6 30 409

Insertion into a full leaf triggers bottom-up node splitting pass.

16 17

Page 13: B-Trees ( Rizwan Rehman)

Split An Overfull Node

• ai is a pointer to a subtree.

• pi is a dictionary pair.

m a0 p1 a1 p2 a2 … pm am

ceil(m/2)-1 a0 p1 a1 p2 a2 … pceil(m/2)-1 aceil(m/2)-1

m-ceil(m/2) aceil(m/2) pceil(m/2)+1 aceil(m/2)+1 … pm am

• pceil(m/2) plus pointer to new node is inserted in parent.

Page 14: B-Trees ( Rizwan Rehman)

Insert

15 20

8

4

1 3 5 6 30 409

• Insert a pair with key = 2.

• New pair goes into a 3-node.

16 17

Page 15: B-Trees ( Rizwan Rehman)

Insert Into A Leaf 3-node• Insert new pair so that the 3 keys are in

ascending order.

• Split overflowed node around middle key.

1 2 3

• Insert middle key and pointer to new node into parent.

1 3

2

Page 16: B-Trees ( Rizwan Rehman)

Insert

15 20

8

4

1 3 5 6 30 409

• Insert a pair with key = 2.

16 17

Page 17: B-Trees ( Rizwan Rehman)

Insert

15 20

8

4

5 6 30 409 16 17

31

2

• Insert a pair with key = 2 plus a pointer into parent.

Page 18: B-Trees ( Rizwan Rehman)

Insert

• Now, insert a pair with key = 18.

15 20

8

1

2 4

5 6 30 409 16 173

Page 19: B-Trees ( Rizwan Rehman)

Insert Into A Leaf 3-node• Insert new pair so that the 3 keys are in

ascending order.

• Split the overflowed node.

16 17 18

• Insert middle key and pointer to new node into parent.

1816

17

Page 20: B-Trees ( Rizwan Rehman)

Insert

• Insert a pair with key = 18.

15 20

8

1

2 4

5 6 30 409 16 173

Page 21: B-Trees ( Rizwan Rehman)

Insert

• Insert a pair with key = 17 plus a pointer into parent.

15 20

8

1

2 4

5 6 30 4093

18

16

17

Page 22: B-Trees ( Rizwan Rehman)

Insert

• Insert a pair with key = 17 plus a pointer into parent.

8

1

2 4

5 6 30 4093 16

17

15

18

20

Page 23: B-Trees ( Rizwan Rehman)

Insert

• Now, insert a pair with key = 7.

1

2 4

5 6 30 4093 16

15

18

20

8 17

Page 24: B-Trees ( Rizwan Rehman)

Insert

• Insert a pair with key = 6 plus a pointer into parent.

30 401

2 4

93 16

15

18

20

8 17

5

7

6

Page 25: B-Trees ( Rizwan Rehman)

Insert

• Insert a pair with key = 4 plus a pointer into parent.

30 401 93 16

15

18

20

8 17

6

4

2

5 7

Page 26: B-Trees ( Rizwan Rehman)

Insert

• Insert a pair with key = 8 plus a pointer into parent.

• There is no parent. So, create a new root.

30 401

93

16

15

18

206

8

2

5 7

417

Page 27: B-Trees ( Rizwan Rehman)

Insert

• Height increases by 1.

30 401 93 16

15

18

2062

5 7

4 17

8