B Tree
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.
Example of B-Tree
Another example of B-Tree
Application of B-Tree
It designed to work on magnetic disks or other (direct access)
secondary storage devices.
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.
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.
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
X is a subtree and k is searching element
Complexity=O(t)
Complexity=
O(logt n)
Creating an empty B tree
b-tree.ppt
INSERT
b-tree.ppt
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
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)
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)
b-tree.ppt
t=3
Comp 750, Fall 2009 B-Trees - 21
Insert Example
G M P X
A C D E J K N O R S T U V Y Z
t = 3
Insert B
G M P X
A B C D E J K N O R S T U V Y Z
Comp 750, Fall 2009 B-Trees - 22
Insert Example (Continued)
Insert Q
G M P X
A B C D E J K N O R S T U V Y Z
G M P T X
A B C D E J K N O Q R S Y Z
U V
Comp 750, Fall 2009 B-Trees - 23
Insert Example (Continued)
Insert L
G M
A B C D E J K L N O Q R S Y Z
U V
G M P T X
A B C D E J K N O Q R S Y Z
U V
P
T X
Comp 750, Fall 2009 B-Trees - 24
Insert Example (Continued)
Insert F
C G M
D E F J K L N O Q R S Y Z
U V
P
T X
G M
A B C D E J K L N O Q R S Y Z
U V
P
T X
A B
• 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
• We want to construct a B-tree of degree 3
• The first four items go into the root:
• To put the fifth item in the root would
violate condition 5
• Therefore, when 25 arrives, pick the
middle key to make a new root
Constructing a B-tree
12
8
1 2
Constructing a B-tree
Add 6 to the tree
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
1 2 25
Exceeds Order.
Promote middle and
split.
Constructing a B-tree (contd.)
6, 14, 28 get added to the leaf nodes:
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
1 2 25
12
8
1 2 25
6
1 2 28
14
Constructing a B-tree (contd.)
Adding 17 to the right leaf node would over-fill it, so we take
the middle key, promote it (to the root) and split the leaf
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
2 25
6
1 2 28
14 28
17
Constructing a B-tree (contd.)
7, 52, 16, 48 get added to the leaf nodes
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
25
6
1 2 28
14
17
7 52
16 48
Constructing a B-tree (contd.)
Adding 68 causes us to split the right most leaf,
promoting 48 to the root
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
8 17
7
6
2
1 16
14
12 52
48
28
25 68
Constructing a B-tree (contd.)
Adding 3 causes us to split the left most leaf
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
48
17
8
7
6
2
1 16
14
12 25 28 52 68
3 7
Constructing a B-tree (contd.)
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
Add 26, 29, 53, 55 then go into the leaves
48
17
8
3
1 2 6 7 52 68
25 28
16
14
12 26 29 53 55
Constructing a B-tree (contd.)
Add 45 increases the trees level
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
48
17
8
3
29
28
26
25 68
55
53
52
16
14
12
6 7
1 2 45
Exceeds Order.
Promote middle and
split.
Exceeds Order.
Promote middle and
split.
34
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Deleting from B-Trees
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.
Lets look at an example:
We’re given this valid B-Tree
Source: Introduction to Algorithms, Thomas H. Cormen
Note: T = 3
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.
… k …
 t keys
x
… …
 t–1 keys
x
leaf
Simple Deletion
Case 1: We delete “F”
Source: Introduction to Algorithms, Thomas H. Cormen
Result: We remove “F” from the leaf node. No further
action needed.
F
Deletion Cases (Continued)
• Case 2: If the child key k is in node x and
x is an internal node, do the following:
… k …
not a leaf
y z
x
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.
… k …
not a leaf
y
 t keys
k´
pred
of k
 t keys
x
… k´…
y
x
Deleting and shifting
Case 2a: We deleted “M”
Source: Introduction to Algorithms, Thomas H. Cormen
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.
M
L
Comp 750, Fall 2009 B-Trees - 44
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.
… k …
not a leaf
z
 t keys
k´
succ
of k
 t keys
