0% found this document useful (0 votes)
38 views

B Trees

The document discusses B-trees, which are balanced search trees that allow for more than two children per node. B-trees store all data in the leaves and only keys and child pointers internally. Basic operations on B-trees like search, insert, create, and delete are explained along with examples.

Uploaded by

rajnikant pubg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

B Trees

The document discusses B-trees, which are balanced search trees that allow for more than two children per node. B-trees store all data in the leaves and only keys and child pointers internally. Basic operations on B-trees like search, insert, create, and delete are explained along with examples.

Uploaded by

rajnikant pubg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

B-Trees

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Introduction to B-Tree

• B-trees are balanced search tree.


• More than two children are possible.
• B-Tree, stores all information in the leaves and
stores only keys and Child pointer.
• If an internal B-tree node x contains n[x] keys
then x has n[x]+1 children.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Example of B-Tree

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Another example of B-Tree

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Application of B-Tree

It designed to work on magnetic disks or other (direct access) secondary storage devices.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Properties of B-Tree
A B-Tree T is a rooted tree having the following properties:
1. Every node x has the following fields:
1. n[x] the no. of keys currently stored in node x
2. The n[x] keys themselves, stored in non-decreasing
order, so that key1[x] ≤ key2[x]…… ≤keyn-1[x] ≤
keyn[x].
3. Leaf[x], a Boolean value that is TRUE if x is a leaf and
FALSE if x is an internal node.
2. Each internal node x also contains n[x]+1 pointers (Childs)
c1[x], c2[x],---------cn[x]+1[x].
3. All leaves have the same depth, which is the tree’s height h.
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
Properties of B-Tree (cont.)
4. There are lower and upper bounds on the no. of
keys a node can contains: these bounds can be
expressed in terms of a fixed integer t≥2 called the
minimum degree of B-Tree.
– Every node other than the root must have at least t-1
keys, then root has at least t children if the tree is non
empty the root must have at least one key.
– Every node can contain at most 2t-1 keys. Therefore, an
internal node can have at most 2t children we say that a
node is full if it contains exactly 2t-1 keys.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Height of B-tree
• Theorem:
If n ≥ 1, then for any n-key B-tree T of height h and minimum degree t ≥ 2,

h ≤ logt (n+1)/2
• Proof:
– The root contains at least one key
– All other nodes contain at least t-1 keys.
– There are at least 2 nodes at depth 1, at least 2t nodes at depth 2, at least 2ti-1
nodes at depth i and 2th-1 nodes at depth h

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Basic operation on B-tree
• B-TREE-SEARCH :-Searching in B Tree

• B-TREE-INSERT :-Inserting key in B Tree

• B-TREE-CREATE :-Creating a B Tree

• B-TREE-DELETE :- Deleting a key from B Tree

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


x is a subtree and k is searching element

Complexity=
O(t)

Complexity=
O(logt n)

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Creating an empty B tree

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
INSERT

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
B-TREE-SPLIT-CHILD ALGORITHM
B-TREE-SPLIT-CHILD(x,i,y)
1. z ALLOCATE-NODE()
2. leaf [z] leaf [y]
3. n[z]t-1
4. for j 1 to t-1
5. do keyj [z]  keyj+t [y]
6. if not leaf [y]
7. then for j1 to t
8. do cj [z] cj+t [y]
9. n[y] t-1
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
cont…….
10. for j n[x] +1 downto i+1
11. do cj+1 [x] cj [x]
12. cj+1 [x]  z
13. for j n[x] downto i
14. do keyj+1 [x]  keyj [x]
15. keyi [x]  keyt [y]
16. n[x]n[x] +1
17. DISK-WRITE(y)
18. DISK-WRITE(z)
19. DISK-WRITE(x)
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
B-TREE-INSERT ALGORITHM
B-TREE-INSERT(T,k)
1. r  root[T]
2. If ( n[r] = 2t-1)
3. then s Allocate-Node()
4. root[T]  s
5. leaf[s]  FALSE
6. n[s]0
7. c1[s]r
8. B-TREE-SPLIT-CHILD(s,1,r)
9. B-TREE-INSERT-NONFULL(s,k)
10. else B-TREE-INSERT-NONFULL(r,k)

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
t=3

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Insert Example
t=3 GMPX

ACDE JK NO RSTUV YZ

Insert B

GMPX

ABCDE JK NO RSTUV YZ

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Insert Example (Continued)
GMPX

ABCDE JK NO RSTUV YZ

Insert Q

GMPTX

ABCDE JK NO QRS UV YZ

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Insert Example (Continued)
GMPTX

ABCDE JK NO QRS UV YZ
Insert L

GM TX

ABCDE JKL NO QRS UV YZ

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


P

GM TX

ABCDE JKL NO QRS UV YZ

Insert F

CGM TX

