<<

Need for Multi-way Search

• In very large nodes may B-Trees reside on disk. • The unit of disk access is a page, 1k, 2k or more bytes. CSE 326 k k1 . . . ki-1 i . . . kM-1 Lecture 10

B-Trees - Lecture 10 2

Example B-Trees

B-Trees are multi-way search trees commonly used in • 1k byte page systems or other applications where data is stored externally on disks and keeping the shallow is important. • Key 8 bytes, pointer 4 bytes A B-Tree of order M has the following properties: • (M-1)8 + 4M = 1024 1. The root is either a leaf or has between 2 and M children. 2. All nonleaf nodes (except the root) have between »M/2ÿ 12 M = 1032 and M children. M = 1032/12⁄ = 86 3. All leaves are at the same depth. All data records are stored at the leaves. Leaves store between »M/2ÿ and M data records. Internal nodes only used for searching.

B-Trees - Lecture 10 3 B-Trees - Lecture 10 4

Example B-Tree Details

• B-tree of order 3 has 2 or 3 children per Each (non-leaf) internal of a B-tree has: node › Between »M/2ÿ and M children. 13:- < < ... < › up to M-1 keys k1 k2 kM-1 6:11 17:-

k1 . . . ki-1 ki . . . kM-1

3 4 6 7 8 11 12 13 14 17 18 Keys are ordered so that:

k1 < k2 < ... < kM-1

B-Trees - Lecture 10 5 B-Trees - Lecture 10 6

1 B-Tree Details Properties of B-Trees

k1 ki-1 ki . . . kM-1 Each leaf node of a B-tree has: . . .

T1 ...... › Between »M/2ÿ and M keys and pointers. Ti TM

Children of each internal node are "between" the items in that node. k k 1 . . . ki-1 i . . . kM Suppose subtree Ti is the i-th child of the node:

all keys in Ti must be between keys ki-1 and ki ≤ < Keys point to i.e. ki-1 Ti ki Keys are ordered so that: data on other ki-1 is the smallest key in Ti < pages. All keys in first subtree T1 k1 k1 < k2 < ... < kM-1 ≥ All keys in last subtree TM kM-1

B-Trees - Lecture 10 7 B-Trees - Lecture 10 8

Example: Searching in B-trees Inserting into B-Trees

• B-tree of order 3: also known as 2-3 tree (2 to 3 • Insert X: Do a Find on X and find appropriate leaf node children) › If leaf node is not full, fill in empty slot with X 13:- • E.g. Insert 5 - means empty slot › If leaf node is full, split leaf node and adjust parents up to root 6:11 17:- node • E.g. Insert 9 13:-

3 4 6 7 8 11 12 13 14 17 18 6:11 17:-

• Examples: Search for 9, 14, 12 • Note: If leaf nodes are connected as a , B- 3 4 6 7 8 11 12 13 14 17 18 tree is called a B+ tree – Allows sorted list to be accessed easily B-Trees - Lecture 10 9 B-Trees - Lecture 10 10

Insert Example Insert Example

13:- 13:-

6:11 17:- 6:- 11:- 17:-

3 4 6 7 8 9 11 12 13 14 17 18 3 4 6 7 8 9 11 12 13 14 17 18

B-Trees - Lecture 10 11 B-Trees - Lecture 10 12

2 Insert Example Deleting From B-Trees

• Delete X : Do a find and remove from leaf 8:13 › Leaf underflows – borrow from a neighbor • E.g. 11 11:- 6:- 17:- › Leaf underflows and can’ borrow – merge nodes, delete parent • E.g. 17 13:- 3 4 6 7 8 9 11 12 13 14 17 18 6:11 17:-

3 4 6 7 8 11 12 13 14 17 18

B-Trees - Lecture 10 13 B-Trees - Lecture 10 14

Delete Example Delete Example

13:- 13:-

6:11 17:- 6:11 -:-

3 4 6 7 8 11 12 13 14 18 3 4 6 7 8 11 12 13 14 18

B-Trees - Lecture 10 15 B-Trees - Lecture 10 16

Run Time Analysis of B-Tree Delete Example Operations • For a B-Tree of order M 11:- › Each internal node has up to M-1 keys to search 6:- 13:- › Each internal node has between »M/2ÿ and M children

3 4 6 7 8 11 12 13 14 18 › Depth of B-Tree storing N items is O(log »M/2ÿN) • Example: M = 86

› log43N = log2 N / log2 43 =.184 log2 N › log43 1,000,000,000 = 5.51 B-Trees - Lecture 10 17 B-Trees - Lecture 10 18

3 Summary of Search Trees

• Problem with Search Trees: Must keep tree balanced to allow › fast access to stored items • AVL trees: Insert/Delete operations keep tree balanced • Splay trees: Repeated Find operations produce balanced trees on average • Multi-way search trees (e.g. B-Trees): More than two children › per node allows shallow trees; all leaves are at the same depth › keeping tree balanced at all times

B-Trees - Lecture 10 19

4