Professional Documents
Culture Documents
Lec-12 BTrees
Lec-12 BTrees
Applied Algorithms
(Lecture 12)
B-Trees
Fall-23
Motivation
• When data is too large to fit in the main memory,
then it is retrieved on need basis from the disk.
Motivation (contd.)
• Secondary memory (disk) is divided into equal-sized
blocks (typical size are 512, 2048,4096, or 8192
bytes).
B-Trees
• A B-tree of order m is m-way search tree.
B-Tree Properties
B-Tree is a rooted tree with root[T] with the following
properties:
Properties Contd…
2-if x is an internal node, it also contains n[ x]+1 pointers
to its children. Leaf node contains no children.
Properties Contd…
5- There are lower and upper bound on the number of keys a
node can contain.
Why t cant be 1?
Properties Contd…
a-Every node other than the root must have at least t-1
keys, Every internal node other than root, thus has at
least t children. If the tree is non empty, the root must
have at least one key.
Height of a B-Tree
Height of a B-Tree.
n 1
h log
t 2
National University Of Computer & Emerging Sciences
Lecture 12 30/10/2003 B-Trees
root[T]
# of nodes
1 1
t-1 t-1 2
t t
t-1 t-1 t-1 t-1 2t
t t t
t
proof
• Number of nodes is minimized, when root
contains one key and all other nodes contain t-1
keys.
Proof( Contd.)
t h 1
n 1 2(t 1)
t 1
n 2t h 1
n 1 2t h
n 1
h log t
2
National University Of Computer & Emerging Sciences
Lecture 12 30/10/2003 B-Trees
Numerical Example
Operation on B-Trees
• Searching a B-Tree.
Searching a B-Tree
Searching (Contd.)
Searching( Contd.)
• The nodes encountered during the recursion
forms a path downward from the root of the
tree.
Splitting (contd..)
Splitting (contd..)
Splitting (contd..)
…N W… …N S W…
P Q R S T U V P Q R T U V
Insertion in a B-Tree
G M P Q X
A C D E J K N O R S T U V Y Z
Inserting B G M P X
A B C D E J K N O R S T U V Y Z
Inserting L
G M P X
A B C D E J K L N O R S U V Y Z
Inserting F G M P X
A B C D E J K L N O R S U V Y Z
C G M P X
A B D E F J K L N O R S U V Y Z