You are on page 1of 42

B- Trees

Lecture 36-38

B-tree 7/20/21
B tree
ò  If m is the order of the tree

ò  Every internal node has at most m children.


ò  Every internal node (except root) has at least ⌈m /
2⌉ children.
ò  The root has at least two children if it is not a leaf
node.
ò  Every leaf has at most m − 1 keys
ò  An internal node with k children has k − 1 keys.
ò  All leaves appear in the same level
B-tree 7/20/21
A very small B tree

B-tree 7/20/21
Searching the tree

keys < 7 keys > 16

7 < keys < 16

B-tree 7/20/21
Organization
ò  Each non-terminal node can have a variable number of
child nodes
ò  Must all be in a specific key range
ò  Number of child nodes typically vary between d (ceiling
(m/2))or t and 2d
ò  Will split nodes that would otherwise have contained 2d + 1
child nodes
ò  Will merge nodes that contain less than d child nodes

B-tree 7/20/21
Insertions
ò  Assume a tree where each node can contain three
pointers
ò  Step 1:

1
ò  Step 2:

ò  Step 3: 1 2

1 2 3 2

B-tree
ò  Split node in middle
1 3 7/20/21
Insertions
ò  Step 4:
2
1 3 4

ò  Step 5: 2
1 3 4 5
ò  Split
2 4
ò  Move up
B-tree
1 3 5 7/20/21
Insertions
ò  Step 6: Insert6

2 4
1 3 5 6

ò  Step 7:Insert7
2 4
1 3 5 6 7

B-tree 7/20/21
Step 7 continued

2 4 6
1 3 5 7

ò  Split after
the promotion 4

2 6
1
B-tree 3 5 7 7/20/21
Two basic operations
ò  Split: 5 6 7
ò  When trying to add to a full node
6
ò  Split node at central value
5 7
ò  Promote:
ò  Must insert root of split
node higher up
ò  May require a new split

B-tree 7/20/21
More about internal nodes
ò  Consist of n -1 key values K1, K2, …, Kn-1 ,and n tree
pointers P1, P2, …, Pn :
ò  < P1,K1, P2, K2, P3, …, Pn-1, Kn-1,, Pn>
ò  The keys are ordered K1 < K2 < … < Kn-1
ò  For each tree value X in the subtree pointed at by tree
pointer Pi, we have:
ò  X > Ki-1 for 1 ≤ i ≤ n
ò  X ≤ Ki for 1 ≤ i ≤ n - 1

B-tree 7/20/21
Best cases and worst cases
ò A B tree of degree m and height h will
store

ò At most mh – 1(m – 1) = mh – m records

ò At least 2⌈m / 2⌉h – 1 records

B-tree 7/20/21
Searches
ò  def search (k) :
return tree_search (k, root)

B-tree 7/20/21
Searches
def tree_search (k, node) :
if node is a leaf :
return node
elif k < k_0 :
return tree_search(k, p_0)

elif k_i ≤ k < k_{i+1}
return tree_search(k, p_{i+1})

elif k_d ≤ k
return tree_search(k, p_{d+1});

B-tree 7/20/21
ò  Next lecture 37

B-tree 7/20/21
ISAM (Indexed Sequential
Access Method)

1.7 5.1 21.2 26.8 ...

16
Why use B-trees
ò  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


B-Trees
A B-tree is an M-way search tree with three properties :

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


depth
2.  Every internal node other than the root, is at least half-
full, i.e. M/2-1 ≤ #keys ≤ M-1
3.  Every internal node with k keys has k+1 non-null
children

For simplicity we consider M even and we use t or d=M/2:


2.* Every internal node other than the root is at least half-
full, i.e. t-1≤ #keys ≤2t-1, t≤ #children ≤2t

B-tree 7/20/21
Example: 4-way B-tree
1<= keys<=3, 2<= #child<=4

20 40 20 40

0 5 10 25 35 45 55 0 5 25 35 45 55

10
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-1
3. Every internal node with k keys has k+1 non-null children
B-tree 7/20/21
B-tree Height
Any B-tree with n keys, height h and minimum degree t
satisfies:
n +1
h ≤ logt
2

B-tree
B-Tree: Insert X

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

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

ò  Less than 2t-1: done


ò  Equal to 2t: overflowed
4.  Fix overflowed node
B-tree 7/20/21
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+1, 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

B-tree 7/20/21
Insert example
M = 6; t = 3

20 40 60 80

0 5 10 15 25 35 45 55 62 66 70 74 78 87 98

Insert 3:
20 40 60 80

0 3 5 10 15 25 35 45 55 62 66 70 74 78 87 98

B-tree
20 40 60 80 M = 6; t = 3

0 3 5 10 15 25 35 45 55 62 66 70 74 78 87 98

