Docsity

B-Trees: Balanced Search Tree for Disk Access, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download B-Trees: Balanced Search Tree for Disk Access and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

B- Trees

Motivation

Large differences between time access to disk,

cash memory and core memory

Minimize expensive access

(e.g., disk access)

B-tree: Dynamic sets that is optimized for disks

In B-tree, a node can have n keys where n is the positive

integer ≥2. A node with n keys have n+1 child nodes. A

typical B-tree node x has following information.

1. The number of keys x.n

2. The array of keys [x.key1,x.key2,…,x.keyx.n]. These

keys are sorted in ascending order i.e.

x.key1≤x.key2≤…≤x.keyx.n.

3. The array of pointers to the x.n+1 child nodes

[x.c1,x.c2,…,x.cx.n+1]. If x is a leaf node, this array is

empty as leaf nodes do not have children.

4. An identifier x.leaf that gives if x is a leaf node.

B-Trees

Example: a 4-way B-tree

B-tree 4-way tree

B-tree

1. It is perfectly balanced: every leaf node is at the same depth.

2. Every node, except maybe the root, is at least half-full

t-1≤ #keys ≤2t-

3. Every internal node with k keys has k+1 non-null children

B-Tree: Search X

B-Tree: Insert X

1. As in M-way tree find the leaf node to which X should be

added

2. Add X to this node in the appropriate place among the

values already there

(there are no subtrees to worry about)

3. Number of values in the node after adding the key:

Fewer than 2t-1: done

Equal to 2t: overflowed

4. Fix overflowed node

Fix an Overflowed

1. Split the node into three parts, M=2t :

Left: the first t values, become a left child node

Middle: the middle value at position t , goes up to parent

Right: the last t-1 values, become a right child node

2. Continue with the parent:

  1. Until no overflow occurs in the parent
  2. If the root overflows, split it too, and create a new root node

Insert example

Insert 3:

M  6 ; t  3

Insert 38:

M  6 ; t  3

Insert 4:

OVERFLOW

SPLIT IT

OVERFLOW

SPLIT IT

M  6 ; t  3

Complexity Insert

Inserting a key into a B-tree of height h is done in a

single pass down the tree and a single pass up the

tree

Complexity:

O ( h ) O (log n )

t

B-Tree: Delete X

Search the tree and find the node x

containing k.

If x is a leaf node, we need to consider the

following cases.

a. If x has at least t keys, we delete k from x.

This is illustrated in figure 7 with t=3.

B-Tree: Delete X

Delete as in M-way tree

A problem:

might cause underflow : the number of keys

remain in a node < t-

Recall: The root should have at least 1 value in it, and all other nodes should

have at least t-1 values in them

B-Tree: Delete X

b. If x has only t−1 keys, we look at its

immediate sibling nodes. If one of the

siblings has at least t keys, we steal one key

from the sibling. To steal the key, we move

the key to the parent node and an

appropriate key from the parent node is

moved down to x. Now x has t keys, we can

delete k from x.