x
… k´…
z
x
Comp 750, Fall 2009
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.
… k …
x
not a leaf
y z
t–1 keys t–1 keys
… …
x
not a leaf
y’s keys, k, z’s keys
y
2t–1 keys
Combining and Deleting
Case 2c: Now, we delete “G”
Source: Introduction to Algorithms, Thomas H. Cormen
Result: First, we combine nodes “DE” and “JK”. Then,
we push down “G” into the “DEJK” node and delete it as
a leaf.
Combining and Deleting
Case 2c: Now, we delete “G”
Source: Introduction to Algorithms, Thomas H. Cormen
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 J K
G
Comp 750, Fall 2009 B-Trees - 48
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.
Comp 750, Fall 2009 B-Trees - 49
Deletion Cases (Continued)
Subcase A: ci[x] has t–1 keys, some sibling has at least t keys.
… …
not a leaf
ci[x]
t–1 keys
k
x
k1
…
k2
… …
ci[x]
t keys
k
x
k2
…
k1
recursively
descend
Deleting “B”
Before:
After: Deleted “B”, Demoted “C”, Promoted “E”
Comp 750, Fall 2009 B-Trees - 51
Deletion Cases (Continued)
Subcase B: ci[x] and sibling both have t–1 keys.
… …
not a leaf
ci[x]
t–1 keys
k
x
k1
t–1 keys
ci+1[x]
… …
ci[x]’s keys, , ci+1[x]’s keys
ci[x]
2t–1 keys
k
x
k1
recursively
descend
Combining and Deleting
Case 3b: Now, we delete “D”
Source: Introduction to Algorithms, Thomas H. Cormen
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
E G J K
D
CSCI 2720
53
Type #1: Simple leaf deletion
12 29 52
2 7 9 15 22 56 69 72
31 43
Delete 2: Since there are enough
keys in the node, just delete it
Assuming a 5-way
B-Tree, as before...
Note when printed: this slide is animated
CSCI 2720
54
Type #2: Simple non-leaf
deletion
12 29 52
7 9 15 22 56 69 72
31 43
Delete 52
Borrow the predecessor
or (in this case) successor
56
Note when printed: this slide is animated
CSCI 2720
55
Type #4: Too few keys in node
and its siblings
12 29 56
7 9 15 22 69 72
31 43
Delete 72
Too few keys!
Join back together
Note when printed: this slide is animated
CSCI 2720
56
Type #4: Too few keys in node
and its siblings
12 29
7 9 15 22 69
56
31 43
Note when printed: this slide is animated
CSCI 2720
57
Type #3: Enough siblings
12 29
7 9 15 22 69
56
31 43
Delete 22
Demote root key and
promote leaf key
Note when printed: this slide is animated
CSCI 2720
58
Type #3: Enough siblings
12
29
7 9 15
31
69
56
43
Note when printed: this slide is animated

More Related Content

PPTX
Stacks IN DATA STRUCTURES
PPTX
Queues
PDF
21 All Pairs Shortest Path
PPTX
Theory of Automata and formal languages unit 1
PPT
PDF
Red black tree
PPT
Python Dictionaries and Sets
PPTX
Linked List
Stacks IN DATA STRUCTURES
Queues
21 All Pairs Shortest Path
Theory of Automata and formal languages unit 1
Red black tree
Python Dictionaries and Sets
Linked List

What's hot (20)

PPT
5.1 greedy
PPTX
Graph representation
PPTX
linked list
PPT
Queue implementation
PPTX
Queue - Data Structure - Notes
PPT
Red black tree
PPT
Data Structure and Algorithms Binary Search Tree
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
PPT
Priority queues
PPTX
PPTX
Threaded Binary Tree.pptx
PPT
Binary search tree(bst)
PPTX
Bfs and Dfs
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPTX
Binary Tree in Data Structure
PPTX
Linear and Binary search
PPTX
Queue in Data Structure
PPT
Notes DATA STRUCTURE - queue
PPT
Recursion - Algorithms and Data Structures
PPTX
Priority Queue in Data Structure
5.1 greedy
Graph representation
linked list
Queue implementation
Queue - Data Structure - Notes
Red black tree
Data Structure and Algorithms Binary Search Tree
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Priority queues
Threaded Binary Tree.pptx
Binary search tree(bst)
Bfs and Dfs
Trees, Binary Search Tree, AVL Tree in Data Structures
Binary Tree in Data Structure
Linear and Binary search
Queue in Data Structure
Notes DATA STRUCTURE - queue
Recursion - Algorithms and Data Structures
Priority Queue in Data Structure
Ad

Similar to b-tree.ppt (20)