AB DEF JKL NO QRS UV YZ


Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
Constructing a B-tree
• Suppose we start with an empty B-tree and keys
arrive in the following order:1 12 8 2 25 6 14 28
17 7 52 16 48 68 3 26 29 53 55 45
• Construct a B-tree of order 3

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Exercise in Inserting a B-Tree
• Insert the following keys in B-tree when t=3 :
• 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4,
31, 35, 56

• Check your approach with a neighbour and


discuss any differences.

26
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deleting from B-
Trees

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


The Concept
• You can delete a key entry from any node.
• ->Therefore, you must ensure that
before/after deletion, the B-Tree maintains
its properties.
• When deleting, you have to ensure that a
node doesn’t get too small (minimum node
size is T – 1). We prevent this by combining
nodes together.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Lets look at an example:
We’re given this valid B-Tree

Note: T = 3

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases
• Case 1: If the key k is in node x and x is a
leaf node having atleast t keys - then delete
k from x.
x x
…k… leaf … …
 t keys  t–1 keys

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Simple Deletion
Case 1: We delete “F”

Result: We remove “F” from the leaf node. No further


action needed.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases (Continued)
• Case 2: If the child key k is in node x and x is
an internal node, do the following:
x

not a leaf
…k…

y z

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases (Continued)
❑Subcase a: If the child y that precedes k has at least
t keys then find predecessor k´ of k in subtree rooted
at y, recursively delete k´ and replace k by k´ in x.

x x
not a leaf … k … … k´…

y y
 t keys  t keys

pred
k´ of k

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deleting and shifting
Case 2a: We deleted “M”

Result: We remove “M” from the parent node. Since


there are four nodes and two letters, we move “L” to
replace “M”. Now, the “N O” node has a parent again.

L
Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal
Deletion Cases (Continued)
Subcase B: Symmetrically, if the child z that follows k in
node x has at least t keys then find successor k´ of k in
subtree rooted at z, recursively delete k´and replace k by k´
in x.
x x
not a leaf … k … … k´…

z z
 t keys  t keys

succ
k´ of k

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases (Continued)
Subcase C: y and z both have t–1 keys -- merge k and z
into y, free z, recursively delete k from y.

x x
not a leaf … k … not a leaf … …

y z y y’s keys, k, z’s keys


t–1 keys t–1 keys 2t–1 keys

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Combining and Deleting
Case 2c: Now, we delete “G”

Result: First, we combine nodes “DE” and “JK”. Then,


we push down “G” into the “DEJK” node and delete it as
a leaf.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Combining and Deleting
Case 2c: Now, we delete “G”

Result: First, we combine nodes “DE” and “JK”. Then,


we push down “G” into the “DEJK” node and delete it as
a leaf.

C L

D E G J K

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases (Continued)

Case 3: k not in internal node. Let ci[x] be the root of the


subtree that must contain k, if k is in the tree.
If ci[x] has at least t keys, then recursively descend;
otherwise, execute 3.A and 3.B as necessary.

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases (Continued)

Subcase A: ci[x] has t–1 keys, some sibling has at least t keys.
recursively
x descend x
not a leaf … k1 … … k2 …

ci[x] k2 … ci[x] k1 …
t–1 keys t keys

k k

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deleting “B”
Before:

After: Deleted “B”, Demoted “C”, Promoted “E”

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Deletion Cases (Continued)
Subcase B: ci[x] and sibling both have t–1 keys.

recursively
descend x
x
not a leaf … k1 … … …
ci[x]
ci[x] ci+1[x] ci[x]’s keys, k1 , ci+1[x]’s keys
t–1 keys t–1 keys 2t–1 keys

k k

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Combining and Deleting
Case 3b: Now, we delete “D”

Result: First, we combine nodes “DE” and “JK”. Then,


we push down “G” into the “DEJK” node and delete “D”
as a leaf.

C L

D E G J K

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Type #1: Simple leaf deletion
Assuming a 5-way
B-Tree, as before... 12 29 52

2 7 9 15 22 31 43 56 69 72

Delete 2: Since there are enough


keys in the node, just delete it

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Type #2: Simple non-leaf deletion
12 29 56
52 Delete 52

7 9 15 22 31 43 56 69 72

Borrow the predecessor


or (in this case) successor

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Type #4: Too few keys in node
and its siblings
12 29 56

Join back together

7 9 15 22 31 43 69 72
Too few keys!
Delete 72

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Type #4: Too few keys in node and
its siblings
12 29

7 9 15 22 31 43 56 69

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Type #3: Enough siblings

12 29
Demote root key and
promote leaf key

7 9 15 22 31 43 56 69

Delete 22

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal


Type #3: Enough siblings

12 31

7 9 15 29 43 56 69

Program Name: BCA+MCA Faculty Name: Dr. Monika Bansal

You might also like