B Tree: Muhammad Haris Department of Computer Science M.haris@nu - Edu.pk
B Tree: Muhammad Haris Department of Computer Science M.haris@nu - Edu.pk
Muhammad Haris
Department of Computer Science
[email protected]
1
Today’s Lecture
Introduction to B tree
Properties of B tree
Insertion into B tree
Examples
Activity to submit now
B-tree concept
BST?
AVL trees/Balance tree
6 12
42 51 62
1 2 4 7 8 13 15 18 25
27 29 45 46 48 53 55 60 64 70 90
4. The keys x.keyi separate the ranges of keys stored in each subtree:
if ki is any key stored in the subtree with root x.c i then
k1 ≤ x.key1 ≤ k2 ≤ x.key2 ≤ … ≤ x.keyx.n ≤ kx.n+1
e.g consider 6|12 node
6 12
1 2 4 7 8 13 15 18 25
B tree Overall properties
Balance tree m-way tree
More than 2 Childs but actually its balance tree (BST)
All leaf nodes must be at same level 2,3,4 or k
Always add items to the leaf node
All order of m leaf have following properties
Ever node has at most m Childs
Min children could be zero for leaf, 2 for root and ceil of (
m/2) for all other nodes
Every node has m-1 keys (values)
Min keys for root will be 1
All other nodes will have minimum keys ceilof(m/2)-1
M way B-tree
5 way tree
A B-tree of order 5, that is, internal nodes can have children
three, four or five children
m-1 nodes max keys
• 3 way tree
A B-tree of order 3, that is, internal nodes can have two or
three children.
m-1 nodes max keys
Insertion value X into B-tree
This strategy might have to be repeated all the way to the top
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
We want to construct a B-tree of order 5
The first four items go into the root:
1 2 8 12
1 2 12 25
1 2 6 12 14 25 28
17 B-Trees
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
1 2 6 12 14 25 28
1 2 6 7 12 14 16 25 28 48 52
18 B-Trees
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
1 2 6 7 12 14 16 25 26 28 29 52 53 55 68
19 B-Trees
1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45
17
3 8 28 48
1 2 6 7 12 14 16 25 26 29 45 52 53 55 68
20 B-Trees
Exercise in Inserting a B-Tree
Home Task
Insert the following Letters to a 3-way B-tree:
CNGAHEKQMFWLT
21 B-Trees
Analysis of B-Tree
Two Principle component of running time :
The number of disc accesses
The CPU computing time
B-Tree-Search
Search 55
17
3 8 28 48
1 2 6 7 12 14 16 25 26 29 45 52 53 55 68
24 B-Trees
Analysis of B-Tree Search
Number of Disk access
The B-TREE-SEARCH procedure accesses disk O(h)=
O(logt n) , where h is the height of the B-tree and n is
the number of keys in the B-tree.
Assumption 2t=m
Each node has 2t-1 items/keys
CPU time
Since x.n < 2t, the while loop of lines 2–3 takes O(t)
time within each node
the total CPU time is O(th)=O(t logtn).
Self study
Read code for the insertion in a B tree (CLRS pages :
Chapter 18 p 491 t0 495)
Analysis of pseudo code should be O(th)
Contd..
It is actually a proactive insertion algorithm where before going down to a
node, we split it if it is full.
The advantage of splitting before is, we never traverse a node twice. If we
don’t split a node before going down to it and split it only if new key is
inserted (reactive), we may end up traversing all nodes again from leaf to
root.
This happens in cases when all nodes on the path from root to leaf are full.
So when we come to the leaf node, we split it and move a key up. Moving a
key up will cause a split in parent node (because parent was already full).
This cascading effect never happens in this proactive insertion algorithm.
There is a disadvantage of this proactive insertion though, we may do
unnecessary splits.