PPTX
PPT
B tree and application B tree and application
PPT
B tree by-jash acharya
PPTX
Presentation on b trees [autosaved]
PDF
B TREE ( a to z concept ) in data structure or DBMS
PPTX
B trees2
PPT
109885098-B-Trees-And-B-Trees in data structure.ppt
PPT
16807097.ppt b tree are a good data structure
PDF
PPT
08 B Trees
PPT
BTrees-fall2010.ppt
PPT
b-trees a tree non linear data structure.ppt
PPT
Btree
PPT
B trees and_b__trees
PPTX
B and B+ tree
PPT
B-and-B-Tree-ppt presentation in data structure
PPT
PPT
B trees and_b__trees
PPTX
B best on presentation on tree ppt.pptx
B tree and application B tree and application
B tree by-jash acharya
Presentation on b trees [autosaved]
B TREE ( a to z concept ) in data structure or DBMS
B trees2
109885098-B-Trees-And-B-Trees in data structure.ppt
16807097.ppt b tree are a good data structure
08 B Trees
BTrees-fall2010.ppt
b-trees a tree non linear data structure.ppt
Btree
B trees and_b__trees
B and B+ tree
B-and-B-Tree-ppt presentation in data structure
B trees and_b__trees
B best on presentation on tree ppt.pptx
Ad

Recently uploaded (20)

PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Amdahl’s law is explained in the above power point presentations
PPTX
mechattonicsand iotwith sensor and actuator
PPTX
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
PDF
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
PDF
Project_Mgmt_Institute_-Marc Marc Marc .pdf
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
Design of Material Handling Equipment Lecture Note
PDF
Cryptography and Network Security-Module-I.pdf
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
DOCX
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PDF
Unit1 - AIML Chapter 1 concept and ethics
PDF
Computer organization and architecuture Digital Notes....pdf
PPTX
Environmental studies, Moudle 3-Environmental Pollution.pptx
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
"Array and Linked List in Data Structures with Types, Operations, Implementat...
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Amdahl’s law is explained in the above power point presentations
mechattonicsand iotwith sensor and actuator
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
Project_Mgmt_Institute_-Marc Marc Marc .pdf
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Design of Material Handling Equipment Lecture Note
Cryptography and Network Security-Module-I.pdf
MLpara ingenieira CIVIL, meca Y AMBIENTAL
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
Unit1 - AIML Chapter 1 concept and ethics
Computer organization and architecuture Digital Notes....pdf
Environmental studies, Moudle 3-Environmental Pollution.pptx