Insert 61: 20 40 60 80
OVERFLOW

0 3 5 10 15 25 35 45 55 61 62 66 70 74 78 87 98

SPLIT IT
20 40 60 70 80

0 3 5 10 15 25 35 45 55 61 62 66 74 78 87 98
B-tree 7/20/21
M = 6; t = 3
20 40 60 70 80
Insert 38:

0 3 5 10 15 25 35 45 55 61 62 66 74 78 87 98

20 40 60 70 80

0 3 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98

B-tree 7/20/21
Insert 4: M = 6; t = 3
20 40 60 70 80

0 3 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98

20 40 60 70 80
OVERFLOW

0 3 4 5 10 15 25 35 38 45 55 61 62 66 74 78 87 98

SPLIT IT
OVERFLOW
5 20 40 60 70 80
SPLIT IT

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98
B-tree 7/20/21
M = 6; t = 3

OVERFLOW
5 20 40 60 70 80
SPLIT IT

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98

60

5 20 40 70 80

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98

B-tree
Complexity - Insert

O(h) = O(logt n)
t= degree of tree = m/2
Assignment
①  Create a B-tree of order 6/5/4/3 by inserting values from 1
to 25. show each operation.

②  Construct a B tree of order 5 with the following set of data


5, 3, 21,9,1, 13, 2, 7, 10, 12, 4, 8,22, 90, 100, 150,291,450

③  Construct a B tree of order 5 with the following set of data

23, 27, 78, 1, 11, 14, 30, 4, 9, 81, 88, 97, 101, 670, 500, 495

④  Construct a B tree of order 6 with the following set of data:

D, H, Z, K, B, P, Q, E, A, S, W, T, C, L, N, Y, M, X,Y

⑤  Create a B-tree of order 5/6/7 by inserting all alphabets


from A to Z by showing each operation
B-tree 7/20/21
ò  Next lecture

B-tree 7/20/21
B-Tree: Delete X
ò  A problem:
ò  might cause underflow: the number of keys remain in a
node < t-1

B-tree 7/20/21
M = 6; t = 3

Underflow Example
60
Delete 87:

5 20 40 70 80

0 3 4 10 15 25 35 38 45 55 61 62 66 74 78 87 98

60 B-tree
UNDERFLOW

5 20 40 70 80

0 3 B-tree
4 10 15 25 35 38 45 55 61 62 66 74 78 98
B-Tree-Delete(x,k)
1st case: k is in x and x is a leaf à delete k
Example M=6 => t=3

k=66

x 62 66 70 74 x 62 70 74

B-tree 7/20/21
k=50
x 30 50 70 90 x 30 45 70 90

5 6 7 5 6 7
y 35 40 45 y 35 40 45
Example
B-tree t=3 7/20/21
2nd case cont.:

c.  Both a and b are not satisfied: y and z have t-1


keys
ò  Merge the two children, y and z

ò  Recursively delete k from the merged cell

Example t=3

x 30 70 90
x 30 50 70 90

y 35 40 55 60 z y 35 40 50 55 65

B-tree1 2 3 4 5 6 1 2 3 4 5 6
7/20/21
B-tree 7/20/21
Case 3b)

B-tree 7/20/21
3a)

B-tree 7/20/21
Case 3. If the key k is not present in internal node, determine the
root x.c(i) of the appropriate subtree that must contain k, if k is in
the tree at all. If x.c(i) has only t-1 keys, execute step 3a or 3b as
necessary to guarantee that we descend to a node containing at least
t keys. Then finish by recursing on the appropriate child of x.

3a) If x.c(i) has only t-1 keys but has an immediate sibling with at
least t keys, give x.c(i) an extra key by moving a key from x down
into x.c(i), moving a key from x.c(i) ’s immediate left or right sibling
up into x, and moving the appropriate child pointer from the sibling
into x.c(i).

3b) If x.c(i) and both of x.c(i)’s immediate siblings have t-1 keys,
merge x.c(i) with one sibling, which involves moving a key from x
down into the new merged node to become the median key for that
node.
B-tree 7/20/21
Deletions
ò  def delete (record) :
ò  Locate target leaf and remove the entry
ò  If leaf is less than half full:
ò  Try to re-distribute, taking from sibling
(adjacent node with same parent)
ò  If re-distribution fails:
ò  Merge leaf and sibling
ò  Delete entry to one of the two merged
leaves
B-tree
ò  Merge could propagate to root 7/20/21
① Create a B-tree of order 5 by inserting values from 1 to
25.
Show each operation.
Delete 2, 7, 9, 20, 6, 17, 1, 8, 22,25
2. Trie data structure – roll no. 35 - 40

B-tree 7/20/21
END

B-tree 7/20/21

You might also like