b-tree.ppt

  • 2. 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.
  • 5. Application of B-Tree It designed to work on magnetic disks or other (direct access) secondary storage devices.
  • 6. 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.
  • 7. 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.
  • 8. 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
  • 9. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 10. 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
  • 11. X is a subtree and k is searching element Complexity=O(t) Complexity= O(logt n)
  • 16. 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
  • 17. 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)
  • 18. 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)
  • 20. t=3
  • 21. Comp 750, Fall 2009 B-Trees - 21 Insert Example G M P X A C D E J K N O R S T U V Y Z t = 3 Insert B G M P X A B C D E J K N O R S T U V Y Z
  • 22. Comp 750, Fall 2009 B-Trees - 22 Insert Example (Continued) Insert Q G M P X A B C D E J K N O R S T U V Y Z G M P T X A B C D E J K N O Q R S Y Z U V
  • 23. Comp 750, Fall 2009 B-Trees - 23 Insert Example (Continued) Insert L G M A B C D E J K L N O Q R S Y Z U V G M P T X A B C D E J K N O Q R S Y Z U V P T X
  • 24. Comp 750, Fall 2009 B-Trees - 24 Insert Example (Continued) Insert F C G M D E F J K L N O Q R S Y Z U V P T X G M A B C D E J K L N O Q R S Y Z U V P T X A B
  • 25. • 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 • We want to construct a B-tree of degree 3 • The first four items go into the root: • To put the fifth item in the root would violate condition 5 • Therefore, when 25 arrives, pick the middle key to make a new root Constructing a B-tree 12 8 1 2
  • 26. Constructing a B-tree Add 6 to the tree 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 1 2 25 Exceeds Order. Promote middle and split.
  • 27. Constructing a B-tree (contd.) 6, 14, 28 get added to the leaf nodes: 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 1 2 25 12 8 1 2 25 6 1 2 28 14
  • 28. Constructing a B-tree (contd.) Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 2 25 6 1 2 28 14 28 17
  • 29. Constructing a B-tree (contd.) 7, 52, 16, 48 get added to the leaf nodes 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 25 6 1 2 28 14 17 7 52 16 48
  • 30. Constructing a B-tree (contd.) Adding 68 causes us to split the right most leaf, promoting 48 to the root 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 8 17 7 6 2 1 16 14 12 52 48 28 25 68
  • 31. Constructing a B-tree (contd.) Adding 3 causes us to split the left most leaf 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 48 17 8 7 6 2 1 16 14 12 25 28 52 68 3 7
  • 32. Constructing a B-tree (contd.) 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 Add 26, 29, 53, 55 then go into the leaves 48 17 8 3 1 2 6 7 52 68 25 28 16 14 12 26 29 53 55
  • 33. Constructing a B-tree (contd.) Add 45 increases the trees level 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 48 17 8 3 29 28 26 25 68 55 53 52 16 14 12 6 7 1 2 45 Exceeds Order. Promote middle and split. Exceeds Order. Promote middle and split.
  • 34. 34 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.
  • 35. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 37. 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.
  • 38. Lets look at an example: We’re given this valid B-Tree Source: Introduction to Algorithms, Thomas H. Cormen Note: T = 3
  • 39. 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. … k …  t keys x … …  t–1 keys x leaf
  • 40. Simple Deletion Case 1: We delete “F” Source: Introduction to Algorithms, Thomas H. Cormen Result: We remove “F” from the leaf node. No further action needed. F
  • 41. Deletion Cases (Continued) • Case 2: If the child key k is in node x and x is an internal node, do the following: … k … not a leaf y z x
  • 42. 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. … k … not a leaf y  t keys k´ pred of k  t keys x … k´… y x
  • 43. Deleting and shifting Case 2a: We deleted “M” Source: Introduction to Algorithms, Thomas H. Cormen 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. M L
  • 44. Comp 750, Fall 2009 B-Trees - 44 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. … k … not a leaf z  t keys k´ succ of k  t keys x … k´… z x
  • 45. Comp 750, Fall 2009 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. … k … x not a leaf y z t–1 keys t–1 keys … … x not a leaf y’s keys, k, z’s keys y 2t–1 keys
  • 46. Combining and Deleting Case 2c: Now, we delete “G” Source: Introduction to Algorithms, Thomas H. Cormen Result: First, we combine nodes “DE” and “JK”. Then, we push down “G” into the “DEJK” node and delete it as a leaf.
  • 47. Combining and Deleting Case 2c: Now, we delete “G” Source: Introduction to Algorithms, Thomas H. Cormen 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 J K G
  • 48. Comp 750, Fall 2009 B-Trees - 48 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.
  • 49. Comp 750, Fall 2009 B-Trees - 49 Deletion Cases (Continued) Subcase A: ci[x] has t–1 keys, some sibling has at least t keys. … … not a leaf ci[x] t–1 keys k x k1 … k2 … … ci[x] t keys k x k2 … k1 recursively descend
  • 50. Deleting “B” Before: After: Deleted “B”, Demoted “C”, Promoted “E”
  • 51. Comp 750, Fall 2009 B-Trees - 51 Deletion Cases (Continued) Subcase B: ci[x] and sibling both have t–1 keys. … … not a leaf ci[x] t–1 keys k x k1 t–1 keys ci+1[x] … … ci[x]’s keys, , ci+1[x]’s keys ci[x] 2t–1 keys k x k1 recursively descend
  • 52. Combining and Deleting Case 3b: Now, we delete “D” Source: Introduction to Algorithms, Thomas H. Cormen 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 E G J K D
  • 53. CSCI 2720 53 Type #1: Simple leaf deletion 12 29 52 2 7 9 15 22 56 69 72 31 43 Delete 2: Since there are enough keys in the node, just delete it Assuming a 5-way B-Tree, as before... Note when printed: this slide is animated
  • 54. CSCI 2720 54 Type #2: Simple non-leaf deletion 12 29 52 7 9 15 22 56 69 72 31 43 Delete 52 Borrow the predecessor or (in this case) successor 56 Note when printed: this slide is animated
  • 55. CSCI 2720 55 Type #4: Too few keys in node and its siblings 12 29 56 7 9 15 22 69 72 31 43 Delete 72 Too few keys! Join back together Note when printed: this slide is animated
  • 56. CSCI 2720 56 Type #4: Too few keys in node and its siblings 12 29 7 9 15 22 69 56 31 43 Note when printed: this slide is animated
  • 57. CSCI 2720 57 Type #3: Enough siblings 12 29 7 9 15 22 69 56 31 43 Delete 22 Demote root key and promote leaf key Note when printed: this slide is animated
  • 58. CSCI 2720 58 Type #3: Enough siblings 12 29 7 9 15 31 69 56 43 Note when printed: this slide is animated